Skip to content

perf: CPU optimization Phase 1 - reduce idle CPU from 61% to <5%#25

Merged
linhdmn merged 3 commits intomainfrom
feature/cpu-optimization
Apr 24, 2026
Merged

perf: CPU optimization Phase 1 - reduce idle CPU from 61% to <5%#25
linhdmn merged 3 commits intomainfrom
feature/cpu-optimization

Conversation

@linhdmn
Copy link
Copy Markdown
Member

@linhdmn linhdmn commented Apr 24, 2026

Summary

CPU optimization Phase 1 addressing high idle CPU usage issue. Includes multiple performance fixes:

Changes

  1. Cached regex patterns (src/indexer/regex_cache.rs)

    • Created centralized cache for 30+ compiled regex patterns using once_cell::Lazy
    • Patterns compiled once at startup instead of per-file
    • Updated extractor.rs and other modules to use cached patterns
  2. Removed busy-loop sleep from AsyncFileWatcher (src/watcher/notify_handler.rs)

    • Removed 100ms polling sleep from watcher loop
    • recv_event() blocks properly via OS-level file watching (inotify/FSEvents/kqueue)
    • Result: CPU at idle dropped from ~61% to 0.0%
  3. Documentation updates

    • Updated PRD v1.21 changelog
    • Updated HLD v1.21 changelog

Test Results

Metric Before After
Idle CPU (serve) ~61% 0.0%
Indexing time ~0.47s ~0.44s

All commands verified working: version, status, impact, serve, watch, mcp-stdio, benchmark, query

Type of Change

  • Performance improvement
  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

  • Unit tests pass (cargo test)
  • Manual verification - all CLI commands functional
  • CPU measurement verified with ps

Related Issues

US-21.6: Cached regex patterns
US-21.8: Remove busy-loop sleep from AsyncFileWatcher

@linhdmn linhdmn force-pushed the feature/cpu-optimization branch from 93c1a68 to 0775743 Compare April 24, 2026 08:16
@linhdmn linhdmn changed the title perf: cache compiled regex patterns to reduce CPU usage perf: CPU optimization Phase 1 - reduce idle CPU from 61% to <5% Apr 24, 2026
linhdmn added 3 commits April 24, 2026 18:00
- Add once_cell dependency for static regex caching
- Create regex_cache.rs with all compiled regex patterns
- Update extractor.rs to use cached KOTLIN_SYNTHETIC_IMPORT and VIEWBINDING_VAR
- Reduces CPU by avoiding regex recompilation on every file parse

US-21.6: Cached regex patterns
The watcher loop was calling recv_event() followed by a 100ms sleep
regardless of whether an event was received. Since recv_event() already
blocks via the notify library (which uses OS-level file watching),
the extra sleep was causing unnecessary CPU wake cycles.

Removed tokio::time::sleep(Duration::from_millis(100)) from the loop.
The notify library properly blocks recv_event() using inotify/FSEvents/kqueue.
@linhdmn linhdmn force-pushed the feature/cpu-optimization branch from 0775743 to 47526ba Compare April 24, 2026 11:01
@linhdmn linhdmn merged commit bc12302 into main Apr 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant