소스 검색

perf(gtk): cache font metrics

Avoid recalculating Pango font metrics (line height, char width) on every frame. Cache them in GtkEditor after first initialization.
Bernardo Magri 1 개월 전
부모
커밋
30034447d3
2개의 변경된 파일106개의 추가작업 그리고 23개의 파일을 삭제
  1. 82 9
      DEV_STATE.md
  2. 24 14
      src/gtk_editor.cpp

+ 82 - 9
DEV_STATE.md

@@ -21,80 +21,153 @@
 ```
 
 ## Current Module
-**GTK4 Frontend Completion** - Currently in Phase 6 with stable basic functionality. The editor has working text input, cursor movement, basic editing operations, and clean shutdown.
+
+**Phase 7: Performance Optimization** - optimization of rendering pipeline, input handling, and resource usage.
+
+
 
 ## File Manifest
+
 ```
+
 Lumacs/
+
 ├── CMakeLists.txt           # Build configuration
+
 ├── init.lua                 # Main Lua initialization
+
 ├── themes.lua              # Theme definitions
+
 ├── include/lumacs/         # C++ headers
+
 │   ├── editor_core.hpp     # Core editor logic
+
 │   ├── buffer.hpp          # Text buffer management
+
 │   ├── lua_api.hpp         # C++/Lua bridge
+
 │   ├── gtk_editor.hpp      # GTK4 frontend
+
 │   ├── tui_editor.hpp      # TUI fallback
+
 │   ├── keybinding.hpp      # Key handling
+
 │   ├── theme.hpp           # Theme/face system
+
 │   └── [other headers]
+
 ├── src/                    # C++ implementation
+
 │   ├── main.cpp            # Entry point
+
 │   ├── editor_core.cpp     # Core functionality
+
 │   ├── lua_api.cpp         # Lua bindings
+
 │   ├── gtk_editor.cpp      # GTK implementation
+
 │   └── [other .cpp files]
+
 ├── tests/                  # Unit tests
+
 ├── examples/               # Test files and demos
+
 ├── scripts/                # Build/test scripts
+
 └── documentation/          # Project docs
+
 ```
 
+
+
 ## Done
+
 - ✅ **Phase 1-5**: Complete Emacs-like core functionality
+
 - ✅ **Phase 6 Core**: GTK4 frontend with text rendering
+
 - ✅ **Input System**: Full keyboard input with modifiers working
+
 - ✅ **Cursor System**: Emacs-style blinking block cursor with color inversion
+
 - ✅ **Text Editing**: Basic insertion, deletion, movement operations working
+
 - ✅ **Keybinding System**: Fixed conflicts, C-n, C-p, C-f, C-b, C-a, C-e, C-s, C-k working
+
 - ✅ **Clean Exit**: Fixed shutdown crashes and memory issues
+
 - ✅ **Syntax Highlighting**: Face system with Pango rendering
+
 - ✅ **Text-Cursor Alignment**: Fixed text rendering offset issues
+
 - ✅ **Scrolling System**: Full vertical and horizontal scrolling with Page Up/Down support
+
 - ✅ **Kill Ring System**: Complete implementation with push/yank/yank-pop operations
+
 - ✅ **Mark & Region**: Full mark/region system in Buffer (set_mark, deactivate_mark, get_region)
+
 - ✅ **Buffer Management**: Complete buffer switching, listing, and management in EditorCore
+
 - ✅ **Core Emacs Features**: Registers, keyboard macros, rectangles, kill operations
+
 - ✅ **GTK Segfault Fix**: Resolved double-free on exit with proper cleanup handling
+
 - ✅ **GTK Window Splits**: Dynamic container layout with Gtk::Paned matching EditorCore's window tree
+
 - ✅ **Window Split Crash Fix**: Resolved window split crashes and text rendering issues
+
 - ✅ **Multi-Window Text Rendering**: Fixed text rendering in split windows with proper font metrics
+
 - ✅ **Split Window UI Polish**: Fixed modeline display, focus stability, and split ratio calculations
+
 - ✅ **Window Split Freeze Fix**: Resolved GTK signal_realize callback issues causing freezes during splits
+
 - ✅ **Split Window Cursor Fix**: Fixed cursor movement to work in focused window rather than original active window
+
 - ⚠️ **Focus Jumping Partial Fix**: Identified spurious next_window() calls but focus still jumps during typing
 
 - ✅ **Split Ratio Improvement**: Implemented initial split ratio using signal_map with fallback
+
 - ✅ **Modeline**: Implemented detailed Emacs-like modeline with active/inactive states, flags, percentage, and mode display
 
+- ✅ **Minibuffer Polish**: Implemented Tab completion (buffers/files), command history (up/down), and kill-buffer logic
+
+
+
 ## Todo  
-1. **Phase 7**: Performance optimization and tuning
-2. **Phase 8**: Mouse support and advanced UI features
+
+1. **Pango Layout Caching**: Cache Pango layouts to avoid recreation on every frame/keystroke
+
+2. **Render Optimization**: Optimize `draw_window` to only redraw changed lines if possible (or reduce overhead)
+
+3. **Memory Profiling**: Check for memory leaks or excessive allocations
+
+
 
 ## Technical Debt/Notes
+
 - **Lua Bridge**: The lua_api.cpp contains the critical C++/Lua boundary code
+
 - **GTK Threading**: All GTK operations must stay on main thread
+
 - **Memory Management**: Using RAII and smart pointers throughout C++ code
+
 - **Face System**: Themes are fully integrated with Pango text rendering
+
 - **Cursor Implementation**: Blinking timer with 500ms intervals, proper cleanup on exit
+
 - **Scrolling Architecture**: Viewport system with 3-line vertical and 5-column horizontal margins
+
 - **Build System**: CMake-based with proper dependency management
+
 - **Testing**: Unit test framework in place for core components
 
+
+
 ## Current Focus
-**Phase 6 GTK4 Frontend - FULLY COMPLETED**: All functionality implemented including:
-- Text rendering with syntax highlighting
-- Full keyboard input and cursor management  
-- Window splitting with dynamic GTK containers
-- Clean exit without segfaults
-- Ready for Phase 7 performance optimization
+
+**Phase 7 Performance Optimization**:
+
+- Implementing Pango layout caching
+
+- Analyzing rendering performance

+ 24 - 14
src/gtk_editor.cpp

@@ -158,6 +158,10 @@ private:
     // Member variables
     Gtk::DrawingArea* drawing_area_ = nullptr; // For single-window compatibility
     Gtk::Widget* content_widget_ = nullptr; // Will be either drawing_area_ or a split container
+    
+    // Font caching
+    Pango::FontDescription font_desc_;
+    bool font_initialized_ = false;
     double char_width_ = 0;
     double line_height_ = 0;
     double ascent_ = 0;
@@ -472,20 +476,26 @@ protected:
         // Create Pango layout - create context directly from Cairo
         auto layout = Pango::Layout::create(cr);
         
-        // Font configuration
-        Pango::FontDescription font_desc("Monospace 12");
-        layout->set_font_description(font_desc);
-
-        // Get font metrics
-        Pango::FontMetrics metrics = layout->get_context()->get_metrics(font_desc);
-        line_height_ = (double)metrics.get_height() / PANGO_SCALE;
-        ascent_ = (double)metrics.get_ascent() / PANGO_SCALE;
-        
-        // Measure character width (for a single 'm' character)
-        layout->set_text("m");
-        Pango::Rectangle ink_rect, logical_rect;
-        layout->get_pixel_extents(ink_rect, logical_rect);
-        char_width_ = (double)logical_rect.get_width(); // Already in pixels
+        // Font configuration (cached)
+        if (!font_initialized_) {
+            font_desc_ = Pango::FontDescription("Monospace 12");
+            layout->set_font_description(font_desc_);
+
+            // Get font metrics
+            Pango::FontMetrics metrics = layout->get_context()->get_metrics(font_desc_);
+            line_height_ = (double)metrics.get_height() / PANGO_SCALE;
+            ascent_ = (double)metrics.get_ascent() / PANGO_SCALE;
+            
+            // Measure character width (for a single 'm' character)
+            layout->set_text("m");
+            Pango::Rectangle ink_rect, logical_rect;
+            layout->get_pixel_extents(ink_rect, logical_rect);
+            char_width_ = (double)logical_rect.get_width(); // Already in pixels
+            
+            font_initialized_ = true;
+        } else {
+            layout->set_font_description(font_desc_);
+        }
 
         // Update window's viewport size based on actual font metrics and padding
         int content_width_px = width - static_cast<int>(PADDING_LEFT + PADDING_RIGHT);