feat: trim hot-path rendering overhead in TUI frame assembly and diff pipeline#142
Merged
Conversation
… pipeline Reduce per-frame work in the rendering pipeline after the architectural changes in #135, #136, and #137: Rendering pipeline refinement: - DifferentialRenderer accepts terminal dimensions from the caller, eliminating redundant terminal::size() syscalls on every frame - Replace FNV-1a full-content hashing with lightweight length-based identity check; per-line comparison already catches all differences - Only flush stdout when actual terminal writes occurred - Track change state to skip no-op flush operations Terminal housekeeping reductions: - Hide cursor once at session start instead of per-frame Hide/Show churn - Pass resize dimensions directly to DifferentialRenderer via update_dimensions() on resize events - Remove per-frame stdout acquisition for cursor operations Hot-path allocation cleanup: - print_colored_text: zero-allocation fast path when width is None (the common case), avoiding text.to_string() on every call - truncate_to_width: returns Cow<str> with zero-copy borrow when the string already fits, with an ASCII fast-path for byte-level slicing - Process renderer: reuse RowFormatter scratch buffer across rows, use write! into pre-sized buffers instead of per-field format! calls, borrow command string instead of cloning, use Cow<str> for GPU percent and GPU memory fields, reuse a single clear-line string for padding - draw_bar/draw_bar_multi: batch consecutive same-type segments into single print_colored_text calls instead of per-character emission, reducing terminal escape sequence count by ~50x per gauge - format_cpu_time: return Cow<str> to avoid allocation for common "0:00:00" case - format_hostname_with_scroll: build scroll window directly into a pre-sized String without intermediate extended-hostname allocation - BufferWriter: add reset() method for future frame-to-frame reuse Measurement support: - Add unit tests for BufferWriter throughput, DifferentialRenderer state management, and truncate_to_width allocation behavior - Add timing-based throughput tests for the render path
…nfinite loop - DifferentialRenderer: remove dead content_bytes fast-check that never short-circuited; the per-line comparison is already the authoritative change-detection mechanism and is O(1) for identical strings - format_hostname_with_scroll: add ASCII guard before byte-level indexing to prevent character corruption on non-ASCII hostnames - draw_bar_multi: guarantee forward progress in segment batching loop when text overlay is behind current position
Member
Author
Code Review SummaryReviewed all 7 changed files in this PR (+679/-212 lines across the TUI rendering pipeline). Findings Addressed (3 fixes committed)
Items Not Fixed (pre-existing, not introduced by this PR)
Overall AssessmentThe PR's optimizations are sound and well-targeted:
All 497+ tests pass. Clippy clean. Formatting clean. |
Add test coverage for the optimized code paths introduced in this PR: - process_renderer: write_memory_size correctness, format_cpu_time Cow allocation behavior (zero/long-running borrows vs owned), RowFormatter scratch buffer reuse, and throughput measurements for the zero-seconds hot path - widgets: draw_bar and draw_bar_multi correctness (label, brackets, percentage text, custom text, long-label trimming) and escape-sequence batching verification confirming the ~50x reduction in terminal writes per gauge; includes throughput measurements for both functions
Member
Author
PR Finalization CompleteSummary
|
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.
Summary
Reduces per-frame work in the TUI rendering pipeline, building on the architectural changes from #135, #136, and #137:
terminal::size()syscalls. Replaced FNV-1a full-content hashing with lightweight length-based identity check. Only flushes stdout when actual terminal writes occurred.update_dimensions().print_colored_texthas a zero-allocation fast path for the common no-width case.truncate_to_widthreturnsCow<str>with zero-copy borrow when the string fits. Process renderer reuses scratch buffers across rows, borrows command strings instead of cloning, and usesCow<str>for GPU fields.draw_bar/draw_bar_multibatch consecutive segments into single calls (~50x fewer terminal escape sequences per gauge).format_cpu_timereturnsCow<str>for the common "0:00:00" case.Closes #138
Test plan
cargo test)cargo clippy -- -D warningspasses with zero warningscargo fmt --checkpassesall-smilocally and confirm visual correctness