Skip to content

Commit 81dce78

Browse files
authored
fix off by one error for slice accounting (#10406)
# Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> - Closes #10385. - works towards closing #10383 # Rationale for this change `get_slice_memory_size` is used to estimate how much memory a slice of an ArrayData actually occupies. For variable-width types (Utf8, Binary, List, etc.), the offset buffer stores len+1 boundaries, one per element plus a final entry marking the end of the last element. The old code calculated `len * byte_width`, missing that final boundary and consistently under-reporting the size of any array using an offset buffer. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? the FixedWidth buffer size is now calculated as (len + 1) * byte_width instead of len * byte_width. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> # Are these changes tested? Yes I added two test for string arrays and binary arrays. existing test still pass <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. --> # Are there any user-facing changes? users will have more accurate memory accounting <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. -->
1 parent cd17899 commit 81dce78

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

arrow-data/src/data.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,19 @@ impl ArrayData {
530530
for spec in layout.buffers.iter() {
531531
match spec {
532532
BufferSpec::FixedWidth { byte_width, .. } => {
533-
let buffer_size = self.len.checked_mul(*byte_width).ok_or_else(|| {
533+
// Offset buffers contain len+1 elements: one boundary per element
534+
// plus a final boundary marking the end of the last element.
535+
let len = match self.data_type {
536+
DataType::Utf8
537+
| DataType::LargeUtf8
538+
| DataType::Binary
539+
| DataType::LargeBinary
540+
| DataType::List(_)
541+
| DataType::LargeList(_)
542+
| DataType::Map(_, _) => self.len + 1,
543+
_ => self.len,
544+
};
545+
let buffer_size = len.checked_mul(*byte_width).ok_or_else(|| {
534546
ArrowError::ComputeError(
535547
"Integer overflow computing buffer size".to_string(),
536548
)
@@ -2616,6 +2628,44 @@ mod tests {
26162628
assert!(!string_data_slice.ptr_eq(&string_data))
26172629
}
26182630

2631+
#[test]
2632+
fn test_slice_memory_size_utf8_offset_buffer_len_plus_one() {
2633+
// 2-element array ["hello", "world"]: array len = 2, 10 bytes
2634+
let data_buffer = Buffer::from_slice_ref("helloworld".as_bytes());
2635+
// offsets need array_len+1 entries to mark the end of every string:
2636+
// [0, 5, 10] -> 3 i32s = 12 bytes
2637+
let offsets_buffer = Buffer::from_slice_ref([0_i32, 5_i32, 10_i32]);
2638+
let array = ArrayData::try_new(
2639+
DataType::Utf8,
2640+
2,
2641+
None,
2642+
0,
2643+
vec![offsets_buffer, data_buffer],
2644+
vec![],
2645+
)
2646+
.unwrap();
2647+
assert_eq!(array.get_slice_memory_size().unwrap(), 22); // 12 + 10
2648+
}
2649+
2650+
#[test]
2651+
fn test_slice_memory_size_binary_offset_buffer_len_plus_one() {
2652+
// 2-element array: array len = 2, not 3
2653+
// values: 5 bytes
2654+
let data_buffer = Buffer::from_slice_ref([0u8, 1, 2, 3, 4]);
2655+
// offsets need array_len+1 entries to mark the end of every element:
2656+
let offsets_buffer = Buffer::from_slice_ref([0_i32, 2_i32, 5_i32]);
2657+
let array = ArrayData::try_new(
2658+
DataType::Binary,
2659+
2,
2660+
None,
2661+
0,
2662+
vec![offsets_buffer, data_buffer],
2663+
vec![],
2664+
)
2665+
.unwrap();
2666+
assert_eq!(array.get_slice_memory_size().unwrap(), 17); // 12 + 5
2667+
}
2668+
26192669
#[test]
26202670
fn test_slice_memory_size() {
26212671
let mut bit_v: [u8; 2] = [0; 2];

0 commit comments

Comments
 (0)