Skip to content

cat: avoid unnecessary allocation#11675

Merged
sylvestre merged 1 commit intouutils:mainfrom
oech3:cat-alloc
Apr 12, 2026
Merged

cat: avoid unnecessary allocation#11675
sylvestre merged 1 commit intouutils:mainfrom
oech3:cat-alloc

Conversation

@oech3
Copy link
Copy Markdown
Contributor

@oech3 oech3 commented Apr 6, 2026

Allocate buffer on heap instead of stack for read()/write() show-path which is unnecessary if splice() fast-path succeed.

$ echo 1 > /tmp/1
> taskset -c 0 hyperfine -N --runs 10000 "/tmp/coreutils/target/release/cat-stack /tmp/1" "target/release/cat-heap /tmp/1"
Benchmark 1: /tmp/coreutils/target/release/cat-stack /tmp/1
  Time (mean ± σ):     921.2 µs ±  84.4 µs    [User: 372.9 µs, System: 443.7 µs]
  Range (min … max):   843.0 µs … 3926.9 µs    10000 runs
Benchmark 2: target/release/cat-heap /tmp/1
  Time (mean ± σ):     908.6 µs ± 117.0 µs    [User: 380.6 µs, System: 424.1 µs]
  Range (min … max):   821.4 µs … 4337.6 µs    10000 runs 
Summary
  target/release/cat-heap /tmp/1 ran
    1.01 ± 0.16 times faster than /tmp/coreutils/target/release/cat-stack /tmp/1

related #10832

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

GNU testsuite comparison:

Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/basenc/bounded-memory is now being skipped but was previously passing.
Note: The gnu test tests/dd/no-allocate is now being skipped but was previously passing.
Note: The gnu test tests/tail/tail-n0f is now being skipped but was previously passing.
Congrats! The gnu test tests/cut/bounded-memory is now passing!

@oech3 oech3 marked this pull request as ready for review April 6, 2026 07:55
@oech3 oech3 marked this pull request as draft April 6, 2026 08:04
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/resolution (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/cut/cut-huge-range is now passing!

@oech3
Copy link
Copy Markdown
Contributor Author

oech3 commented Apr 6, 2026

hyperfine is flakey

@oech3 oech3 marked this pull request as ready for review April 6, 2026 08:49
@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented Apr 6, 2026

Switching from a stack allocation to a heap allocation doesn’t avoid allocation...

@oech3
Copy link
Copy Markdown
Contributor Author

oech3 commented Apr 6, 2026 via email

@oech3
Copy link
Copy Markdown
Contributor Author

oech3 commented Apr 6, 2026

I saw more perf difference with 1024 * 1024 by switching to vec. So I think vec's allocation is deffered.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

GNU testsuite comparison:

Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

GNU testsuite comparison:

Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/cut/cut-huge-range is now passing!

@sylvestre sylvestre merged commit efd0f0c into uutils:main Apr 12, 2026
169 checks passed
@oech3 oech3 deleted the cat-alloc branch April 12, 2026 14:38
@oech3
Copy link
Copy Markdown
Contributor Author

oech3 commented Apr 12, 2026

We might use nightly fill_buf in the future to avoid 0-fill at here.

@xtqqczze
Copy link
Copy Markdown
Contributor

We might use nightly fill_buf in the future to avoid 0-fill at here.

Presumably you mean nightly-only Read::read_buf. Might be worth prototyping an implementation to validate this approach.

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.

3 participants