Skip to content

expand, unexpand: flush output after each line - #15293

Open
r3wretrhy wants to merge 4 commits into
uutils:mainfrom
r3wretrhy:expand-unexpand-stdin-flush
Open

r3wretrhy wants to merge 4 commits into
uutils:mainfrom
r3wretrhy:expand-unexpand-stdin-flush

Conversation

@r3wretrhy

Copy link
Copy Markdown
Contributor

When stdin is read line-at-a-time (interactive terminal, or any producer that pauses between lines), expand / unexpand held converted output in a BufWriter and only flushed on exit. An interactive session therefore showed no converted line until the user quit, while GNU shows each line immediately.

Like cut, skip BufWriter when stdout is a terminal. On the buffered path, flush once per input chunk that completed a line (before the next read), so line-at-a-time pipes also see output promptly without a write per line on large inputs.

$ expand -t3
        a          # typed tab+a, Enter
   a               # GNU shows this right away; we used to wait until quit

Added test_stdin_tty_flushes_each_line to test_expand.rs and test_unexpand.rs.

Closes #11091

@codspeed

codspeed Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 7.31%

3 warnings

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

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

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

❌ 2 regressed benchmarks
✅ 6 untouched benchmarks
⏩ 476 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation expand_many_lines[100000] 37.5 ms 40.5 ms -7.34%
❌ CPU Simulation expand_custom_tabstops[50000] 27.2 ms 29.4 ms -7.28%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing r3wretrhy:expand-unexpand-stdin-flush (759ad17) with main (293a724)2

Open in CodSpeed

Footnotes

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

@oech3

oech3 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Can we avoid large performance drop?

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

Comment thread src/uu/expand/src/expand.rs Resolved
Comment thread src/uu/expand/src/expand.rs Resolved
Comment thread tests/by-util/test_expand.rs Resolved
@r3wretrhy

Copy link
Copy Markdown
Contributor Author

@oech3 The perf drop is fixed in 0a02252, it only flushes when stdin is a pipe/tty now. CodSpeed shows expand_many_lines +9% and expand_custom_tabstops +6.5% faster. The factor regression looks unrelated since this PR doesn't touch factor. Also enabled rustix for all unix targets so it builds on macOS/BSD.

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased, and a bunch of jobs are failing

@r3wretrhy
r3wretrhy force-pushed the expand-unexpand-stdin-flush branch from 7f48ca8 to 84c4f58 Compare November 16, 2026 08:22
@r3wretrhy

Copy link
Copy Markdown
Contributor Author

Rebased onto current main. I'll keep an eye on CI and fix anything related.

@r3wretrhy
r3wretrhy force-pushed the expand-unexpand-stdin-flush branch from 84c4f58 to 3c27381 Compare November 16, 2026 08:44
@sylvestre
sylvestre force-pushed the expand-unexpand-stdin-flush branch 2 times, most recently from b54db61 to efc5f67 Compare November 17, 2026 19:21
@github-actions

github-actions Bot commented Nov 18, 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.48 MB (+100 KB, +0.06%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@sylvestre
sylvestre force-pushed the expand-unexpand-stdin-flush branch from efc5f67 to 635bb72 Compare November 18, 2026 14:18
@r3wretrhy
r3wretrhy force-pushed the expand-unexpand-stdin-flush branch from 635bb72 to 9e964ef Compare November 18, 2026 20:22
@r3wretrhy

Copy link
Copy Markdown
Contributor Author

Rebased onto current main again to resolve the conflict with 293a724 ("expand: accept a zero +N or /N after a tab list") in tests/by-util/test_expand.rs; both tests are kept, and the expand/unexpand tests pass locally. The Style/format failure on the previous run was in tests/by-util/test_dir.rs / test_vdir.rs, which this PR does not touch.

When reading line-at-a-time from stdin, BufWriter only flushed on exit, so
an interactive session showed no converted output until the user quit. Match
cut by skipping BufWriter when stdout is a terminal, and on the buffered path
flush once per input chunk that completed a line, so piped line-at-a-time
producers also see prompt output without a write per line on large inputs.

Fixes uutils#11091
Keep the plain BufWriter<Stdout> path and flush before the next read only
when the input is stdin and stdin is not a regular file. File arguments and
redirected regular files are fully buffered again, avoiding the slowdown.
Qualify Stdio in the tty flush tests so the import block matches
main and the PR can merge without conflicting on use-lines.
@sylvestre
sylvestre force-pushed the expand-unexpand-stdin-flush branch from 9e964ef to 759ad17 Compare November 19, 2026 17:08

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.

expand/unexpand: no output if input is read from stdin

3 participants