Skip to content

du: buffer output to files and pipes - #15483

Open
limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/du-buffer-output-9146
Open

limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/du-buffer-output-9146

Conversation

@limadog9

Copy link
Copy Markdown
Contributor

Buffer output to files and pipes, preserve immediate terminal output, and flush before diagnostics and on completion. Add tests for final totals and flush failures. A 10,000-file check reduced write calls from 10,001 to 7.

Validation: 108 du integration tests pass. Verified unchanged output with non-UTF-8 names, totals, NUL separators, and timestamps.

Closes #9146

@codspeed

codspeed Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 1 benchmark

1 warning

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

⚡ 2 improved benchmarks
❌ 1 regressed benchmark
✅ 5 untouched benchmarks
⏩ 482 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory du_deep_tree[(100, 3)] 82.4 KB 90.4 KB -8.85%
⚡ CPU Simulation du_all_wide_tree[(5000, 500)] 33.1 ms 29 ms +14.11%
⚡ CPU Simulation du_deep_tree[(100, 3)] 1.8 ms 1.7 ms +4.3%

Tip

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


Comparing limadog9:fix/du-buffer-output-9146 (5a647c7) with main (279cdbf)2

Open in CodSpeed

Footnotes

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

@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from 1fa4ff9 to 139a237 Compare October 18, 2026 19:48
@sylvestre

Copy link
Copy Markdown
Contributor

@codspeedbot fix this regression

Comment thread src/uu/du/src/du.rs Outdated
// be printed by the producer thread.
// A small buffer batches redirected output without a large allocation
// for invocations that only print a few totals.
let capacity = if stdout.is_terminal() { 0 } else { 2 * 1024 };

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.

why 2 KiB and not the default capacity?

Comment thread src/uu/du/src/du.rs Outdated
// A small buffer batches redirected output without a large allocation
// for invocations that only print a few totals.
let capacity = if stdout.is_terminal() { 0 } else { 2 * 1024 };
let mut output = BufWriter::with_capacity(capacity, stdout);

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.

with --verbose, the "ignored" lines printed by the traversal thread with println! now come before the buffered stats when stdout is a file, so the order differs from main.
could you please route them through the printer or flush before them? and please add a test with -v --exclude

Comment thread tests/by-util/test_du.rs Outdated
}

#[test]
fn test_buffered_output_flushes_final_total() {

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 one passes on main too, no? it doesn't really test the buffering

Comment thread tests/by-util/test_du.rs Outdated
}
ucmd.set_stdout(std::fs::File::create("/dev/full").unwrap())
.fails_with_code(1)
.stderr_contains("No space left on device")

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.

stderr_does_not_contain("panicked") isn't needed, fails_with_code + stderr_contains is enough

@sylvestre

Copy link
Copy Markdown
Contributor

codspeed reports a 13.86% regression, could you please check where it comes from?

@codspeed

codspeed Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

@codspeedbot fix this regression

The current head of this PR (139a237) already fixes this regression, so I didn't push any changes.

Cause

The report above was produced for an earlier commit, 1fa4ff9. That commit gave the output a BufWriter::with_capacity(64 * 1024, …) whenever stdout is not a terminal. The benchmarks write to a non-terminal stdout, so this 64 KiB buffer stayed allocated for the whole run. Every memory regression in the report matches it exactly:

Benchmark Base 1fa4ff9 Δ
du_deep_tree[(100, 3)] 76.5 KB 140.5 KB +64.0 KB
du_summarize_balanced_tree[(5, 4, 10)] 419.9 KB 483.9 KB +64.0 KB
du_wide_tree[(5000, 500)] 1,010.5 KB 1,074.5 KB +64.0 KB

Current head

139a237 cuts the buffer to 2 KiB. That should bring the extra peak memory down from 64 KB to about 2 KB: roughly +2.6% on du_deep_tree, and under 0.5% on the other benchmarks. That's still enough to batch redirected output into a few large writes.

I couldn't measure memory in my environment, so these memory numbers are estimates. CodSpeed hasn't benchmarked 139a237 in CI yet, and the next CI run will confirm them.

I did measure CPU simulation locally, main (aa74c61) against 139a237:

Benchmark main head Change
du_all_wide_tree[(5000, 500)] 14.69 ms 12.94 ms −11.9%
du_deep_tree[(100, 3)] 842 µs 806 µs −4.3%
du_wide_tree[(5000, 500)] 6.87 ms 6.75 ms −1.7%
du_summarize_balanced_tree[(5, 4, 10)] 4.83 ms 4.84 ms ~0%

The speedup from buffering is still there, and the time doesn't go up on any du benchmark.

If CI still shows a memory regression after it runs, comment @codspeedbot investigate the memory results and I'll look into it.

Summary
  • Warming up the performance wizard...
  • Analyze the reported regression — Report was for 1fa4ff9, which used a 64 KiB BufWriter (+64 KB peak memory). Current head 139a237 already reduced it to 2 KiB but has not been benchmarked yet.
  • Measure memory + simulation of current head vs main — Simulation measured locally: head is the same or faster on every du benchmark. Memory could not be measured here; expected delta is +2 KiB
  • Reply with findings — Reply with findings
  • Handling comment — Comment handling finished successfully

Was this response helpful? React with 👍 or 👎 to help us improve CodSpeedBot.

@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch 3 times, most recently from 0f16a92 to ca21e3a Compare October 19, 2026 11:35
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/follow-symlink is no longer failing!

Comment thread src/uu/du/src/du.rs Outdated
let mut output = if stdout.is_terminal() {
BufWriter::with_capacity(0, stdout.lock())
} else {
// Batch lines without retaining the default 8 KiB throughout traversal.

This comment was marked as resolved.

Comment thread src/uu/du/src/du.rs Outdated
BufWriter::with_capacity(0, stdout.lock())
} else {
// 1 KiB limits memory retained throughout traversal while batching lines.
// A 10,000-file check ran within 2% of the default 8 KiB buffer's time.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

so does performance regress by 2% by reducing buffer size from 8 KiB to 1 KiB?

and why 1 KiB, not 512 B or 2 KiB?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair point—the earlier check didn’t justify choosing 1 KiB. I’ve switched to BufWriter::new() and removed the 2% comment.

I compared 512 B, 1 KiB, 2 KiB, and the default. Timings varied too much to draw a reliable conclusion, but the default used 35 writes on the 10,000-file case, versus 273 with 1 KiB and 10,001 without buffering. The tradeoff is a larger fixed buffer, currently 8 KiB.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the default BufWriter buffer size is likely the best choice unless benchmarks provide evidence otherwise.

@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from 6662750 to 52e2f2a Compare October 21, 2026 14:41
@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from 52e2f2a to b0e4ca7 Compare November 23, 2026 01:02
@sylvestre
sylvestre force-pushed the fix/du-buffer-output-9146 branch from b0e4ca7 to eed52a9 Compare November 23, 2026 20:41
@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from eed52a9 to 8466378 Compare November 29, 2026 15:15
@sylvestre
sylvestre force-pushed the fix/du-buffer-output-9146 branch from 8466378 to e1487ac Compare November 29, 2026 15:20
@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch 2 times, most recently from 585bc02 to a1f4470 Compare November 30, 2026 04:21
Comment thread src/uu/du/src/du.rs Outdated
let stdout = stdout().lock();
let mut output = if stdout.is_terminal() {
// Keep terminal output immediate.
BufWriter::with_capacity(0, stdout)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider introducing an enum to avoid initialising a BufWriter in the unbuffered case, something like:

enum OutputWriter<'a> {
    Immediate(io::StdoutLock<'a>),
    Buffered(BufWriter<io::StdoutLock<'a>>),
}

@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch 2 times, most recently from f954161 to 070d4ce Compare November 30, 2026 19:25
@xtqqczze

xtqqczze commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

The new approach using generics results in code size +2208 bytes:

$ git checkout -q a1f4470 
$ CARGO_PROFILE_RELEASE_LTO=off cargo build -p uu_du -q -r && wc -c < target/release/du
 2031440
$ git checkout -q 070d4ce                                                              
$ CARGO_PROFILE_RELEASE_LTO=off cargo build -p uu_du -q -r && wc -c < target/release/du
 2032736

What is the intent of the changes in 070d4ce?

Comment thread src/uu/du/src/du.rs Outdated
}
}

fn print_stats_to(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Functions taking a writer should be named with write_ prefix, with the writer being the first parameter after &self. Also does the function need to own the writer, could take &mut impl OsWrite, no?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could call the writer writer instead of output too.

@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from 070d4ce to b5915c8 Compare December 1, 2026 00:38
@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from b5915c8 to 5a647c7 Compare December 1, 2026 02:10
@xtqqczze

xtqqczze commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

5a647c7 got a bit better for code size

$ git checkout -q 5a647c7762bc6cff663f78cc91d297ba7968cf72
$ CARGO_PROFILE_RELEASE_LTO=off cargo build -p uu_du -q -r && wc -c < target/release/du
 2030752

Send stats and verbose messages through one writer to preserve their order. Flush redirected output before reporting traversal errors and when printing finishes so write failures reach the caller.

Borrow the selected writer so terminals need no extra buffer and both output modes share one copy of the printing loop.
@limadog9
limadog9 force-pushed the fix/du-buffer-output-9146 branch from 5a647c7 to e522982 Compare December 1, 2026 17:27

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.

du: Use BufWriter<Stdout> and write to stdout when the buffer is near full than directly printing always

3 participants