Skip to content

Commit aa2d756

Browse files
adriangbclaude
andcommitted
Use macro to reduce HeapSize impl repetition
Replace individual HeapSize implementations with a macro to reduce code duplication. A blanket impl<T: Array> is not possible due to Rust's orphan rules (E0210). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e9186e5 commit aa2d756

File tree

1 file changed

+36
-118
lines changed

1 file changed

+36
-118
lines changed

arrow-array/src/heap_size.rs

Lines changed: 36 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -28,134 +28,52 @@ use crate::{
2828
StringViewArray, StructArray, UnionArray,
2929
};
3030

31+
// Note: A blanket implementation `impl<T: Array> HeapSize for T` would be ideal,
32+
// but is not possible due to Rust's orphan rules (E0210) since HeapSize is defined
33+
// in a separate crate.
34+
//
3135
// Note: HeapSize cannot be implemented for ArrayRef (Arc<dyn Array>) here due to
3236
// Rust's orphan rules. Use array.get_buffer_memory_size() directly instead.
3337

34-
// =============================================================================
35-
// Primitive and Boolean Arrays
36-
// =============================================================================
38+
/// Implements HeapSize for array types that delegate to get_buffer_memory_size()
39+
macro_rules! impl_heap_size {
40+
($($ty:ty),*) => {
41+
$(
42+
impl HeapSize for $ty {
43+
fn heap_size(&self) -> usize {
44+
self.get_buffer_memory_size()
45+
}
46+
}
47+
)*
48+
};
49+
}
50+
51+
impl_heap_size!(
52+
BooleanArray,
53+
NullArray,
54+
StringArray,
55+
LargeStringArray,
56+
BinaryArray,
57+
LargeBinaryArray,
58+
StringViewArray,
59+
BinaryViewArray,
60+
FixedSizeBinaryArray,
61+
ListArray,
62+
LargeListArray,
63+
ListViewArray,
64+
LargeListViewArray,
65+
FixedSizeListArray,
66+
StructArray,
67+
MapArray,
68+
UnionArray
69+
);
3770

3871
impl<T: ArrowPrimitiveType> HeapSize for PrimitiveArray<T> {
3972
fn heap_size(&self) -> usize {
4073
self.get_buffer_memory_size()
4174
}
4275
}
4376

44-
impl HeapSize for BooleanArray {
45-
fn heap_size(&self) -> usize {
46-
self.get_buffer_memory_size()
47-
}
48-
}
49-
50-
impl HeapSize for NullArray {
51-
fn heap_size(&self) -> usize {
52-
// NullArray has no buffers
53-
0
54-
}
55-
}
56-
57-
// =============================================================================
58-
// String and Binary Arrays
59-
// =============================================================================
60-
61-
impl HeapSize for StringArray {
62-
fn heap_size(&self) -> usize {
63-
self.get_buffer_memory_size()
64-
}
65-
}
66-
67-
impl HeapSize for LargeStringArray {
68-
fn heap_size(&self) -> usize {
69-
self.get_buffer_memory_size()
70-
}
71-
}
72-
73-
impl HeapSize for BinaryArray {
74-
fn heap_size(&self) -> usize {
75-
self.get_buffer_memory_size()
76-
}
77-
}
78-
79-
impl HeapSize for LargeBinaryArray {
80-
fn heap_size(&self) -> usize {
81-
self.get_buffer_memory_size()
82-
}
83-
}
84-
85-
impl HeapSize for StringViewArray {
86-
fn heap_size(&self) -> usize {
87-
self.get_buffer_memory_size()
88-
}
89-
}
90-
91-
impl HeapSize for BinaryViewArray {
92-
fn heap_size(&self) -> usize {
93-
self.get_buffer_memory_size()
94-
}
95-
}
96-
97-
impl HeapSize for FixedSizeBinaryArray {
98-
fn heap_size(&self) -> usize {
99-
self.get_buffer_memory_size()
100-
}
101-
}
102-
103-
// =============================================================================
104-
// List Arrays
105-
// =============================================================================
106-
107-
impl HeapSize for ListArray {
108-
fn heap_size(&self) -> usize {
109-
self.get_buffer_memory_size()
110-
}
111-
}
112-
113-
impl HeapSize for LargeListArray {
114-
fn heap_size(&self) -> usize {
115-
self.get_buffer_memory_size()
116-
}
117-
}
118-
119-
impl HeapSize for ListViewArray {
120-
fn heap_size(&self) -> usize {
121-
self.get_buffer_memory_size()
122-
}
123-
}
124-
125-
impl HeapSize for LargeListViewArray {
126-
fn heap_size(&self) -> usize {
127-
self.get_buffer_memory_size()
128-
}
129-
}
130-
131-
impl HeapSize for FixedSizeListArray {
132-
fn heap_size(&self) -> usize {
133-
self.get_buffer_memory_size()
134-
}
135-
}
136-
137-
// =============================================================================
138-
// Complex/Nested Arrays
139-
// =============================================================================
140-
141-
impl HeapSize for StructArray {
142-
fn heap_size(&self) -> usize {
143-
self.get_buffer_memory_size()
144-
}
145-
}
146-
147-
impl HeapSize for MapArray {
148-
fn heap_size(&self) -> usize {
149-
self.get_buffer_memory_size()
150-
}
151-
}
152-
153-
impl HeapSize for UnionArray {
154-
fn heap_size(&self) -> usize {
155-
self.get_buffer_memory_size()
156-
}
157-
}
158-
15977
impl<K: ArrowDictionaryKeyType> HeapSize for DictionaryArray<K> {
16078
fn heap_size(&self) -> usize {
16179
self.get_buffer_memory_size()

0 commit comments

Comments
 (0)