Skip to content

perf(editor): share token TextStyles between highlight segments (#1543) - #1686

Merged
ZhuchkaTriplesix merged 1 commit into
devfrom
perf/syntax-highlight-style-cache
Oct 17, 2026
Merged

ZhuchkaTriplesix merged 1 commit into
devfrom
perf/syntax-highlight-style-cache

Conversation

@ZhuchkaTriplesix

Copy link
Copy Markdown
Member

Closes #1543.

_styleFromSegment ran (base ?? const TextStyle()).copyWith(...) for every segment, and segmentsToTextSpan runs on every keystroke (patched segments while typing, and after each highlight pass). A theme has a handful of token looks, a document hundreds of tokens.

  • Styles are cached by (colour, has-colour, weight, font style), packed into one int, for the current base style. A new base (theme, font size) clears the cache; so does reaching 256 looks, so a pathological document cannot grow it.
  • Segments with the same look now share one TextStyle instance; segments with no colour, weight or style still get null.

Not measured: no editor scenario in the bench isolates span construction. The saving is the copyWith, Color and weight lookup per token, minus a hash lookup. Tests: shared instance for equal looks, base style and attributes preserved, a different base is not served stale styles, 600 distinct looks stay correct past the cache cap.

@github-actions github-actions Bot added enhancement New feature or request editor Code/SQL editor performance Theme parser epic label: performance core Core library logic and services P2 Medium priority / Parity & Refactoring labels Oct 17, 2026
@github-actions github-actions Bot added this to the 0.5.0 milestone Oct 17, 2026
@ZhuchkaTriplesix
ZhuchkaTriplesix merged commit c12e383 into dev Oct 17, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core library logic and services editor Code/SQL editor enhancement New feature or request P2 Medium priority / Parity & Refactoring performance Theme parser epic label: performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant