Skip to content

Commit 3c6734e

Browse files
authored
refactor: Simplify heap size estimation for arrays (#22954)
This introduces a macro for the redundant heap size estimations for arrays. ## Which issue does this PR close? - Closes None. ## Rationale for this change This pr simplifies the heap size estimation for arrow arrays by introducing a macro to remove redundant code. ## What changes are included in this PR? See above. ## Are these changes tested? Yes. ## Are there any user-facing changes? No.
1 parent 15bc933 commit 3c6734e

1 file changed

Lines changed: 80 additions & 41 deletions

File tree

datafusion/common/src/heap_size.rs

Lines changed: 80 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -326,47 +326,6 @@ impl DFHeapSize for Fields {
326326
}
327327
}
328328

329-
impl DFHeapSize for StructArray {
330-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
331-
self.get_array_memory_size()
332-
}
333-
}
334-
335-
impl DFHeapSize for LargeListArray {
336-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
337-
self.get_array_memory_size()
338-
}
339-
}
340-
341-
impl DFHeapSize for LargeListViewArray {
342-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
343-
self.get_array_memory_size()
344-
}
345-
}
346-
347-
impl DFHeapSize for ListArray {
348-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
349-
self.get_array_memory_size()
350-
}
351-
}
352-
353-
impl DFHeapSize for ListViewArray {
354-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
355-
self.get_array_memory_size()
356-
}
357-
}
358-
359-
impl DFHeapSize for FixedSizeListArray {
360-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
361-
self.get_array_memory_size()
362-
}
363-
}
364-
impl DFHeapSize for MapArray {
365-
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
366-
self.get_array_memory_size()
367-
}
368-
}
369-
370329
impl<T: DFHeapSize> DFHeapSize for Box<T> {
371330
fn heap_size(&self, ctx: &mut DFHeapSizeCtx) -> usize {
372331
size_of::<T>() + self.as_ref().heap_size(ctx)
@@ -469,6 +428,29 @@ impl_zero_heap_size!(
469428
DateTime<Utc>,
470429
);
471430

431+
/// Implement [`DFHeapSize`] for Arrow arrays types.
432+
macro_rules! impl_array_heap_size {
433+
($($t:ty),+ $(,)?) => {
434+
$(
435+
impl DFHeapSize for $t {
436+
fn heap_size(&self, _: &mut DFHeapSizeCtx) -> usize {
437+
self.get_array_memory_size()
438+
}
439+
}
440+
)+
441+
};
442+
}
443+
444+
impl_array_heap_size!(
445+
StructArray,
446+
LargeListArray,
447+
LargeListViewArray,
448+
ListArray,
449+
ListViewArray,
450+
FixedSizeListArray,
451+
MapArray,
452+
);
453+
472454
#[cfg(test)]
473455
mod tests {
474456
use super::*;
@@ -696,4 +678,61 @@ mod tests {
696678
let field = Field::new("temperature", DataType::Float64, true);
697679
assert!(size(&field) > 0);
698680
}
681+
682+
#[test]
683+
fn test_list_array() {
684+
use arrow::array::types::Int32Type;
685+
686+
let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
687+
Some(vec![Some(1), Some(2), Some(3)]),
688+
Some(vec![Some(4)]),
689+
]);
690+
assert_eq!(size(&array), array.get_array_memory_size());
691+
assert!(size(&array) > 0);
692+
693+
let large =
694+
LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![Some(vec![
695+
Some(1),
696+
Some(2),
697+
])]);
698+
assert_eq!(size(&large), large.get_array_memory_size());
699+
assert!(size(&large) > 0);
700+
}
701+
702+
#[test]
703+
fn test_struct_array() {
704+
use arrow::array::Int32Array;
705+
706+
let array = StructArray::from(vec![(
707+
Arc::new(Field::new("a", DataType::Int32, true)),
708+
Arc::new(Int32Array::from(vec![1, 2, 3])) as _,
709+
)]);
710+
assert_eq!(size(&array), array.get_array_memory_size());
711+
assert!(size(&array) > 0);
712+
}
713+
714+
#[test]
715+
fn test_fixed_size_list_array() {
716+
use arrow::array::Int32Array;
717+
718+
let values = Arc::new(Int32Array::from(vec![1, 2, 3, 4]));
719+
let field = Arc::new(Field::new("item", DataType::Int32, true));
720+
let array = FixedSizeListArray::new(field, 2, values, None);
721+
assert_eq!(size(&array), array.get_array_memory_size());
722+
assert!(size(&array) > 0);
723+
}
724+
725+
#[test]
726+
fn test_map_array() {
727+
use arrow::array::{Int32Builder, MapBuilder, StringBuilder};
728+
729+
let mut builder =
730+
MapBuilder::new(None, StringBuilder::new(), Int32Builder::new());
731+
builder.keys().append_value("key");
732+
builder.values().append_value(1);
733+
builder.append(true).unwrap();
734+
let array = builder.finish();
735+
assert_eq!(size(&array), array.get_array_memory_size());
736+
assert!(size(&array) > 0);
737+
}
699738
}

0 commit comments

Comments
 (0)