Skip to content

docker stats: assorted fixes and optimizations in rendering#6876

Open
thaJeztah wants to merge 5 commits intodocker:masterfrom
thaJeztah:stats_optimize
Open

docker stats: assorted fixes and optimizations in rendering#6876
thaJeztah wants to merge 5 commits intodocker:masterfrom
thaJeztah:stats_optimize

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Mar 20, 2026

cli/command/container: fix buffer reuse when printing stats

Don't write lines back into the same buffer that's being read from when
clearing lines; add a separate output buffer to construct the output,
then write it to the CLI's output at once (to prevent terminal flicker).

Relates to / introduced in cb2f95c.

cli/command/container: stats: add snapshot method

Move logic to capture a snapshot of the current stats to the stats struct.

cli/command/container: RunStats: rename buffer var for brevity

cli/command/container: RunStats: avoid bytes to strings conversions

This code is using a bytes.Buffer to render the stats, before writing
the results to the CLI's output. Let's try to use bytes where possible
instead of converting to a string;

  • Use the buffer's Write (and Out().Write) to write directly to the
    buffer/writer where possible.
  • Use io.WriteString instead of fmt.Printf
  • Use bytes.SplitSeq instead of strings.SplitSeq

cli/command/container: statsFormatWrite: inline render func

- Human readable description for the release notes

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added this to the 29.3.1 milestone Mar 20, 2026
@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Mar 20, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 23.80952% with 32 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cli/command/container/stats.go 0.00% 24 Missing ⚠️
cli/command/container/stats_helpers.go 0.00% 8 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors and optimizes docker stats rendering by centralizing stats snapshot collection and reducing string conversions during output generation, aiming to improve performance and reduce terminal flicker.

Changes:

  • Added a snapshot() method on the internal stats tracker to safely copy the tracked container list under lock.
  • Updated RunStats to render using bytes.Buffer/io.WriteString and to avoid bytes -> string conversions.
  • Simplified statsFormatWrite by inlining the per-entry render closure.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cli/command/container/stats.go Switches rendering to byte-oriented writes and uses snapshot() to avoid holding the stats lock while building output.
cli/command/container/stats_helpers.go Adds (*stats).snapshot() helper for point-in-time container list copying.
cli/command/container/formatter_stats.go Minor refactor to inline the render function passed to ctx.Write.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@thaJeztah thaJeztah force-pushed the stats_optimize branch 2 times, most recently from 2a620c7 to e7a3a64 Compare April 2, 2026 13:05
@thaJeztah thaJeztah requested a review from Copilot April 2, 2026 13:10

This comment was marked as resolved.

Don't write lines back into the same buffer that's being read from when
clearing lines; add a separate output buffer to construct the output,
then write it to the CLI's output at once (to prevent terminal flicker).

Relates to / introduced in cb2f95c.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Move logic to capture a snapshot of the current stats to the stats struct.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This code is using a `bytes.Buffer` to render the stats, before writing
the results to the CLI's output. Let's try to use bytes where possible
instead of converting to a string;

- Use the buffer's `Write` (and `Out().Write`) to write directly to the
  buffer/writer where possible.
- Use `io.WriteString` instead of `fmt.Printf`
- Use `bytes.SplitSeq` instead of `strings.SplitSeq`

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

This comment was marked as resolved.

_, _ = io.WriteString(&frameBuf, "\033[J")
_, _ = dockerCLI.Out().Write(frameBuf.Bytes())

if len(ccStats) == 0 && !showAll {
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 this is dead code since we already have the same check in L325?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants