Skip to content

Commit 12acf39

Browse files
goffrieConvex, Inc.
authored and
Convex, Inc.
committed
Collapse DatabaseIndexSnapshotCache.documents into a single layer map (#36974)
OrdMap likes to clone its contents and if those contents are a BTreeMap, then that cloning can get expensive. `OrdMap<K, OrdMap>` would also work, but in this case we can use slightly cleverer ranges and collapse into a single layer of map. GitOrigin-RevId: a44d57bdb707dba3bb04e81745e99bef4fc70b3e
1 parent c8b0780 commit 12acf39

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

crates/indexing/src/backend_in_memory_indexes.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use common::{
2424
},
2525
instrument,
2626
interval::{
27+
End,
2728
Interval,
2829
IntervalSet,
2930
},
@@ -655,7 +656,7 @@ struct DatabaseIndexSnapshotCache {
655656
/// After the cache has been fully populated, `db.get`s which do point
656657
/// queries against by_id will be cached, and any indexed query against
657658
/// by_age that is a subset of (<age:18>, Unbounded) will be cached.
658-
documents: OrdMap<IndexId, BTreeMap<IndexKeyBytes, (Timestamp, PackedDocument)>>,
659+
documents: OrdMap<(IndexId, IndexKeyBytes), (Timestamp, PackedDocument)>,
659660
intervals: OrdMap<IndexId, IntervalSet>,
660661
cache_size: usize,
661662
}
@@ -696,9 +697,7 @@ impl DatabaseIndexSnapshotCache {
696697
let result_size: usize = doc.value().size();
697698
let interval = Interval::prefix(index_key_bytes.clone().into());
698699
self.documents
699-
.entry(index_id)
700-
.or_default()
701-
.insert(index_key_bytes, (ts, doc));
700+
.insert((index_id, index_key_bytes), (ts, doc));
702701
self.intervals.entry(index_id).or_default().add(interval);
703702
self.cache_size += result_size;
704703
}
@@ -741,20 +740,19 @@ impl DatabaseIndexSnapshotCache {
741740
}
742741
if in_set {
743742
cache_hit_count += 1;
744-
match self.documents.get(&index_id) {
745-
None => {},
746-
Some(range) => {
747-
results.extend(range.range(&component_interval).map(
748-
|(index_key, (ts, doc))| {
749-
DatabaseIndexSnapshotCacheResult::Document(
750-
index_key.clone(),
751-
*ts,
752-
doc.clone(),
753-
)
754-
},
755-
));
756-
},
757-
}
743+
let range = self
744+
.documents
745+
.range((index_id, IndexKeyBytes(component_interval.start.0.into()))..)
746+
.take_while(|&((index, key), _)| {
747+
*index == index_id
748+
&& match &component_interval.end {
749+
End::Excluded(end) => key[..] < end[..],
750+
End::Unbounded => true,
751+
}
752+
});
753+
results.extend(range.map(|((_, index_key), (ts, doc))| {
754+
DatabaseIndexSnapshotCacheResult::Document(index_key.clone(), *ts, doc.clone())
755+
}));
758756
} else {
759757
results.push(DatabaseIndexSnapshotCacheResult::CacheMiss(
760758
component_interval,

0 commit comments

Comments
 (0)