docker stats: assorted fixes and optimizations in rendering#6876
docker stats: assorted fixes and optimizations in rendering#6876vvoland merged 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
e7a3a64 to
1d5578b
Compare
cli/command/container/stats.go
Outdated
| _, _ = io.WriteString(&frameBuf, "\033[J") | ||
| _, _ = dockerCLI.Out().Write(frameBuf.Bytes()) | ||
|
|
||
| if len(ccStats) == 0 && !showAll { |
There was a problem hiding this comment.
I think this is dead code since we already have the same check in L325?
There was a problem hiding this comment.
oh, you're right; I think it is; let me remove
There was a problem hiding this comment.
Removed 👍
There's more to cleanup in this code IMO; I hate the FormatContext having the buffer / writer attached here; it would've been much more organic if statsFormatWrite would just get a buffer passed in I think
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>
1d5578b to
e7cbaaf
Compare
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)