sort: bound collation-key precomputation per line #12144
<!-- gnu-comment-bot -->
GNU testsuite comparison:
Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!
<!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->
Merging this PR will degrade performance by 13.11%
<details>
<summary><b>1 warning, 1 note</b></summary>
:warning: 29 benchmarks are dominated by system calls
> System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.
> Learn more about system calls
> See affected benchmarks
:information_source: 1 benchmark has no flamegraph
> The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.
> See affected benchmarks
</details>
❌ 1 regressed benchmark ✅ 57 untouched benchmarks ⏩ 426 skipped benchmarks[^skipped]
[!WARNING]
Please fix the performance issues or acknowledge them on CodSpeed.
Performance Changes
| | Mode | Benchmark | BASE | HEAD | Efficiency |
| --- | ---- | --------- | ------ | ------ | ---------- |
| ❌ | CPU Simulation | ` sort_german_de_locale | 285.7 ms | 328.9 ms | -13.11% |
[!TIP]
Investigate this regression by commenting @codspeedbot fix this regression` on this PR, or directly use the CodSpeed MCP with your agent.
---
<sub>Comparing <code>mattsu2020:fix_sort_performance</code> (71f605c) with <code>main</code> (293a724)[^unexpected-base]</sub>
<a href="https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Afix_sort_performance?utm_source=github&utm_medium=comment-v2&utm_content=button">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
<img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
</picture>
</a>
[^skipped]: 426 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.
[^unexpected-base]: No successful run was found on <code>main</code> (2cb55db) during the generation of this report, so 293a724 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.
Out of interest, why choose 1 MiB as the limit, rather than something lower like u16::MAX?
Out of interest, why choose 1 MiB as the limit, rather than something lower like u16::MAX?
Since measurements using 64 KiB showed performance that was at least equivalent for the issue workload, we will change the threshold to u16::MAX.
@mattsu2020 Could you also add a benchmark (in separate PR)?
@mattsu2020 Could you also add a benchmark (in separate PR)?
Sure, I’ll keep this PR focused on the fix and open a separate PR adding a benchmark for long-line locale collation.
Codspeed is not happy
Codspeed is not happy
The memory usage is within expectations, but I'll look into the CPU.
seems unrelated
seems unrelated
16 MiB + 16 MiB of input in a debug build, is this one really needed on top of the unit test that already checks the limit?
the high-bit tagging is a bit too clever, a separate Vec<bool> or an Option would be easier to read, no?
seems unrelated to the limit, please move it to another pr
thanks for the PR!
The benchmark rewrite and the workflow change are still in: please move them to a separate PR, as agreed with xtqqczze, and keep this one to the per-line limit.
CodSpeed is red too, please have a look.
Please also squash the commits into one.
seems unrelated
fixed it
fixed it
fixed it
fixed it
fixed it
fixed it
you said to xtqqczze you would switch to u16::MAX, why 16 MiB now?
there is no shared budget anymore, is this loop over ~240 lines of 70k still needed?
this computes a 16 MiB ICU key in a debug build, same concern as for the integration test. could you please make the limit smaller under cfg(test) or drop this case?
almost the same as push_collation_key_end, could be one function taking a bool, no?
During performance testing with u16::MAX, the entire line was rescanned.
fixed it
it's unnecessary. I've removed it.
Fixed it
<!-- size-comment-bot -->
Binary size comparison:
Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).
Total size of compared binaries: 155.49 MB (+104 KB, +0.07%)
Significant per-binary changes:
env 1.20 MB -> 1.28 MB (+80 KB, +6.51%)
sylvestre
mattsu2020
In UTF-8 locales, generating ICU collation keys for enormous lines can dominate sorting time. Precompute keys only for lines up to 16 MiB of input and compare larger lines lazily. Apply the limit per line so a chunk with many moderately long lines keeps reusable keys.
Closes #12138.