This is an automated email from the ASF dual-hosted git repository.
Jefffrey pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new f3c5dc5bab Perf: Introduce zero copy path when tonic returns an
aligned buffer (#10273)
f3c5dc5bab is described below
commit f3c5dc5babb648281f02cbe0460c5af1ffda1f80
Author: RIchard Baah <[email protected]>
AuthorDate: Mon Jul 6 21:59:33 2026 -0400
Perf: Introduce zero copy path when tonic returns an aligned buffer (#10273)
# 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.
-->
- works towards https://github.com/apache/arrow-rs/issues/10125
# Rationale for this change
see
https://github.com/apache/arrow-rs/pull/10206#issuecomment-4785443780
If the buffer Tonic returns happens to be 64-bit aligned ([as the Arrow
spec
requires](https://arrow.apache.org/docs/format/Columnar.html#buffer-alignment-and-padding)),
we can wrap it directly via `Buffer::from(Bytes)`, a zero-copy path that
just increments a reference count. If not, `Buffer::from(&[u8])` copies
into a fresh aligned allocation. The check is cheap (a single pointer
modulo), and can save a potentially very large buffer copy.
<!--
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?
this PR adds a pointer check for the data body. if the buffer is aligned
to a 64 bit address no copy happens, otherwise copy the bytes as usual.
<!--
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?
existing test cover this. if the buffer isn't aligned we fallback to
copying it.
<!--
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?
no
<!--
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.
-->
---
arrow-flight/src/decode.rs | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/arrow-flight/src/decode.rs b/arrow-flight/src/decode.rs
index 1f64924932..756603b844 100644
--- a/arrow-flight/src/decode.rs
+++ b/arrow-flight/src/decode.rs
@@ -336,8 +336,17 @@ impl FlightDataDecoder {
"Unable to convert flight data header to a record
batch".to_string(),
)
})?;
+ let buffer = {
+ // see
https://arrow.apache.org/docs/format/Columnar.html#buffer-alignment-and-padding
+ // reuse the allocation if already aligned, otherwise copy
into a fresh aligned buffer.
+ if data.data_body.as_ptr() as usize % 64 == 0 {
+ &Buffer::from(data.data_body.clone())
+ } else {
+ &Buffer::from(data.data_body.as_ref())
+ }
+ };
let batch = arrow_ipc::reader::RecordBatchDecoder::try_new(
- &Buffer::from(data.data_body.as_ref()),
+ buffer,
record_batch,
Arc::clone(&state.schema),
&state.dictionaries_by_field,