Skip to content

cut: bound memory usage for streamable selections and improve the perf (+23%) - #15094

Merged
sylvestre merged 7 commits into
uutils:mainfrom
mattsu2020:bounded-memory.sh
Nov 9, 2026
Merged

sylvestre merged 7 commits into
uutils:mainfrom
mattsu2020:bounded-memory.sh

Conversation

@mattsu2020

@mattsu2020 mattsu2020 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

cut buffers entire records in several modes, causing memory to grow without bound on input without newlines. Stream byte selections and single-byte fields, and retain only the required prefix for finite character selections. Add regression tests for chunk boundaries and long records under a memory limit.

Closes #13990

Validation: 64 cut integration tests and 22 unit tests pass on macOS. The Linux memory-limit regression test has not been run locally.

Limitation: an open-ended character range such as -c 2- in a multi-byte locale still keeps the whole record in memory, since there is no upper bound on the prefix to retain.

@codspeed

codspeed Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 23.3%

2 warnings

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

⚠️ 3 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 3 improved benchmarks
✅ 9 untouched benchmarks
⏩ 464 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation cut_characters 22.9 ms 16.6 ms +37.53%
⚡ CPU Simulation cut_fields_tab 45.5 ms 37 ms +22.83%
⚡ CPU Simulation cut_fields_custom_delim 51.9 ms 46.8 ms +10.95%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mattsu2020:bounded-memory.sh (931d159) with main (47acd14)2

Open in CodSpeed

Footnotes

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

  2. No successful run was found on main (35fc618) during the generation of this report, so 47acd14 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@xtqqczze

xtqqczze commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

please fix clippy and cspell

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/cut/bounded-memory is no longer failing!
Congrats! The gnu test tests/env/printenv is no longer failing!

@xtqqczze

xtqqczze commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)

Please remove from .github/workflows/ignore-intermittent.txt if it is fixed.

@xtqqczze

xtqqczze commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

@mattsu2020 please convert to draft until it is ready

@mattsu2020
mattsu2020 marked this pull request as draft October 5, 2026 14:31
@xtqqczze

xtqqczze commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

@xtqqczze

xtqqczze commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

@mattsu2020 when it is ready, please drop merge commits and then rebase for a clean history

Comment thread src/uu/cut/src/cut.rs Outdated
out_delim: &[u8],
explicit_delim: bool,
/// Process records in fixed-size chunks without retaining their contents.
fn for_record_chunks<R: Read>(

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.

seems like a too generic function name, it doesn't say that a record is split into pieces

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased

Comment thread src/uu/cut/src/cut.rs
) -> UResult<()> {
let encoding = locale_encoding();
if encoding == Encoding::SingleByte || !(by_char || opts.suppress_split) {
if ranges.is_empty() || encoding == Encoding::SingleByte || !(by_char || opts.suppress_split) {

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.

when can ranges be empty here? --complement -c 1-? please add a test for it

Comment thread src/uu/cut/src/cut.rs
) -> UResult<()> {
let encoding = locale_encoding();
if encoding == Encoding::SingleByte || !(by_char || opts.suppress_split) {
if ranges.is_empty() || encoding == Encoding::SingleByte || !(by_char || opts.suppress_split) {

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.

when can ranges be empty here? --complement -c 1-? please add a test for it

Comment thread src/uu/cut/src/cut.rs

// All supported encodings use at most four bytes per character. Once
// this prefix is present, later bytes cannot affect a finite selection.
if let Some(limit) = ranges.last().and_then(|r| r.high.checked_mul(4)) {

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.

open-ended ranges like -c 2- still keep the whole record in memory in UTF-8, is it expected?
if yes, please say it in the PR description

Comment thread src/uu/cut/src/cut.rs Outdated
delimiter: u8,
) -> UResult<()> {
let newline = opts.line_ending.into();
let only_delimited = opts.field_opts.as_ref().unwrap().only_delimited;

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.

cut_fields already unwrapped field_opts, could you please pass only_delimited as a parameter instead?

Comment thread src/uu/cut/src/cut.rs Outdated
let buffer_first = first_selected == only_delimited;
let delimiter_bytes = [delimiter];
let matcher = ExactMatcher::new(&delimiter_bytes);
let mut first = Vec::new();

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.

this is a lot of mut, often a sign of potential refactor

@sylvestre
sylvestre force-pushed the bounded-memory.sh branch 4 times, most recently from 54a1d8b to 9eab958 Compare November 8, 2026 16:35
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.32 MB (+76 KB, +0.05%)

Significant per-binary changes:
  printenv     1.09 MB ->    1.15 MB  (+60 KB, +5.36%)

@sylvestre sylvestre changed the title cut: bound memory usage for streamable selections cut: bound memory usage for streamable selections and improve the perf (+23%) Nov 9, 2026
@sylvestre
sylvestre marked this pull request as ready for review November 9, 2026 17:42
Copilot AI balanced review requested due to automatic review settings November 9, 2026 17:42

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre merged commit 27498d7 into uutils:main Nov 9, 2026
103 of 104 checks passed
@mattsu2020
mattsu2020 deleted the bounded-memory.sh branch November 9, 2026 22:44
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.

Fixing flaky cut bounded memory test

4 participants