Repository navigation
sort: cap automatic buffer size on 32-bit targets - #16901
ChrisX101010 wants to merge 7 commits into
Conversation
|
It seems this approach is same with rejected one (see #10290 (comment)). Maybe, repeat |
|
Thanks for the suggestion! I've updated the PR to dynamically scale down the buffer allocation on memory pressure instead of relying on a static cap, and verified it against short lines, numeric/key sorts, |
| // kick things off with two reads | ||
| for _ in 0..2 { | ||
| // Reserve space for one read-ahead block before the chunks compete for memory. | ||
| let mut carry_over = if cfg!(target_pointer_width = "32") { |
There was a problem hiding this comment.
Good catch, thanks! You're right—the cfg isn't strictly needed just to avoid a 64 KiB upfront allocation on 64-bit. I've removed it to keep the code simpler, and the tests passed on both 32-bit and 64-bit.
Merging this PR will degrade performance by 17.95%1 warning, 1 note
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
| // sorting scratch space. Leave more headroom on 32-bit targets, even when the | ||
| // machine reports plenty of physical memory. | ||
| #[cfg(target_pointer_width = "32")] | ||
| const MAX_AUTOMATIC_BUF_SIZE: usize = 32 * 1024 * 1024; // 32 MiB |
There was a problem hiding this comment.
you said the static cap was replaced by the retry, but it is still here?
please keep only one of the two
There was a problem hiding this comment.
Apologies for the confusion in my previous comment—I inadvertently left the 32 MiB static cap in place while adding the retry logic. Thanks for the thorough review; I'm updating the PR now to remove the static cap and target_pointer_width guards so the retry path runs on 64-bit CI too, clean up the chunks.rs logic, split the stable sort change into a separate PR, and add an address-space-limited integration test.
| if buffer.len() < carry_over.len() { | ||
| // Leave room for bookkeeping and temporary-file I/O while sizing this chunk. | ||
| let mut headroom = Vec::<u8>::new(); | ||
| if cfg!(target_pointer_width = "32") { |
There was a problem hiding this comment.
why only 32-bit? with cfg!(target_pointer_width = "32") everywhere, the retry path is never run on our CI
| let count = memchr_iter(separator, scanned_bytes).count(); | ||
| line_count += count; | ||
| if !reserve(line_count) { | ||
| return if reserved_end == 0 { |
There was a problem hiding this comment.
almost the same block 3 times, could be dedup, no?
| .try_reserve_exact(ALLOC_CHUNK_SIZE.saturating_sub(carry_over.len())) | ||
| .map_err(|_| out_of_memory())?; | ||
| headroom | ||
| .try_reserve_exact(1024 * 1024) |
There was a problem hiding this comment.
where does this 1 MiB come from? please use a named const
| && { | ||
| #[cfg(feature = "i18n-collator")] | ||
| if precomputed.fast_locale_collation { | ||
| return try_reserve(&mut self.collation_key_ends, count); |
There was a problem hiding this comment.
a return inside the && chain is hard to follow, could you please use plain ifs?
| }; | ||
| } | ||
| if count != 0 { | ||
| reserved_end = scanned + memrchr(separator, scanned_bytes).unwrap() + 1; |
There was a problem hiding this comment.
could use if let Some(pos) = memrchr(...) instead of the count check + unwrap
| #[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))); |
There was a problem hiding this comment.
this looks like a separate change, please split it into another pr
|
|
||
| #[cfg(target_pointer_width = "32")] | ||
| #[test] | ||
| fn automatic_memory_hint_leaves_room_for_line_metadata() { |
There was a problem hiding this comment.
please add a test in tests/by-util/test_sort.rs too (e.g. under a limited address space)
|
thanks for working on this! |
| let mut buffer = std::mem::take(&mut recycled_chunk.buffer); | ||
| let limit = max_buffer_size.unwrap_or(buffer.len()); | ||
| // Reusing an allocation should not skip the capacity checks while filling it. | ||
| buffer.truncate(ALLOC_CHUNK_SIZE.max(carry_over.len())); |
There was a problem hiding this comment.
this now zero-fills the whole recycled buffer again on every read, on 64-bit too
is it the cause of the codspeed regression?
| /// The remaining bytes must be copied to the start of the buffer for the next invocation, | ||
| /// if another invocation is necessary, which is determined by the other return value. | ||
| /// * Whether this function should be called again. | ||
| #[allow(clippy::too_many_arguments)] |
There was a problem hiding this comment.
could we avoid the #[allow] here, e.g. by grouping the reserve closure and limit?
| } | ||
| buffer.resize(next_end, 0); | ||
| } | ||
| read_target = &mut buffer[end..next_end]; |
There was a problem hiding this comment.
with this window, each read() gets at most 64 KiB, so filling a 1 GiB buffer from a regular file takes ~16k syscalls instead of a few
does codspeed still show the regression on this head?
| separator, | ||
| )?; | ||
| if read != 0 { | ||
| let (fitted, line_count) = fit_line_capacity(&buffer[..read], separator, |count| { |
There was a problem hiding this comment.
read_to_buffer already counted and reserved the lines, do we really need a second full memchr pass here in the common case?
|
GNU testsuite comparison: |
2b3db45 to
c0bd40a
Compare
|
Thanks, Sylvestre, for updating the branch and for pointing out the extra
reads and the second scan.
I checked c0bd40a. Pre-commit passed, and the test suite
<https://github.com/uutils/coreutils/actions/runs/46053355615> and benchmark
run <https://github.com/uutils/coreutils/actions/runs/46053355642> are
still queued. We’ll need the CodSpeed result for this commit to see whether
the earlier regression persists.
On Fri, 27 Nov 2026 08:42:56 -0800, Sylvestre Ledru < ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/uu/sort/src/chunks.rs
<#16901 (comment)>:
}
- // We need to read more lines
- let len = buffer.len();
- buffer.resize(len + ALLOC_CHUNK_SIZE, 0);
- read_target = &mut buffer[len..];
+ let next_end = end + ALLOC_CHUNK_SIZE;
+ if next_end > buffer.len() {
+ if !try_reserve(buffer, next_end - buffer.len()) {
+ return spillable_prefix(reserved_end, end);
+ }
+ buffer.resize(next_end, 0);
+ }
+ read_target = &mut buffer[end..next_end];
with this window, each read() gets at most 64 KiB, so filling a 1 GiB
buffer from a regular file takes ~16k syscalls instead of a few
does codspeed still show the regression on this head?
------------------------------
In src/uu/sort/src/chunks.rs
<#16901 (comment)>:
separator,
)?;
+ if read != 0 {
+ let (fitted, line_count) = fit_line_capacity(&buffer[..read],
separator, |count| {
read_to_buffer already counted and reserved the lines, do we really need a
second full memchr pass here in the common case?
—
Reply to this email directly, view it on GitHub
<#16901?email_source=notifications&email_token=BCKHDNNTKD5ORSMOOX57GHD53GVJBA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTMMBZGEYTEMRZGE4KM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#pullrequestreview-6091122918>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BCKHDNPQM5W6M54227FCJEL53GVJBAVCNFSNUABEKJSXA33TNF2G64TZHMYTCOBUG42TAMB3JFZXG5LFHM3DMNBUG43TINZQG6QXMAQ>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/BCKHDNMHAFTU4MPJFSGDBWT53GVJBA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTMMBZGEYTEMRZGE4KM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG>
and Android
<https://github.com/notifications/mobile/android/BCKHDNOPFV2HG7RIVMDQNVT53GVJBA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTMMBZGEYTEMRZGE4KM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>.
Download it today!
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
Thanks, Sylvestre. I went through your review against c0bd40a.
- The 32 MiB cap is gone. The existing 1 GiB automatic ceiling is
unchanged, and allocation retries run on both 32-bit and 64-bit. The
separate stable-sort change is in #16906.
- The reserve logic is shared through try_reserve_lines, the 1 MiB
allowance is named SPILL_HEADROOM_BYTES, and the read path uses
explicit branches and if-let separator handling. ReadBudget groups the
limit and reserve callback in read_to_buffer. The outer read wrapper
still has its existing Clippy allowance.
- There is now a Linux integration test under RLIMIT_AS as well as
tests for buffer reuse and spilling complete records. c0bd40a reuses
initialized bytes and only zero-fills newly added bytes.
- The 64 KiB window is still there. A focused check of read_to_buffer
with a regular 16 MiB file recorded 256 non-empty read calls, each
requesting at most 64 KiB. This checks the window size, not sort
throughput.
- fit_line_capacity still recounts the prefix on the common path.
read_to_buffer tracks the last reserved record boundary, but its
result does not carry the exact count for the emitted prefix. Carrying
that count through looks like a way to skip the recount when the
prefix is already covered, while keeping the fallback for EOF and
spill boundaries.
The latest test and benchmark workflows are still queued, so I can’t
confirm whether the earlier CodSpeed regression persists on this head.
…On Fri, 27 Nov 2026 08:42:56 -0800, Sylvestre Ledru ***@***.***> wrote:
@sylvestre commented on this pull request.
> }
- // We need to read more lines
- let len = buffer.len();
- buffer.resize(len + ALLOC_CHUNK_SIZE, 0);
- read_target = &mut buffer[len..];
+ let next_end = end + ALLOC_CHUNK_SIZE;
+ if next_end > buffer.len() {
+ if !try_reserve(buffer, next_end - buffer.len()) {
+ return spillable_prefix(reserved_end, end);
+ }
+ buffer.resize(next_end, 0);
+ }
+ read_target = &mut buffer[end..next_end];
with this window, each read() gets at most 64 KiB, so filling a 1 GiB buffer from a regular file takes ~16k syscalls instead of a few
does codspeed still show the regression on this head?
> separator,
)?;
+ if read != 0 {
+ let (fitted, line_count) = fit_line_capacity(&buffer[..read], separator, |count| {
read_to_buffer already counted and reserved the lines, do we really need a second full memchr pass here in the common case?
--
Reply to this email directly or view it on GitHub:
#16901 (review)
You are receiving this because you authored the thread.
Message ID: ***@***.***>
|
Cap
MAX_AUTOMATIC_BUF_SIZEat 32 MiB on 32-bit targets to leave enough virtual address space for per-line metadata and sorting scratch space, preventing out-of-memory crashes on large inputs. Updated the buffer hint unit tests to verify the 32-bit clamping behavior.Closes #16850