Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ members = [
"arrow-integration-testing",
"arrow-ipc",
"arrow-json",
"arrow-memory-size",
"arrow-memory-size-derive",
"arrow-ord",
"arrow-pyarrow",
"arrow-row",
Expand Down Expand Up @@ -94,6 +96,8 @@ arrow-csv = { version = "57.2.0", path = "./arrow-csv" }
arrow-data = { version = "57.2.0", path = "./arrow-data" }
arrow-ipc = { version = "57.2.0", path = "./arrow-ipc" }
arrow-json = { version = "57.2.0", path = "./arrow-json" }
arrow-memory-size = { version = "57.2.0", path = "./arrow-memory-size" }
Copy link
Contributor

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-buffer or something

Copy link
Contributor Author

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

arrow-memory-size-derive = { version = "57.2.0", path = "./arrow-memory-size-derive" }
arrow-ord = { version = "57.2.0", path = "./arrow-ord" }
arrow-pyarrow = { version = "57.2.0", path = "./arrow-pyarrow" }
arrow-row = { version = "57.2.0", path = "./arrow-row" }
Expand Down
1 change: 1 addition & 0 deletions arrow-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ahash = { version = "0.8", default-features = false, features = ["runtime-rng"]

[dependencies]
arrow-buffer = { workspace = true }
arrow-memory-size = { workspace = true }
arrow-schema = { workspace = true }
arrow-data = { workspace = true }
chrono = { workspace = true }
Expand Down
143 changes: 143 additions & 0 deletions arrow-array/src/heap_size.rs
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());
}
}
2 changes: 2 additions & 0 deletions arrow-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ pub mod timezone;
mod trusted_len;
pub mod types;

mod heap_size;

#[cfg(test)]
mod tests {
use crate::builder::*;
Expand Down
1 change: 1 addition & 0 deletions arrow-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ all-features = true
pool = []

[dependencies]
arrow-memory-size = { workspace = true }
bytes = { version = "1.4" }
num-bigint = { version = "0.4.6", default-features = false, features = ["std"] }
num-traits = { version = "0.2.19", default-features = false, features = ["std"] }
Expand Down
81 changes: 81 additions & 0 deletions arrow-buffer/src/heap_size.rs
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);
}
}
2 changes: 2 additions & 0 deletions arrow-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub use interval::*;

mod arith;

mod heap_size;

#[cfg(feature = "pool")]
mod pool;
#[cfg(feature = "pool")]
Expand Down
43 changes: 43 additions & 0 deletions arrow-memory-size-derive/Cargo.toml
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 }
1 change: 1 addition & 0 deletions arrow-memory-size-derive/LICENSE.txt
1 change: 1 addition & 0 deletions arrow-memory-size-derive/NOTICE.txt
Loading
Loading