Skip to content

cut: replace Box<dyn Write> with uucore's OutputWriter - #16406

Open
shergin wants to merge 4 commits into
uutils:mainfrom
shergin:cut-enum-writer
Open

shergin wants to merge 4 commits into
uutils:mainfrom
shergin:cut-enum-writer

Conversation

@shergin

@shergin shergin commented Nov 13, 2026 •

Copy link
Copy Markdown

part of #11811.

#13075 tried this but got slower, because its enum only forwarded write and flush.
cut writes through write_all, and the trait's default write_all loops over write,
while BufWriter has its own that just copies into the buffer.
so this one forwards all of Write's methods.

the enum lives in uucore::io next to InputReader, so other utils can use it too.

on my M1 Pro it's 5-21% faster than Box<dyn Write>, output is identical:

case Box<dyn Write> this PR
-b 1-20 62.8 ms 54.5 ms
-c 5-30 mixed, UTF-8 134.5 ms 105.9 ms
-f 2,4 tab 85.0 ms 80.8 ms

Comment thread src/uu/cut/src/cut.rs Resolved
Comment thread src/uu/cut/Cargo.toml Outdated
fluent = { workspace = true }
memchr = { workspace = true }
uucore = { workspace = true, features = ["i18n-charmap", "parser", "ranges"] }
whichever = { workspace = true }

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.

We don't prefer new dependency just for this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for such a quick response!
that’s fair! I’ll update this PR with a hand-written version in uucore::io then.
it'll still forward write_all and the rest, since that’s what made #13075 slower.

just wanted to show that it can be done this way in case it’s helpful.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did it here:
https://github.com/uutils/coreutils/pull/16408/changes

ofc, feel free to close both if that doesn't really bring much

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.

Please don't open new PR. Rebase this PR instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@shergin shergin changed the title cut: replace Box<dyn Write> with an enum cut: replace Box<dyn Write> with uucore's OutputWriter Nov 13, 2026
@shergin
shergin marked this pull request as ready for review November 13, 2026 06:16
@codspeed

codspeed Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 39.81%

2 warnings, 1 note

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

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

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 13 improved benchmarks
✅ 392 untouched benchmarks
🆕 8 new benchmarks
⏩ 70 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Memory factor_small_factors_and_a_large_prime 104.8 KB 15.4 KB ×6.8
⚡ CPU Simulation factor_small_factors_and_a_large_prime 1,012.7 µs 682 µs +48.49%
⚡ CPU Simulation cut_bytes 15.3 ms 10.6 ms +45.2%
⚡ CPU Simulation cut_characters 16.6 ms 12.3 ms +35%
⚡ CPU Simulation join_partial_overlap 18.7 ms 15.1 ms +23.42%
⚡ CPU Simulation join_full_match 21.8 ms 18.3 ms +19.19%
⚡ CPU Simulation join_french_locale 21.8 ms 18.3 ms +19.15%
⚡ CPU Simulation join_unicode_locale 2.2 ms 1.8 ms +19.09%
⚡ CPU Simulation cut_fields_tab 37 ms 31.1 ms +19.02%
⚡ CPU Simulation cut_fields_custom_delim 46.8 ms 39.3 ms +19%
⚡ CPU Simulation join_custom_separator 22.1 ms 18.8 ms +17.82%
⚡ CPU Simulation cut_characters_long_lines 40.7 ms 37.5 ms +8.39%
⚡ CPU Simulation du_wide_tree[(5000, 500)] 16 ms 15.3 ms +4.6%
🆕 Memory factor_batch_of_largest_u64s N/A 796 KB N/A
🆕 Memory factor_batch_of_semiprimes N/A 199.5 KB N/A
🆕 Memory factor_batch_of_small_numbers N/A 7.5 MB N/A
🆕 Memory factor_batch_of_smallest_u128s N/A 885.4 KB N/A
🆕 CPU Simulation factor_batch_of_largest_u64s N/A 557.4 ms N/A
🆕 CPU Simulation factor_batch_of_semiprimes N/A 794.2 ms N/A
🆕 CPU Simulation factor_batch_of_small_numbers N/A 138.6 ms N/A
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

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


Comparing shergin:cut-enum-writer (d9c9f4c) with main (fca3de1)2

Open in CodSpeed

Footnotes

  1. 70 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 (8fdb44a) during the generation of this report, so fca3de1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/usage_vs_getopt (passes in this run but fails in the 'main' branch)

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the work on this!
CI "check" is red, please have a look and fix it.

Comment thread src/uu/cut/Cargo.toml Outdated
fluent = { workspace = true }
memchr = { workspace = true }
uucore = { workspace = true, features = ["i18n-charmap", "parser", "ranges"] }
uucore = { workspace = true, features = ["fs", "i18n-charmap", "parser", "ranges"] }

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.

do we really need the whole fs feature just for uucore::io? it only exists on unix/windows/wasi

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, it didn't need it. OutputWriter (and InputReader) are plain std::io, only RawReader/RawWriter and OwnedFileDescriptorOrHandle actually need rustix and the fd/handle types.
so uucore::io is now always compiled and the fs + platform gate sits only on those items. cut doesn't need fs anymore, so it builds on any target again.

that also took care of the red "check": it was taplo wanting the longer features line split, and cut's Cargo.toml is back to what's on main.

Only the file descriptor and handle types need fs; InputReader,
OutputWriter, ReadWriteError and read_and_discard are plain std::io.

This branch has not been deployed

No deployments
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