mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
perf: optimize CodeMirror decoration fields to avoid unnecessary rebuilds
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
1b61ec35cc
commit
4fb4e3db04
2 changed files with 11 additions and 4 deletions
3
.jules/bolt.md
Normal file
3
.jules/bolt.md
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
## 2024-11-20 - [Avoid unnecessary CodeMirror rebuilds]
|
||||||
|
**Learning:** Highlight and Sparkline decorators in CodeMirror trigger `buildDecorations` on every transaction by default, which gets slow as document size increases (e.g. 50+ lines). It re-runs the lexer across the whole document on every keystroke if we do not check `tr.docChanged`/etc.
|
||||||
|
**Action:** Use `tr.docChanged` to selectively run `buildDecorations` or just use `value.map(tr.changes)` when only the text changed and results haven't been processed yet.
|
||||||
|
|
@ -65,8 +65,11 @@ function buildDecorations(state: EditorState): DecorationSet {
|
||||||
|
|
||||||
const highlightField = StateField.define<DecorationSet>({
|
const highlightField = StateField.define<DecorationSet>({
|
||||||
create: buildDecorations,
|
create: buildDecorations,
|
||||||
update(_value, tr) {
|
update(value, tr) {
|
||||||
return buildDecorations(tr.state);
|
const hasResults = tr.effects.some((e) => e.is(setResults));
|
||||||
|
if (hasResults) return buildDecorations(tr.state);
|
||||||
|
if (tr.docChanged) return value.map(tr.changes);
|
||||||
|
return value;
|
||||||
},
|
},
|
||||||
provide: (f) => EditorView.decorations.from(f),
|
provide: (f) => EditorView.decorations.from(f),
|
||||||
});
|
});
|
||||||
|
|
@ -98,8 +101,9 @@ function buildVarHighlight(state: EditorState): DecorationSet {
|
||||||
|
|
||||||
const varHighlightField = StateField.define<DecorationSet>({
|
const varHighlightField = StateField.define<DecorationSet>({
|
||||||
create: buildVarHighlight,
|
create: buildVarHighlight,
|
||||||
update(_value, tr) {
|
update(value, tr) {
|
||||||
return buildVarHighlight(tr.state);
|
if (tr.docChanged || tr.selection) return buildVarHighlight(tr.state);
|
||||||
|
return value;
|
||||||
},
|
},
|
||||||
provide: (f) => EditorView.decorations.from(f),
|
provide: (f) => EditorView.decorations.from(f),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue