Rich-T-kid opened a new pull request, #10262:
URL: https://github.com/apache/arrow-rs/pull/10262

   # 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.
   -->
   
   - last chunk of [[10125] [encode path] Minor optimizations to 
arrow-flight](https://github.com/apache/arrow-rs/pull/10137)
   - closes #10125 
   
   
   # Rationale for this change
   **TLDR**: Its useful to pre-allocate vectors when you know the amount of 
data it will require 
   
   When `IpcDataGenerator` uses the `IpcBodySink::Write` variant, record batch 
buffer bytes are written directly into a `Vec`. If that `Vec` is undersized, it 
repeatedly reallocates and copies bytes into a larger buffer, growing 
exponentially (1, 4, 16, 32 ... KB ... MB) and paying **two** costs on each 
reallocation: 
   1. an OS memory request and 
   2. a full copy of existing bytes into the new buffer. 
   
   For large batches this cascade is expensive, and paying it fresh on every 
record batch chunk compounds the problem further. Since 
`FlightDataEncoder::split_batch_for_grpc_response` splits record batches into 
roughly equal-sized chunks, we exploit this by using the previous buffer's 
final capacity as an estimate for the next call, keeping a correctly-sized 
`Vec` alive across iterations and avoiding repeated reallocation on the hot 
path. 
   
   ### why not pre-allocate the buffers using an estimate with the length 
`split_batch_for_grpc_response` uses?
   
   Using the final capacity rather than the uncompressed dictionary size is 
intentional, since IPC encoding and compression both affect the actual bytes 
written, the final capacity naturally adapts to whatever encoding and 
compression settings are in effect rather than consistently overprovisioning.
   
   <!--
   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?
   
   - Move the scratch buffer out of ipc_write_context via mem::take (zero copy)
   - Write the IPC bytes into the buffer
   - Record the final capacity
   - Pre-allocate a fresh scratch buffer at that capacity for the next call
   
   <!--
   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?
   n/a
   <!--
   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.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to