Skip to content

Perf: optimize actual_buffer_size to use only data buffer capacity for coalesce #7967

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

Merged
merged 5 commits into from
Jul 23, 2025
Merged
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
42 changes: 22 additions & 20 deletions arrow-select/src/coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,21 +785,27 @@ mod tests {

#[test]
fn test_string_view_many_small_compact() {
// The strings are 28 long, so each batch has 400 * 28 = 5600 bytes
// 200 rows alternating long (28) and short (≤12) strings.
// Only the 100 long strings go into data buffers: 100 × 28 = 2800.
let batch = stringview_batch_repeated(
400,
200,
[Some("This string is 28 bytes long"), Some("small string")],
);
let output_batches = Test::new()
// First allocated buffer is 8kb.
// Appending five batches of 5600 bytes will use 5600 * 5 = 28kb (8kb, an 16kb and 32kbkb)
// Appending 10 batches of 2800 bytes will use 2800 * 10 = 14kb (8kb, an 16kb and 32kbkb)
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch(batch.clone())
.with_batch_size(8000)
.with_expected_output_sizes(vec![2000]) // only 2000 rows total
.with_expected_output_sizes(vec![2000]) // only 1000 rows total
.run();

// expect a nice even distribution of buffers
Expand Down Expand Up @@ -854,22 +860,27 @@ mod tests {

#[test]
fn test_string_view_large_small() {
// The strings are 37 bytes long, so each batch has 200 * 28 = 5600 bytes
// The strings are 37 bytes long, so each batch has 100 * 28 = 2800 bytes
let mixed_batch = stringview_batch_repeated(
400,
200,
[Some("This string is 28 bytes long"), Some("small string")],
);
// These strings aren't copied, this array has an 8k buffer
let all_large = stringview_batch_repeated(
100,
50,
[Some(
"This buffer has only large strings in it so there are no buffer copies",
)],
);

let output_batches = Test::new()
// First allocated buffer is 8kb.
// Appending five batches of 5600 bytes will use 5600 * 5 = 28kb (8kb, an 16kb and 32kbkb)
// Appending five batches of 2800 bytes will use 2800 * 10 = 28kb (8kb, an 16kb and 32kbkb)
.with_batch(mixed_batch.clone())
.with_batch(mixed_batch.clone())
.with_batch(all_large.clone())
.with_batch(mixed_batch.clone())
.with_batch(all_large.clone())
.with_batch(mixed_batch.clone())
.with_batch(mixed_batch.clone())
.with_batch(all_large.clone())
Expand All @@ -883,26 +894,17 @@ mod tests {
col_as_string_view("c0", output_batches.first().unwrap()),
vec![
ExpectedLayout {
len: 8176,
len: 8190,
capacity: 8192,
},
// this buffer was allocated but not used when the all_large batch was pushed
ExpectedLayout {
len: 3024,
len: 16366,
capacity: 16384,
},
ExpectedLayout {
len: 7000,
capacity: 8192,
},
ExpectedLayout {
len: 5600,
len: 6244,
capacity: 32768,
},
ExpectedLayout {
len: 7000,
capacity: 8192,
},
],
);
}
Expand Down
5 changes: 4 additions & 1 deletion arrow-select/src/coalesce/byte_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ impl<B: ByteViewType> InProgressArray for InProgressByteViewArray<B> {
(false, 0)
} else {
let ideal_buffer_size = s.total_buffer_bytes_used();
let actual_buffer_size = s.get_buffer_memory_size();
// We don't use get_buffer_memory_size here, because gc is for the contents of the
// data buffers, not views and nulls.
let actual_buffer_size =
s.data_buffers().iter().map(|b| b.capacity()).sum::<usize>();
// copying strings is expensive, so only do it if the array is
// sparse (uses at least 2x the memory it needs)
let need_gc =
Expand Down
Loading