viirya opened a new pull request, #19555:
URL: https://github.com/apache/datafusion/pull/19555

   ## 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. For example `Closes #123` 
indicates that this PR will close issue #123.
   -->
   
   - Closes #.
   
   ## Rationale for this change
   
   <!--
    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?
   
   <!--
   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.
   -->
   
   Replace per-row Vec<char> allocation with a reusable buffer in trim 
functions (ltrim, rtrim, btrim).
   
   The previous implementation allocated a Vec<char> for the pattern on every 
row, which was inefficient. This optimization introduces a pattern_buf that is 
allocated once and reused across all rows by clearing and refilling it.
   
   Changes:
   - Refactored general_trim to pass TrimType directly instead of closures
   - Created apply_trim helper function that accepts a mutable pattern buffer
   - Updated string_view_trim and string_trim to allocate pattern_buf once
   - Buffer is cleared and reused for each row to avoid repeated allocations
   
   Benchmark results for ltrim (size=1024):
   - INPUT LEN <= 12, string_view: 21.484 µs -> 13.243 µs (38.4% faster, 1.6x 
speedup)
   - INPUT LEN <= 12, string: 21.540 µs -> 14.051 µs (34.8% faster, 1.5x 
speedup)
   - INPUT LEN > 12, OUTPUT LEN > 12, string_view: 21.951 µs -> 13.325 µs 
(39.3% faster, 1.6x speedup)
   - INPUT LEN > 12, OUTPUT LEN > 12, string: 24.328 µs -> 16.844 µs (30.8% 
faster, 1.4x speedup)
   - INPUT LEN > 12, OUTPUT LEN <= 12, string_view: 87.967 µs -> 77.016 µs 
(12.4% faster)
   
   Benchmark results for ltrim (size=4096):
   - INPUT LEN <= 12, string_view: 85.626 µs -> 51.478 µs (39.9% faster, 1.7x 
speedup)
   - INPUT LEN <= 12, string: 84.011 µs -> 54.774 µs (34.8% faster, 1.5x 
speedup)
   - INPUT LEN > 12, OUTPUT LEN > 12, string_view: 85.964 µs -> 51.825 µs 
(39.7% faster, 1.7x speedup)
   - INPUT LEN > 12, OUTPUT LEN > 12, string: 102.42 µs -> 74.097 µs (27.7% 
faster, 1.4x speedup)
   
   The optimization shows consistent 28-40% improvement across most workloads 
by eliminating per-row Vec allocations. This applies to all trim variants 
(ltrim, rtrim, btrim) as they share the same underlying implementation.
   
   ## Are these changes tested?
   
   <!--
   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)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   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 add the `api 
change` label.
   -->
   


-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to