docker stats: assorted fixes and optimizations in rendering#6876
Open
thaJeztah wants to merge 5 commits intodocker:masterfrom
Open
docker stats: assorted fixes and optimizations in rendering#6876thaJeztah wants to merge 5 commits intodocker:masterfrom
thaJeztah wants to merge 5 commits intodocker:masterfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
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 internalstatstracker to safely copy the tracked container list under lock. - Updated
RunStatsto render usingbytes.Buffer/io.WriteStringand to avoidbytes -> stringconversions. - Simplified
statsFormatWriteby 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.
2a620c7 to
e7a3a64
Compare
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>
e7a3a64 to
1d5578b
Compare
vvoland
reviewed
Apr 2, 2026
| _, _ = io.WriteString(&frameBuf, "\033[J") | ||
| _, _ = dockerCLI.Out().Write(frameBuf.Bytes()) | ||
|
|
||
| if len(ccStats) == 0 && !showAll { |
Collaborator
There was a problem hiding this comment.
I think this is dead code since we already have the same check in L325?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
docker statsto not require clearing the whole screen #5625cli/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.Bufferto render the stats, before writingthe results to the CLI's output. Let's try to use bytes where possible
instead of converting to a string;
Write(andOut().Write) to write directly to thebuffer/writer where possible.
io.WriteStringinstead offmt.Printfbytes.SplitSeqinstead ofstrings.SplitSeqcli/command/container: statsFormatWrite: inline render func
- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)