From 9f2501b1f9bba06727773cb1f8c54b525956d354 Mon Sep 17 00:00:00 2001 From: ChrisX001001 Date: Fri, 27 Nov 2026 14:53:31 +0100 Subject: [PATCH 1/6] sort: avoid scratch allocation for stable sorts on 32-bit --- src/uu/sort/src/sort.rs | 5 ++++- tests/by-util/test_sort.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index cec38b32a24..9b9f8ea7188 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -2748,7 +2748,10 @@ fn sort_by<'a>(unsorted: &mut Vec>, settings: &GlobalSettings, line_dat // WASI does not support threads, so use non-parallel sort to avoid // 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"))] + // Retain stable ordering without a second allocation proportional to the chunk. + unsorted.par_sort_unstable_by(|a, b| cmp(a, b).then(a.index.cmp(&b.index))); + #[cfg(all(not(target_os = "wasi"), not(target_pointer_width = "32")))] unsorted.par_sort_by(cmp); #[cfg(target_os = "wasi")] unsorted.sort_by(cmp); diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 7f63dcec076..8ef4c44a113 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -100,6 +100,24 @@ fn test_buffer_size_limits_both_chunks() { } } +#[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; + const PAIRS: usize = 36 * 32 * 1024; + let input = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n".repeat(PAIRS); + new_ucmd!() + .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) + .env("LC_ALL", "C") + .env("MALLOC_ARENA_MAX", "1") + .limit(Resource::AS, MEMORY_LIMIT, MEMORY_LIMIT) + .limit(Resource::CORE, 0, 0) + .timeout(Duration::from_secs(120)) + .pipe_in(input) + .succeeds() + .stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); +} + #[test] fn test_buffer_size_trailing_whitespace_is_rejected() { for size in [" 1 ", "1 ", "1M ", "50% "] { From 427898c8702afe272899414226df02a30482769d Mon Sep 17 00:00:00 2001 From: ChrisX001001 Date: Fri, 27 Nov 2026 14:55:10 +0100 Subject: [PATCH 2/6] tests/sort: group the stable allocation case with unique sorts --- tests/by-util/test_sort.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 8ef4c44a113..2ab2555c720 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -100,24 +100,6 @@ fn test_buffer_size_limits_both_chunks() { } } -#[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; - const PAIRS: usize = 36 * 32 * 1024; - let input = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n".repeat(PAIRS); - new_ucmd!() - .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) - .env("LC_ALL", "C") - .env("MALLOC_ARENA_MAX", "1") - .limit(Resource::AS, MEMORY_LIMIT, MEMORY_LIMIT) - .limit(Resource::CORE, 0, 0) - .timeout(Duration::from_secs(120)) - .pipe_in(input) - .succeeds() - .stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); -} - #[test] fn test_buffer_size_trailing_whitespace_is_rejected() { for size in [" 1 ", "1 ", "1M ", "50% "] { @@ -361,6 +343,24 @@ fn test_ignore_case_orders_punctuation_after_letters() { .stdout_is("A\na\n_\n"); } +#[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; + const PAIRS: usize = 36 * 32 * 1024; + let input = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n".repeat(PAIRS); + new_ucmd!() + .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) + .env("LC_ALL", "C") + .env("MALLOC_ARENA_MAX", "1") + .limit(Resource::AS, MEMORY_LIMIT, MEMORY_LIMIT) + .limit(Resource::CORE, 0, 0) + .timeout(Duration::from_secs(120)) + .pipe_in(input) + .succeeds() + .stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); +} + #[test] fn test_ignore_case_unique_orders_punctuation_after_letters() { new_ucmd!() From c3a385e22aef914213c5ea50d974dd373f9e1b46 Mon Sep 17 00:00:00 2001 From: ChrisX001001 Date: Fri, 27 Nov 2026 15:24:29 +0100 Subject: [PATCH 3/6] sort: preserve equal-key order without chunk scratch allocation --- src/uu/sort/src/sort.rs | 6 ++---- tests/by-util/test_sort.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 9b9f8ea7188..fef6970f6e1 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -2748,11 +2748,9 @@ fn sort_by<'a>(unsorted: &mut Vec>, settings: &GlobalSettings, line_dat // WASI does not support threads, so use non-parallel sort to avoid // rayon's thread pool which triggers an unreachable trap. if settings.stable || settings.unique { - #[cfg(all(not(target_os = "wasi"), target_pointer_width = "32"))] + #[cfg(not(target_os = "wasi"))] // Retain stable ordering without a second allocation proportional to the chunk. - unsorted.par_sort_unstable_by(|a, b| cmp(a, b).then(a.index.cmp(&b.index))); - #[cfg(all(not(target_os = "wasi"), not(target_pointer_width = "32")))] - unsorted.par_sort_by(cmp); + unsorted.par_sort_unstable_by(|a, b| cmp(a, b).then_with(|| a.index.cmp(&b.index))); #[cfg(target_os = "wasi")] unsorted.sort_by(cmp); } else { diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 2ab2555c720..54fcd885a39 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -346,9 +346,13 @@ fn test_ignore_case_orders_punctuation_after_letters() { #[test] #[cfg(all(target_os = "linux", target_pointer_width = "32"))] fn test_unique_sort_under_address_space_limit() { + // On i686, 36 MiB of these short records fits for an ordinary sort + // under a 96 MiB address-space limit. The former stable sorter aborted + // here trying to allocate about 24 MiB of scratch space. const MEMORY_LIMIT: u64 = 96 * 1024 * 1024; - const PAIRS: usize = 36 * 32 * 1024; - let input = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n".repeat(PAIRS); + const INPUT_SIZE: usize = 36 * 1024 * 1024; + const RECORD_PAIR: &[u8] = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n"; + let input = RECORD_PAIR.repeat(INPUT_SIZE / RECORD_PAIR.len()); new_ucmd!() .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) .env("LC_ALL", "C") From 09ee1ed12f8eb0d1ea4a96123a9e602b8998d0b9 Mon Sep 17 00:00:00 2001 From: ChrisX001001 Date: Fri, 27 Nov 2026 20:57:40 +0100 Subject: [PATCH 4/6] sort: preserve diagnostics in the address-space regression test --- tests/by-util/test_sort.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 54fcd885a39..764ffe4ab79 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -352,15 +352,18 @@ fn test_unique_sort_under_address_space_limit() { const MEMORY_LIMIT: u64 = 96 * 1024 * 1024; const INPUT_SIZE: usize = 36 * 1024 * 1024; const RECORD_PAIR: &[u8] = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n"; - let input = RECORD_PAIR.repeat(INPUT_SIZE / RECORD_PAIR.len()); - new_ucmd!() - .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) + let (at, mut ucmd) = at_and_ucmd!(); + at.write_bytes("input", &RECORD_PAIR.repeat(INPUT_SIZE / RECORD_PAIR.len())); + + // A file-backed stdin lets the test report sort's exit status and stderr + // even if sort exits before consuming all of the input. + ucmd.args(&["--parallel=1", "--buffer-size=64M", "--unique"]) + .set_stdin(at.open("input")) .env("LC_ALL", "C") .env("MALLOC_ARENA_MAX", "1") .limit(Resource::AS, MEMORY_LIMIT, MEMORY_LIMIT) .limit(Resource::CORE, 0, 0) .timeout(Duration::from_secs(120)) - .pipe_in(input) .succeeds() .stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); } From 48dbfba384cb166306b9b5c32b7cbbc2ccbe36d7 Mon Sep 17 00:00:00 2001 From: Hristos Nenkov <143946165+ChrisX101010@users.noreply.github.com> Date: Sat, 28 Nov 2026 20:14:04 +0100 Subject: [PATCH 5/6] sort: keep the address-space fixture below buffer growth --- tests/by-util/test_sort.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 764ffe4ab79..d5f9a1a5259 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -346,26 +346,30 @@ fn test_ignore_case_orders_punctuation_after_letters() { #[test] #[cfg(all(target_os = "linux", target_pointer_width = "32"))] fn test_unique_sort_under_address_space_limit() { - // On i686, 36 MiB of these short records fits for an ordinary sort - // under a 96 MiB address-space limit. The former stable sorter aborted - // here trying to allocate about 24 MiB of scratch space. + // Leave room for the EOF read inside the reader's 36 MiB allocation. + // Filling that allocation would make the reader grow it before sorting. const MEMORY_LIMIT: u64 = 96 * 1024 * 1024; - const INPUT_SIZE: usize = 36 * 1024 * 1024; + const INPUT_SIZE: usize = 36 * 1024 * 1024 - 64 * 1024; const RECORD_PAIR: &[u8] = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n"; let (at, mut ucmd) = at_and_ucmd!(); at.write_bytes("input", &RECORD_PAIR.repeat(INPUT_SIZE / RECORD_PAIR.len())); - // A file-backed stdin lets the test report sort's exit status and stderr - // even if sort exits before consuming all of the input. - ucmd.args(&["--parallel=1", "--buffer-size=64M", "--unique"]) + let result = ucmd + .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) .set_stdin(at.open("input")) .env("LC_ALL", "C") .env("MALLOC_ARENA_MAX", "1") .limit(Resource::AS, MEMORY_LIMIT, MEMORY_LIMIT) .limit(Resource::CORE, 0, 0) .timeout(Duration::from_secs(120)) - .succeeds() - .stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); + .run(); + let status = result.exit_status(); + assert!( + status.success(), + "sort failed under the address-space limit: {status}\nstderr: {}", + result.stderr_str() + ); + result.stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); } #[test] From 4239f379f6b942135121653748d066d783d8f74f Mon Sep 17 00:00:00 2001 From: ChrisX101010 <143946165+ChrisX101010@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:54:48 +0100 Subject: [PATCH 6/6] sort: retain the 64-bit sort path and isolate the i686 regression --- src/uu/sort/src/sort.rs | 6 ++++-- tests/by-util/test_sort.rs | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index fef6970f6e1..25b16d34e17 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -2748,9 +2748,11 @@ fn sort_by<'a>(unsorted: &mut Vec>, settings: &GlobalSettings, line_dat // WASI does not support threads, so use non-parallel sort to avoid // rayon's thread pool which triggers an unreachable trap. if settings.stable || settings.unique { - #[cfg(not(target_os = "wasi"))] - // Retain stable ordering without a second allocation proportional to the chunk. + #[cfg(all(not(target_os = "wasi"), target_pointer_width = "32"))] + // Avoid a second chunk-sized allocation where address space is limited. unsorted.par_sort_unstable_by(|a, b| cmp(a, b).then_with(|| a.index.cmp(&b.index))); + #[cfg(all(not(target_os = "wasi"), not(target_pointer_width = "32")))] + unsorted.par_sort_by(cmp); #[cfg(target_os = "wasi")] unsorted.sort_by(cmp); } else { diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index d5f9a1a5259..4451b7938da 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -346,16 +346,18 @@ fn test_ignore_case_orders_punctuation_after_letters() { #[test] #[cfg(all(target_os = "linux", target_pointer_width = "32"))] fn test_unique_sort_under_address_space_limit() { - // Leave room for the EOF read inside the reader's 36 MiB allocation. - // Filling that allocation would make the reader grow it before sorting. - const MEMORY_LIMIT: u64 = 96 * 1024 * 1024; - const INPUT_SIZE: usize = 36 * 1024 * 1024 - 64 * 1024; - const RECORD_PAIR: &[u8] = b"zzzzzzzzzzzzzzz\naaaaaaaaaaaaaaa\n"; + // The reader halves the explicit 32 MiB budget. Keep the input below + // 16 MiB so reading EOF never requires a second chunk. + // Four-byte records need about 45 MiB of 32-bit line descriptors. + // The input and descriptors fit, but a second descriptor array does not. + const MEMORY_LIMIT: u64 = 104 * 1024 * 1024; + const INPUT_SIZE: usize = 15 * 1024 * 1024; + const RECORD_PAIR: &[u8] = b"zzz\naaa\n"; let (at, mut ucmd) = at_and_ucmd!(); at.write_bytes("input", &RECORD_PAIR.repeat(INPUT_SIZE / RECORD_PAIR.len())); let result = ucmd - .args(&["--parallel=1", "--buffer-size=64M", "--unique"]) + .args(&["--parallel=1", "--buffer-size=32M", "--unique"]) .set_stdin(at.open("input")) .env("LC_ALL", "C") .env("MALLOC_ARENA_MAX", "1") @@ -369,7 +371,7 @@ fn test_unique_sort_under_address_space_limit() { "sort failed under the address-space limit: {status}\nstderr: {}", result.stderr_str() ); - result.stdout_only("aaaaaaaaaaaaaaa\nzzzzzzzzzzzzzzz\n"); + result.stdout_only("aaa\nzzz\n"); } #[test]