Commit 631c918
authored
perf: sort replace free()->try_grow() pattern with try_resize() to reduce memory pool interactions (#20729)
## 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 #20728.
## 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.
-->
See full discussion in #20728, but the tl;dr is:
> Commit
3f0b342
("Improve sort memory resilience",
#19494) added per-chunk
reservation.try_grow(total_sorted_size) calls inside the sort's unfold
closure (sort.rs:748-750). I believe this causes a performance
regression for projects that implement custom MemoryPool backends where
try_grow is not trivially cheap.
## 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 the pattern of `free()`->`try_grow()` with a `try_resize()` that
in the common case doesn't do much work since a sorted output batch is
likely the same size as the input batch.
## 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)?
-->
Existing tests, and we are running some longer benchmarks with Comet at
the moment.
When I profile TPC-H Q21 SF100 locally I see the giant stack of memory
allocations that occur on DF52...
<img width="639" height="197" alt="Screenshot 2026-03-05 at 2 23 15 PM"
src="https://github.com/user-attachments/assets/237c7099-0bf0-48f5-a994-8959d3427377"
/>
...completely disappear...
<img width="546" height="122" alt="Screenshot 2026-03-05 at 2 24 31 PM"
src="https://github.com/user-attachments/assets/550cddc1-fcde-486e-88d2-2156eab661b9"
/>
... and TPC-H Q21 looks back to DF51 performance, at least locally.
## 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.
-->
No.1 parent 13cebf8 commit 631c918
1 file changed
Lines changed: 20 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
731 | 731 | | |
732 | 732 | | |
733 | 733 | | |
734 | | - | |
735 | | - | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
736 | 745 | | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
749 | | - | |
750 | | - | |
751 | | - | |
752 | | - | |
753 | | - | |
754 | | - | |
755 | | - | |
756 | | - | |
757 | | - | |
758 | | - | |
759 | | - | |
760 | | - | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
765 | 755 | | |
766 | 756 | | |
767 | 757 | | |
| |||
0 commit comments