-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a crate for HeapSize trait #9138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adriangb
wants to merge
10
commits into
apache:main
Choose a base branch
from
pydantic:heap-size-crate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2766721
Add arrow-memory-size crate with HeapSize trait
adriangb f0fb88a
Restructure HeapSize: move arrow impls to respective crates
adriangb 4d0e68d
Fix formatting in arrow-memory-size crates
adriangb 6f71be2
Fix formatting in arrow-array heap_size module
adriangb 1c2eb09
Fix formatting in parquet_derive_test
adriangb 41fa2bc
Update README tone: focus on why this crate exists
adriangb 4722a72
Move HeapSize derive tests to arrow-memory-size-derive
adriangb 23d2648
Remove broken intra-doc links in proc-macro crate
adriangb 7245a2a
Use macro to reduce HeapSize impl repetition
adriangb 54bf894
touch up
adriangb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! [`HeapSize`] implementations for arrow-array types | ||
|
|
||
| use arrow_memory_size::HeapSize; | ||
|
|
||
| use crate::Array; | ||
| use crate::types::{ArrowDictionaryKeyType, ArrowPrimitiveType, RunEndIndexType}; | ||
| use crate::{ | ||
| BinaryArray, BinaryViewArray, BooleanArray, DictionaryArray, FixedSizeBinaryArray, | ||
| FixedSizeListArray, LargeBinaryArray, LargeListArray, LargeListViewArray, LargeStringArray, | ||
| ListArray, ListViewArray, MapArray, NullArray, PrimitiveArray, RunArray, StringArray, | ||
| StringViewArray, StructArray, UnionArray, | ||
| }; | ||
|
|
||
| // Note: A blanket implementation `impl<T: Array> HeapSize for T` would be ideal, | ||
| // but is not possible due to Rust's orphan rules (E0210) since HeapSize is defined | ||
| // in a separate crate. | ||
| // | ||
| // Note: HeapSize cannot be implemented for ArrayRef (Arc<dyn Array>) here due to | ||
| // Rust's orphan rules. Use array.get_buffer_memory_size() directly instead. | ||
|
|
||
| /// Implements HeapSize for array types that delegate to get_buffer_memory_size() | ||
| macro_rules! impl_heap_size { | ||
| ($($ty:ty),*) => { | ||
| $( | ||
| impl HeapSize for $ty { | ||
| fn heap_size(&self) -> usize { | ||
| self.get_buffer_memory_size() | ||
| } | ||
| } | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| impl_heap_size!( | ||
| BooleanArray, | ||
| NullArray, | ||
| StringArray, | ||
| LargeStringArray, | ||
| BinaryArray, | ||
| LargeBinaryArray, | ||
| StringViewArray, | ||
| BinaryViewArray, | ||
| FixedSizeBinaryArray, | ||
| ListArray, | ||
| LargeListArray, | ||
| ListViewArray, | ||
| LargeListViewArray, | ||
| FixedSizeListArray, | ||
| StructArray, | ||
| MapArray, | ||
| UnionArray | ||
| ); | ||
|
|
||
| impl<T: ArrowPrimitiveType> HeapSize for PrimitiveArray<T> { | ||
| fn heap_size(&self) -> usize { | ||
| self.get_buffer_memory_size() | ||
| } | ||
| } | ||
|
|
||
| impl<K: ArrowDictionaryKeyType> HeapSize for DictionaryArray<K> { | ||
| fn heap_size(&self) -> usize { | ||
| self.get_buffer_memory_size() | ||
| } | ||
| } | ||
|
|
||
| impl<R: RunEndIndexType> HeapSize for RunArray<R> { | ||
| fn heap_size(&self) -> usize { | ||
| self.get_buffer_memory_size() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::Int32Array; | ||
|
|
||
| #[test] | ||
| fn test_primitive_array_heap_size() { | ||
| let array = Int32Array::from(vec![1, 2, 3, 4, 5]); | ||
| // HeapSize should mirror the Array memory size APIs | ||
| assert_eq!(array.heap_size(), array.get_buffer_memory_size()); | ||
| assert_eq!(array.total_size(), array.get_array_memory_size()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_string_array_heap_size() { | ||
| let array = StringArray::from(vec!["hello", "world"]); | ||
| // Buffer capacities depend on allocator alignment and may vary by platform | ||
| assert_eq!(array.heap_size(), array.get_buffer_memory_size()); | ||
| assert_eq!(array.total_size(), array.get_array_memory_size()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_boolean_array_heap_size() { | ||
| let array = BooleanArray::from(vec![true, false, true]); | ||
| // Packed bits make heap_size small, but struct size is platform-dependent | ||
| assert_eq!(array.heap_size(), array.get_buffer_memory_size()); | ||
| assert_eq!(array.total_size(), array.get_array_memory_size()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_null_array_heap_size() { | ||
| let array = NullArray::new(100); | ||
| // NullArray has no buffers; total size still depends on struct layout | ||
| assert_eq!(array.heap_size(), array.get_buffer_memory_size()); | ||
| assert_eq!(array.total_size(), array.get_array_memory_size()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_struct_array_heap_size() { | ||
| use crate::builder::StructBuilder; | ||
| use arrow_schema::{DataType, Field, Fields}; | ||
|
|
||
| let fields = Fields::from(vec![Field::new("a", DataType::Int32, false)]); | ||
| let mut builder = StructBuilder::from_fields(fields, 10); | ||
| builder | ||
| .field_builder::<crate::builder::Int32Builder>(0) | ||
| .unwrap() | ||
| .append_value(1); | ||
| builder.append(true); | ||
| let array = builder.finish(); | ||
| // StructArray aggregates child buffers; sizes depend on allocator and layout | ||
| assert_eq!(array.heap_size(), array.get_buffer_memory_size()); | ||
| assert_eq!(array.total_size(), array.get_array_memory_size()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! [`HeapSize`] implementations for arrow-buffer types | ||
|
|
||
| use arrow_memory_size::HeapSize; | ||
|
|
||
| use crate::{ArrowNativeType, BooleanBuffer, Buffer, NullBuffer, OffsetBuffer, ScalarBuffer}; | ||
|
|
||
| impl HeapSize for Buffer { | ||
| fn heap_size(&self) -> usize { | ||
| self.capacity() | ||
| } | ||
| } | ||
|
|
||
| impl<T: ArrowNativeType> HeapSize for ScalarBuffer<T> { | ||
| fn heap_size(&self) -> usize { | ||
| self.inner().capacity() | ||
| } | ||
| } | ||
|
|
||
| impl<T: ArrowNativeType> HeapSize for OffsetBuffer<T> { | ||
| fn heap_size(&self) -> usize { | ||
| self.inner().inner().capacity() | ||
| } | ||
| } | ||
|
|
||
| impl HeapSize for NullBuffer { | ||
| fn heap_size(&self) -> usize { | ||
| self.buffer().capacity() | ||
| } | ||
| } | ||
|
|
||
| impl HeapSize for BooleanBuffer { | ||
| fn heap_size(&self) -> usize { | ||
| self.inner().capacity() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_buffer_heap_size() { | ||
| let buf = Buffer::from(vec![1u8, 2, 3, 4, 5]); | ||
| assert!(buf.heap_size() >= 5); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_scalar_buffer_heap_size() { | ||
| let buf: ScalarBuffer<i32> = vec![1, 2, 3, 4, 5].into(); | ||
| assert!(buf.heap_size() >= 5 * std::mem::size_of::<i32>()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_null_buffer_heap_size() { | ||
| let buf = NullBuffer::new_null(100); | ||
| assert!(buf.heap_size() > 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_boolean_buffer_heap_size() { | ||
| let buf = BooleanBuffer::new_set(100); | ||
| assert!(buf.heap_size() > 0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| [package] | ||
| name = "arrow-memory-size-derive" | ||
| version = { workspace = true } | ||
| description = "Derive macro for HeapSize trait" | ||
| homepage = { workspace = true } | ||
| repository = { workspace = true } | ||
| authors = { workspace = true } | ||
| license = { workspace = true } | ||
| keywords = { workspace = true } | ||
| include = { workspace = true } | ||
| edition = { workspace = true } | ||
| rust-version = { workspace = true } | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [package.metadata.docs.rs] | ||
| all-features = true | ||
|
|
||
| [dependencies] | ||
| proc-macro2 = { version = "1.0", default-features = false } | ||
| quote = { version = "1.0", default-features = false } | ||
| syn = { version = "2.0", features = ["extra-traits"] } | ||
|
|
||
| [dev-dependencies] | ||
| arrow-memory-size = { workspace = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../LICENSE.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../NOTICE.txt |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are going to do this I would suggest we put it in
arrow-bufferor somethingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to just move it there instead of a new crate, certainly easier