Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2749,7 +2749,8 @@ fn sort_by<'a>(unsorted: &mut Vec<Line<'a>>, settings: &GlobalSettings, line_dat
// rayon's thread pool which triggers an unreachable trap.
if settings.stable || settings.unique {
#[cfg(not(target_os = "wasi"))]
unsorted.par_sort_by(cmp);
// Retain stable ordering without a second allocation proportional to the chunk.
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 {
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,31 @@ 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() {
// 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;

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

const INPUT_SIZE: usize = 36 * 1024 * 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"])
.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");
}

#[test]
fn test_ignore_case_unique_orders_punctuation_after_letters() {
new_ucmd!()
Expand Down
Loading