Skip to content

Commit 3e00f14

Browse files
committed
perf: optimize slot_db by removing redundant Orphan usage in DataCtner
- Replaced `Orphan<usize>` with `usize` in `DataCtner::Large`. - This eliminates an expensive extra DB read per item iteration in `slot_db`, resolving the 280x performance regression. - Length is now persisted as part of the `DataCtner` value itself, which is safe and efficient. - Updated CHANGELOG to reflect this optimization.
1 parent 373e366 commit 3e00f14

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ enum DataCtner<K> {
203203
Small(BTreeSet<K>),
204204
Large {
205205
map: MapxOrd<K, ()>,
206-
len: Orphan<usize> // Explicit length counter
206+
len: usize, // Explicit length counter (fast)
207207
},
208208
}
209209
```
@@ -212,6 +212,7 @@ This is an acceptable exception because:
212212
1. Slot DB explicitly needs length for its pagination algorithm
213213
2. The counter is clearly visible in the type definition
214214
3. It's maintained manually at the application level, not in the core
215+
4. `DataCtner` updates are fully persisted as values, ensuring the length counter is safe.
215216

216217
### Testing Changes
217218

utils/slot_db/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ where
563563
Small(BTreeSet<K>),
564564
Large {
565565
map: MapxOrd<K, ()>,
566-
len: Orphan<usize>,
566+
len: usize,
567567
},
568568
}
569569

@@ -578,7 +578,7 @@ where
578578
fn len(&self) -> usize {
579579
match self {
580580
Self::Small(i) => i.len(),
581-
Self::Large { len, .. } => len.get_value(),
581+
Self::Large { len, .. } => *len,
582582
}
583583
}
584584

@@ -600,7 +600,7 @@ where
600600

601601
*self = Self::Large {
602602
map: new_map,
603-
len: Orphan::new(set_len),
603+
len: set_len,
604604
};
605605
}
606606

@@ -613,7 +613,7 @@ where
613613
let existed = map.get(&k).is_some();
614614
map.insert(&k, &());
615615
if !existed {
616-
*len.get_mut() += 1;
616+
*len += 1;
617617
}
618618
!existed
619619
}
@@ -627,7 +627,7 @@ where
627627
let existed = map.get(target).is_some();
628628
if existed {
629629
map.remove(target);
630-
*len.get_mut() -= 1;
630+
*len -= 1;
631631
}
632632
existed
633633
}

0 commit comments

Comments
 (0)