perf: use aligned slice access in SparkUnsafeArray bulk append#3659
Draft
andygrove wants to merge 3 commits intoapache:mainfrom
Draft
perf: use aligned slice access in SparkUnsafeArray bulk append#3659andygrove wants to merge 3 commits intoapache:mainfrom
andygrove wants to merge 3 commits intoapache:mainfrom
Conversation
SparkUnsafeArray guarantees natural alignment for element data: the header is 8 + ceil(n/64)*8 bytes (always 8-byte aligned), and elements are at element_size stride from the aligned base. This means we can create a slice upfront and use indexed access instead of per-element read_unaligned() with manual pointer arithmetic. Changes: - Nullable paths in impl_append_to_builder macro, append_booleans, append_timestamps, and append_dates now create a slice upfront and iterate with enumerate, eliminating read_unaligned and ptr.add(1) - Non-nullable paths simplified: removed runtime alignment checks since alignment is guaranteed, always use append_slice bulk copy - Added debug_assert for alignment invariant
SparkUnsafeArray elements may not be naturally aligned (e.g., i64 at 4-byte offset). Restore runtime alignment check: use slice-based access when aligned, fall back to per-element read_unaligned when not. The nullable path now has the same aligned fast path as non-nullable, which was the original goal of this PR.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
read_unaligned()+ manual pointer arithmetic with slice-based indexed access in all nullable bulk append paths (impl_append_to_buildermacro,append_booleans,append_timestamps,append_dates)append_slicebulk copydebug_assertto verify the alignment invariantRationale
SparkUnsafeArray guarantees natural alignment for element data: the header is
8 + ceil(n/64)*8bytes (always 8-byte aligned), and elements are atelement_sizestride from the aligned base. The nullable paths were previously doing per-elementptr.read_unaligned()withptr = ptr.add(1), while the non-nullable paths had runtime alignment checks with fallback to unaligned reads. Since alignment is guaranteed, all paths can use slice access, which gives better codegen (no manual pointer arithmetic, compiler can reason about bounds).Test plan
cargo clippy --all-targets --workspace -- -D warningspasses