Skip to content

Commit e7e2277

Browse files
committed
Improve mem consumption estimate
1 parent 426fbf3 commit e7e2277

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/aggregation/bucket/composite/map.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ impl<K: Eq + Hash + Clone + Ord, V, const S: usize> ArrayHeapMap<K, V, S> {
4949
self.buckets.remove(&highest);
5050
}
5151
}
52+
53+
fn memory_consumption(&self) -> u64 {
54+
let key_size = std::mem::size_of::<[K; S]>();
55+
let map_size = (key_size + std::mem::size_of::<V>()) * self.buckets.capacity();
56+
let heap_size = key_size * self.heap.capacity();
57+
(map_size + heap_size) as u64
58+
}
5259
}
5360

5461
impl<K: Copy + Ord + Clone + 'static, V: 'static, const S: usize> ArrayHeapMap<K, V, S> {
@@ -249,6 +256,27 @@ impl<K: Ord + Hash + Clone, V> DynArrayHeapMap<K, V> {
249256
DynArrayHeapMapInner::Dim16(map) => map.evict_highest(),
250257
}
251258
}
259+
260+
pub(crate) fn memory_consumption(&self) -> u64 {
261+
match &self.0 {
262+
DynArrayHeapMapInner::Dim1(map) => map.memory_consumption(),
263+
DynArrayHeapMapInner::Dim2(map) => map.memory_consumption(),
264+
DynArrayHeapMapInner::Dim3(map) => map.memory_consumption(),
265+
DynArrayHeapMapInner::Dim4(map) => map.memory_consumption(),
266+
DynArrayHeapMapInner::Dim5(map) => map.memory_consumption(),
267+
DynArrayHeapMapInner::Dim6(map) => map.memory_consumption(),
268+
DynArrayHeapMapInner::Dim7(map) => map.memory_consumption(),
269+
DynArrayHeapMapInner::Dim8(map) => map.memory_consumption(),
270+
DynArrayHeapMapInner::Dim9(map) => map.memory_consumption(),
271+
DynArrayHeapMapInner::Dim10(map) => map.memory_consumption(),
272+
DynArrayHeapMapInner::Dim11(map) => map.memory_consumption(),
273+
DynArrayHeapMapInner::Dim12(map) => map.memory_consumption(),
274+
DynArrayHeapMapInner::Dim13(map) => map.memory_consumption(),
275+
DynArrayHeapMapInner::Dim14(map) => map.memory_consumption(),
276+
DynArrayHeapMapInner::Dim15(map) => map.memory_consumption(),
277+
DynArrayHeapMapInner::Dim16(map) => map.memory_consumption(),
278+
}
279+
}
252280
}
253281

254282
impl<K: Ord + Clone + Copy + 'static, V: 'static> DynArrayHeapMap<K, V> {

src/aggregation/bucket/composite/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,9 @@ impl SegmentAggregationCollector for SegmentCompositeCollector {
341341

342342
impl SegmentCompositeCollector {
343343
fn get_memory_consumption(&self) -> u64 {
344-
// TODO get correct estimate (these are just the keys)
345-
(self.buckets.size() * (std::mem::size_of::<InternalValueRepr>())) as u64
344+
// TODO: the footprint is underestimated because we don't account for the
345+
// sub-aggregations which are trait objects
346+
self.buckets.memory_consumption()
346347
}
347348

348349
pub(crate) fn from_req_and_validate(
@@ -601,6 +602,7 @@ fn recursive_key_visitor(
601602
let sub_level_sources = &sources[1..];
602603
let mut missing = true;
603604
for (i, accessor) in current_level_accessor.iter().enumerate() {
605+
// TODO: optimize with prefetching using fetch_block
604606
let values = accessor.column.values_for_doc(doc_id);
605607
match current_level_source {
606608
CompositeAggregationSource::Terms(term_source) => {

0 commit comments

Comments
 (0)