Skip to content

perf(arrow-buffer): use a static address for buffers without an allocation - #12141

Merged
mbutrovich merged 2 commits into
apache:mainfrom
azecevic000:arrow-buffer-zero-size-area
Dec 3, 2026
Merged

mbutrovich merged 2 commits into
apache:mainfrom
azecevic000:arrow-buffer-zero-size-area

Conversation

@azecevic000

@azecevic000 azecevic000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

#12269

Rationale for this change

Buffers without an allocation have a dangling pointer in the unmapped first page. On x86-64 CPUs with AVX512 and glibc >=2.35 , the memcmp and bcmp of length 0 that Rust uses to compare empty slices take up to about 100 ns at such an address, instead of 2 ns. So each compare of values of an all-empty string or binary array is slow. The CPU walks the page tables on every call, because the TLB does not cache unmapped pages. Then it cancels the page fault, because the mask covers it.

What changes are included in this PR?

All buffers without an allocation now point at ZERO_SIZE_AREA, a static aligned to 256 bytes (at least ALIGNMENT on all targets), as Arrow C++ does with zero_size_area:

  • dangling_ptr() returns its address. The cfg(miri) variant is no longer necessary, because the address now comes from a real static.
  • From<Vec<T>> for MutableBuffer (so also Buffer::from_vec) uses it for a Vec without an allocation.
  • MutableBuffer uses it after it shrinks to size 0. Before, it kept the pointer to the freed memory.

Empty buffers already shared addresses before (0x80, or 0x1 for Vec<u8>), so code cannot rely on a unique address for them.

Are these changes tested?

test_buffers_without_allocation_point_to_zero_size_area checks the three places. The existing tests pass.

Intel Xeon Platinum 8375C (AVX512VL and AVX512BW), glibc 2.35 or later, one core, 1,048,576 empty strings.

arrow_writer benchmark, string_empty from #12140, criterion median per iteration:

benchmark main this PR change
string_empty/default 205.6 ms 15.0 ms -92.7%
string_empty/bloom_filter 205.9 ms 15.4 ms -92.5%
string_empty/parquet_2 205.9 ms 15.1 ms -92.7%
string_empty/zstd 205.7 ms 15.1 ms -92.6%
string_empty/zstd_parquet_2 205.4 ms 15.2 ms -92.6%
string_empty/cdc 208.3 ms 17.4 ms -91.6%
string_empty/number_distinct_values 226.4 ms 36.2 ms -84.0%

With this PR, string_empty/default is as fast as the same values in an allocated buffer (15.04 ms and 15.05 ms).

No regressions: the existing string, string_non_null and string_dictionary cases (21 benchmarks) show no change (-3.8% to +3.5%, all within criterion's noise). In buffer_create, the changes are -1.7% to +2.8%; a control run of the unchanged code moved the same three bool cases by +1.2% to +1.8%.

Are there any significant user-facing changes?

No. Only the address of empty buffers changes.

AI use

An AI coding agent wrote the code. I have reviewed it and verified the benchmark results.

@boring-cyborg boring-cyborg Bot added arrow-core Core crates related to representing arrow data parquet parquet crate labels Nov 9, 2026
@etseidl

etseidl commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Thanks @azecevic000, this looks nice. Could you please open an issue for this and link this PR and #12140 to it? 🙏

@etseidl etseidl added the performance Speed or memory optimizations label Nov 9, 2026
@jhorstmann

Copy link
Copy Markdown
Contributor

Does this issue also exist in the rust standard lib when comparing empty vectors or strings?

@azecevic000

Copy link
Copy Markdown
Contributor Author

Does this issue also exist in the rust standard lib when comparing empty vectors or strings?

Yes, exactly the same issue.

Thanks @azecevic000, this looks nice. Could you please open an issue for this and link this PR and #12140 to it? 🙏

Thank you, I've opened it and linked it.

…ation

Buffers without an allocation used a dangling pointer in the unmapped
first page: `ALIGNMENT` from `MutableBuffer::new(0)`, or the pointer of
an empty `Vec` (`0x1` for `Vec<u8>`). Rust compares empty slices with
`memcmp` and `bcmp` of length 0, and on x86-64 CPUs with AVX512VL and
AVX512BW, glibc 2.35 and later make these calls slow at an unmapped
address: up to about 100 ns instead of 2 ns. So each compare of values
of an all-empty string or binary array was slow, for example in Parquet
statistics and in the comparison and sort kernels.

All such buffers now point at one static, aligned area, as Arrow C++
does (`zero_size_area`): `dangling_ptr()`, a `Vec` without an
allocation in `From<Vec<T>> for MutableBuffer`, and `MutableBuffer`
after shrinking to size 0, which kept the pointer to the freed memory.
@azecevic000
azecevic000 force-pushed the arrow-buffer-zero-size-area branch from e1f831a to 6239496 Compare November 16, 2026 16:54
@azecevic000
azecevic000 marked this pull request as ready for review November 16, 2026 17:07
@azecevic000

Copy link
Copy Markdown
Contributor Author

@etseidl Kindly nudge, do you have a time to review this PR?

@mbutrovich
mbutrovich self-requested a review December 3, 2026 18:29

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @azecevic000, this is a nice find, and the fix is small. I ran the arrow-buffer unit tests under Miri at the head commit, along with extra tests that write to, grow, and round-trip empty buffers through Vec. Miri reported no undefined behavior under Stacked Borrows or Tree Borrows.

Comment thread arrow-buffer/src/buffer/mutable.rs Resolved

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @azecevic000!

@mbutrovich
mbutrovich enabled auto-merge December 3, 2026 19:37
@mbutrovich
mbutrovich added this pull request to the merge queue Dec 3, 2026
Merged via the queue into apache:main with commit 7aa9312 Dec 3, 2026
21 of 22 checks passed
@jhorstmann

Copy link
Copy Markdown
Contributor

Does this issue also exist in the rust standard lib when comparing empty vectors or strings?

Yes, exactly the same issue.

@azecevic000 do you plan to, or did you already open an issue in the rust repository? I could reproduce it now also with just empty Vec, so might be worth reporting.

@azecevic000

Copy link
Copy Markdown
Contributor Author

@jhorstmann Thanks for suggestion, I opened an issue in Rust repo. Let's see, I can see a value in fix in both Rust and glibc, basically it's a chain of reliance on UB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data parquet parquet crate performance Speed or memory optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants