Skip to content

Commit 1613cb8

Browse files
Preslav LeConvex, Inc.
Preslav Le
authored and
Convex, Inc.
committed
Use BTreeMap instead of OrdMap in Funrun (#24677)
Use BTreeMap instead of OrdMap in Funrun: 1. BTreeMaps are generally much more efficient memory structures. The only reason to use OrdMap is because they have cheap copy-on-write. However, we use them as read-only in FunrunInMemory indexes so there is no need for the copy-on-write. 2. The implementation of WithHeapSize for OrdMap is very inaccurate since it doesn't take into account the tree structure overhead like WithHeapSize for BTreeMap does. We could fix it, but implementing WithHeapSize for OrdMap is close to impossible due to the copy-on-write and efficient shared pointers. You can have 10000 OrdMaps that consume the same memory as 1 depending on which nodes are shared. Remove the implementation to avoid future confusion here. GitOrigin-RevId: fe2173bddd79714e8c258e4b31a7124c504dc229
1 parent 2d81311 commit 1613cb8

File tree

4 files changed

+4
-28
lines changed

4 files changed

+4
-28
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/function_runner/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ database = { path = "../database" }
1919
errors = { path = "../errors" }
2020
file_storage = { path = "../file_storage" }
2121
futures = { workspace = true }
22-
imbl = { workspace = true }
2322
indexing = { path = "../indexing" }
2423
isolate = { path = "../isolate" }
2524
keybroker = { path = "../keybroker" }

crates/function_runner/src/in_memory_indexes.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use futures::{
5454
FutureExt,
5555
TryStreamExt,
5656
};
57-
use imbl::OrdMap;
5857
use indexing::{
5958
backend_in_memory_indexes::{
6059
DatabaseIndexSnapshot,
@@ -158,7 +157,7 @@ struct CacheKey {
158157
/// The cache value is the same as [DatabaseIndexMap] apart from keeping track
159158
/// of last modified timestamps. The [OrdMap] keys are the index keys.
160159
#[derive(Clone)]
161-
struct CacheValue(WithHeapSize<OrdMap<Vec<u8>, (Timestamp, PackedDocument)>>);
160+
struct CacheValue(WithHeapSize<BTreeMap<Vec<u8>, (Timestamp, PackedDocument)>>);
162161

163162
impl SizedValue for CacheValue {
164163
fn size(&self) -> u64 {
@@ -181,12 +180,11 @@ async fn load_index(
181180
table_name: String,
182181
) -> anyhow::Result<CacheValue> {
183182
let _timer = load_index_timer(&table_name, &instance_name);
184-
let index_map: OrdMap<Vec<u8>, (Timestamp, PackedDocument)> = persistence_snapshot
183+
let index_map: BTreeMap<Vec<u8>, (Timestamp, PackedDocument)> = persistence_snapshot
185184
.index_scan(index_id, table_id, &Interval::all(), Order::Asc, usize::MAX)
186185
.map_ok(|(key, ts, doc)| (key.0, (ts, PackedDocument::pack(doc))))
187-
.try_collect::<Vec<_>>()
188-
.await?
189-
.into();
186+
.try_collect()
187+
.await?;
190188
log_funrun_index_load_rows(index_map.len() as u64, &table_name, &instance_name);
191189
Ok(CacheValue(index_map.into()))
192190
}

crates/value/src/heap_size.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::{
2626

2727
use ::bytes;
2828
use futures::channel::oneshot;
29-
use imbl::OrdMap;
3029
#[cfg(any(test, feature = "testing"))]
3130
use proptest::{
3231
prelude::Arbitrary,
@@ -709,25 +708,6 @@ impl<T: HeapSize> From<WithHeapSize<BTreeSet<T>>> for BTreeSet<T> {
709708
}
710709
}
711710

712-
impl<K: HeapSize + Ord, V: HeapSize> From<OrdMap<K, V>> for WithHeapSize<OrdMap<K, V>> {
713-
fn from(value: OrdMap<K, V>) -> Self {
714-
let elements_heap_size = value
715-
.iter()
716-
.map(|(k, v)| k.heap_size() + v.heap_size())
717-
.sum();
718-
Self {
719-
inner: value,
720-
elements_heap_size,
721-
}
722-
}
723-
}
724-
725-
impl<K: HeapSize + Ord, V: HeapSize> HeapSize for WithHeapSize<OrdMap<K, V>> {
726-
fn heap_size(&self) -> usize {
727-
self.elements_heap_size
728-
}
729-
}
730-
731711
impl HeapSize for serde_json::Map<String, JsonValue> {
732712
fn heap_size(&self) -> usize {
733713
// It's actually an IndexMap since we enable the preserve_order feature.

0 commit comments

Comments
 (0)