Skip to content

sort: avoid scratch allocation for stable sorts on 32-bit - #16906

Open
ChrisX101010 wants to merge 4 commits into
uutils:mainfrom
ChrisX101010:fix/16850-stable-sort-scratch
Open

ChrisX101010 wants to merge 4 commits into
uutils:mainfrom
ChrisX101010:fix/16850-stable-sort-scratch

Conversation

@ChrisX101010

Copy link
Copy Markdown
Contributor

On 32-bit Linux, sort -u can run out of memory when allocating temporary scratch space for stable sorting under tight address-space limits. This change preserves the existing relative order of equal keys using par_sort_unstable_by with an index tie-breaker, avoiding the extra chunk-sized allocation.

Refs #16850. Split out from #16901 as requested in review.

Comment thread src/uu/sort/src/sort.rs Outdated
// rayon's thread pool which triggers an unreachable trap.
if settings.stable || settings.unique {
#[cfg(not(target_os = "wasi"))]
#[cfg(all(not(target_os = "wasi"), target_pointer_width = "32"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only 32-bit? the index tie-break would work everywhere and drop a cfg branch

#[test]
#[cfg(all(target_os = "linux", target_pointer_width = "32"))]
fn test_unique_sort_under_address_space_limit() {
const MEMORY_LIMIT: u64 = 96 * 1024 * 1024;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this test fail without the fix? please document where 96M / 36*32k come from

@codspeed

codspeed Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 1 benchmark

1 warning, 1 note

⚠️ 33 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

ℹ️ 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

⚡ 2 improved benchmarks
❌ 1 regressed benchmark
✅ 75 untouched benchmarks
⏩ 408 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation sort_unique_utf8_locale 85.4 ms 88.1 ms -3.1%
⚡ Memory sort_unique_locale[500000] 32.1 MB 20.6 MB +55.55%
⚡ Memory sort_unique_utf8_locale 3.5 MB 2.3 MB +49.33%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ChrisX101010:fix/16850-stable-sort-scratch (812bb68) with main (ab69b86)

Open in CodSpeed

Footnotes

  1. 408 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. ↩

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/env is no longer failing!

@ChrisX101010

ChrisX101010 commented Nov 27, 2026 via email

Copy link
Copy Markdown
Contributor Author

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants