Skip to content

[OVERKILL (DO NOT MERGE)]perf: lazy color index rebuild for large codebases - #8

Open
lmn451 wants to merge 13 commits into
masterfrom
perf/lazy-color-index-rebuild
Open

[OVERKILL (DO NOT MERGE)]perf: lazy color index rebuild for large codebases#8
lmn451 wants to merge 13 commits into
masterfrom
perf/lazy-color-index-rebuild

Conversation

@lmn451

@lmn451 lmn451 commented Mar 20, 2026

Copy link
Copy Markdown
Owner

Summary

Replaces immediate full rebuild on every document change with a lazy rebuild pattern using a dirty flag.

Motivation: For very large codebases (100k+ CSS variables), frequent rebuilds could impact performance.

Changes:

  • Add flag
  • Add - O(1) set
  • Add - rebuild if dirty
  • Update lsp_server to use lazy pattern

Benchmark (100k vars with chains):

Note: For typical projects (<1000 vars), rebuild time is ~2ms which is imperceptible. This optimization mainly helps in edge cases with many variables and rapid document changes.

Tests added:

    • benchmarks 100k var rebuild
    • proves rebuild count reduction
    • integration test

Closes #TODO

lmn451 added 13 commits March 18, 2026 11:13
Extract flag resolution logic into dedicated helper functions in new
flags.rs module. This reduces boilerplate per flag from ~20 lines to
~3 lines, making it easier to add new feature flags.

Changes:
- Add src/flags.rs with reusable flag_bool, flag_bool_simple, flag_opt,
  flag_enum helpers
- Refactor runtime_config.rs to use the new helpers
- Pass runtime_config to code_actions_for_replaceable_literal_colors
- Add --no-suggest-add-fallback and --no-suggest-exact-color-variables
  flags for controlling code action suggestions
Document all available feature flags in:
- CHANGELOG.md: Add Unreleased section with new flags
- AGENTS.md: Add flags.rs to source layout and expand configuration
  management section with flag architecture and full flag table
- README.md: Add Configuration section with flag table and examples
…efinitions

Filters out replacement suggestions where the literal color is already
part of the variable's own value range. This prevents suggesting
replacing #ffd166 with --accent-2 when editing: --accent-2: #ffd166;
- Change literal_colors from HashMap<Uri, Vec<>> to HashMap<Uri, HashMap<u32, Vec<>>>
- Add get_literal_colors_at_position() for O(1) line lookup
- Update literal_color_under_cursor() to use position-based lookup
- get_document_literal_colors() still returns flattened vec for compatibility

Lookup complexity: O(n) -> O(1) hash + O(k) where k = colors on line
- Add color_occurrence_uris index to track documents with literal colors
- Modify revalidate_affected_documents to optionally include color documents
- Use selective revalidation on did_change instead of validating all docs
- This improves performance in large workspaces with many open files
Replaces immediate full rebuild on every document change with
lazy rebuild pattern using dirty flag.

Changes:
- Add color_variables_dirty AtomicBool flag
- Add mark_color_index_dirty() - O(1) set flag
- Add ensure_color_index_valid() - rebuild if dirty
- Update lsp_server to use lazy pattern

Benchmark (100k vars with chains):
- Rebuild time: ~479ms (unchanged)
- Rebuild count: reduced from O(N) to O(1) per validation cycle

Note: For typical projects with <1000 vars, rebuild time is ~2ms
which is imperceptible. This optimization mainly helps in
large codebases with many variables and rapid document changes.

Tests:
- test_color_index_rebuild_performance_100k_vars
- test_lazy_rebuild_batches_multiple_changes
- test_lazy_rebuild_reduces_rebuild_count
- test_lazy_rebuild_batches_document_changes
@lmn451 lmn451 changed the title perf: lazy color index rebuild for large codebases [OVERKILL (DO NOT MERGE)]perf: lazy color index rebuild for large codebases Mar 20, 2026
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