-
Notifications
You must be signed in to change notification settings - Fork 7.5k
TUI Performance Optimization #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
entrepeneur4lyf
wants to merge
1
commit into
anomalyco:dev
Choose a base branch
from
entrepeneur4lyf:tui/performance
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+8,935
−348
Conversation
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
### Diff Component - **Baseline**: 27.6ms per diff render - **Optimized**: 3.3µs - 2.4ms (depending on cache hit rate) - **CPU Usage Reduction**: 97.82% → <1% ### Messages Component - **Baseline**: 5.54ms for 500 messages (sequential) - **Optimized**: 2.34ms for 500 messages (parallel) - **Improvement**: 2.4x (cold cache), 2.6x (warm cache) - **Concurrency Scaling**: Near-linear with CPU cores ### Textarea Component - **Strategy**: Adaptive implementation selection - **Threshold**: 1MB file size - **Memory Efficiency**: O(n) → O(log n) for large files ## Technical Implementation ### Diff Component Changes The profiling revealed that syntax highlighting consumed 97.82% of CPU time during diff rendering. I addressed this through: 1. **Batch Processing**: Consolidated per-line highlighting into batch operations 2. **LRU Cache**: Implemented content-addressable caching with FNV hashing 3. **Dynamic ANSI Detection**: Runtime pattern detection for cross-theme compatibility Key modules: - `syntax_cache.go`: High-performance caching layer with configurable size limits - `diff.go`: Batch highlighting coordinator with fallback mechanisms ### Messages Component Changes I implemented a multi-threaded rendering pipeline optimized for modern multi-core processors: 1. **Concurrent Batch Processor**: Work-stealing queue with dynamic batch sizing 2. **Part Cache**: Content-based caching using FNV-1a hashing 3. **Piece Table** VS Code-inspired data structure for O(log n) operations Key modules: - `batch_processor.go`: Concurrent rendering orchestrator - `part_cache.go`: Thread-safe caching implementation - `piece_table.go`: Tree-based text management structure ### Textarea Component Changes Implemented an adaptive strategy that selects the optimal data structure based on content characteristics: - **Original Implementation**: Direct string manipulation for files <1MB - **Rope Implementation**: B-tree based structure for files >1MB - **Automatic Switching**: Transparent to the API consumer ### Message Memory Management Implemented a MessageBroker pattern to prevent terminal crashes with large conversations: #### MessageBroker - Manages message loading and caching between API and UI components - replacing app.Messages - Implements windowed access with configurable window size (default: 1000 messages) - Integrates with existing memory-bounded cache system (500MB limit) - Provides methods: `GetMessages()`, `GetMessage()`, `GetMessageCount()`, `InvalidateCache()` #### Memory Management - Prevents loading all conversation messages into memory simultaneously - Maintains active window of messages based on viewport requirements - Leverages existing LRU cache with memory bounds for rendered content - Automatic cache eviction when memory limits are exceeded #### Integration Updates - Modified sliding window renderer to work with MessageBroker instead of direct message arrays - Updated messages component to use broker for all message access - Maintained backward compatibility with existing rendering pipeline #### Performance - Memory usage bounded regardless of conversation size - Constant memory overhead for message metadata indexing - Efficient batch rendering for visible message ranges ### Sliding Window Viewport Optimization Implemented a sliding window renderer that reduces memory usage and improves rendering performance for large conversations: #### Architecture - **Message Index**: Lightweight metadata structure storing position and height information for O(1) lookups - **Adaptive Window Size**: Dynamically calculated based on viewport height (2.5x visible messages, bounded 20-50) - **Binary Search**: Efficient visible message range detection using cumulative line positions - **Lazy Rendering**: Only renders messages within the active window plus buffer #### Key Components - `MessageMeta` struct: Stores `StartLine`, `Height`, and `ContentHash` for each message - `SlidingWindowRenderer`: Manages window state and coordinates rendering - `findVisibleMessageRange()`: Binary search for viewport intersection - `calculateWindowRange()`: Centers window on visible area with padding #### Performance - **Memory Usage**: Constant regardless of conversation size - **Rendering Time**: Proportional to visible messages only (typically 10-20 messages) - **Scrolling**: Smooth performance through predictive window adjustment - **Cache Integration**: Works with global memory-bounded cache for rendered content #### Memory Efficiency - **Index Size**: ~24 bytes per message for metadata only - **Window Size**: Limited to 50 messages maximum in memory - **Cache Eviction**: Automatic cleanup of off-screen content - **Height Correction**: Real heights update estimated values for accuracy ### Cache Architecture All caching implementations use FNV-1a hashing for content addressing, providing: - O(1) average lookup time - Minimal hash collision probability - Consistent performance across different content types ### Memory Bounds - **Diff Cache**: 100MB limit with LRU eviction - **Message Cache**: 500MB limit with memory-bounded eviction - **Part Cache**: Unbounded (relies on system memory management) ### Concurrency Model - **Messages**: Parallel batch processing with work-stealing queues - **Diff**: Single-threaded with async cache population - **Textarea**: Single-threaded with efficient data structures ## Testing and Validation All optimizations include comprehensive test coverage: - **Benchmark Tests**: Performance regression detection - **Unit Tests**: Correctness verification - **Integration Tests**: End-to-end functionality validation - **Memory Tests**: Resource usage verification
f16de3d to
cc0d460
Compare
772b621 to
eb855e1
Compare
f1dc981 to
3e15a39
Compare
f8ee907 to
6a9856d
Compare
This was referenced Jan 16, 2026
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.
Diff Component
Messages Component
Textarea Component
Technical Implementation
Diff Component Changes
The profiling revealed that syntax highlighting consumed 97.82% of CPU time during diff rendering. I addressed this through:
Key modules:
syntax_cache.go: High-performance caching layer with configurable size limitsdiff.go: Batch highlighting coordinator with fallback mechanismsMessages Component Changes
I implemented a multi-threaded rendering pipeline optimized for modern multi-core processors:
Key modules:
batch_processor.go: Concurrent rendering orchestratorpart_cache.go: Thread-safe caching implementationpiece_table.go: Tree-based text management structureTextarea Component Changes
Implemented an adaptive strategy that selects the optimal data structure based on content characteristics:
Message Memory Management
Implemented a MessageBroker pattern to prevent terminal crashes with large conversations:
MessageBroker
GetMessages(),GetMessage(),GetMessageCount(),InvalidateCache()Memory Management
Integration Updates
Performance
Sliding Window Viewport Optimization
Implemented a sliding window renderer that reduces memory usage and improves rendering performance for large conversations:
Architecture
Key Components
MessageMetastruct: StoresStartLine,Height, andContentHashfor each messageSlidingWindowRenderer: Manages window state and coordinates renderingfindVisibleMessageRange(): Binary search for viewport intersectioncalculateWindowRange(): Centers window on visible area with paddingPerformance
Memory Efficiency
Cache Architecture
All caching implementations use FNV-1a hashing for content addressing, providing:
Memory Bounds
Concurrency Model
Testing and Validation
All optimizations include comprehensive test coverage: