Repository navigation
[lexical] Perf: Skip rewriting an unchanged contentEditable on slot containers - #9743
Conversation
…ontainers
## Description
`$applySlotEditable` runs on every mount and every reconcile of a slot host. For a slot container inside a non-editable host (for example a decorator host), it calls `$markSlotEditable`, which currently assigns `element.contentEditable` unconditionally, even when the container already has that value.
That write is not free. In Chrome, **any write of `contenteditable` to a connected element forces a synchronous style and layout update, even when the value is unchanged** (writing another attribute does not). So every reconcile that touches a slot host after other DOM work pays a forced style and layout of the whole document in the middle of the commit, and later writes in the same commit dirty it again.
This change makes `$markSlotEditable` write `contentEditable` only when the `contenteditable` attribute differs from the target value. It compares with `getAttribute('contenteditable')` rather than the `contentEditable` getter, because the getter returns `'inherit'` when the attribute is absent and would never match. An editable toggle still reaches every container, since the value does change then.
### Measurement
On a page with a 3,014-block document (Chrome, 1440x900), after dirtying one style:
| write on a connected slot container | time |
| --- | --- |
| `el.contentEditable = sameValue` | 93–132 ms |
| native `el.setAttribute('contenteditable', sameValue)` | 91–117 ms |
| native `el.setAttribute('data-x', ...)` | 0–0.1 ms |
| reading `el.contentEditable` / `el.isContentEditable` | 0 ms |
With this patch applied to that app's first reconcile after load (4 slot containers, 12 `$markSlotEditable` calls; median of 5 alternating loads per arm), time inside `$markSlotEditable` dropped from 64 ms to 7 ms and the editor build frame shrank by 12–29 ms. The frame saves less than the full 57 ms because most of the forced style work is the document's first style pass, which the next layout read or paint pays anyway; the patch removes the repeated part.
### Repro (any page with a large DOM)
```js
const frame = () => new Promise(r => requestAnimationFrame(() => setTimeout(r)));
const el = document.querySelector('[contenteditable]'); // any connected element
await frame();
document.body.style.setProperty('--probe', Math.random()); // dirty style
let t = performance.now();
el.setAttribute('contenteditable', el.getAttribute('contenteditable'));
console.log('same-value contenteditable write', performance.now() - t, 'ms');
```
## Changeset
`.changeset/lexical-slot-contenteditable-unchanged.md`: `[perf]` patch for `lexical`.
## Test plan
New unit test `re-reconciling a decorator-host slot leaves an unchanged contentEditable alone` in `LexicalSlot.test.ts` counts `contentEditable` writes on slot containers: one at mount, none when the host and its slot reconcile again, and one more after `setEditable(false)`.
```
node_modules/.bin/vitest run packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'leaves an unchanged contentEditable alone'
```
### Before
With the test but without the `LexicalUtils.ts` change (list of 124 skipped tests trimmed):
```
❯ |unit| packages/lexical/src/__tests__/unit/LexicalSlot.test.ts (125 tests | 1 failed | 124 skipped) 152ms
❯ named-slots: core foundation (63)
× re-reconciling a decorator-host slot leaves an unchanged contentEditable alone 149ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
FAIL |unit| packages/lexical/src/__tests__/unit/LexicalSlot.test.ts > named-slots: core foundation > re-reconciling a decorator-host slot leaves an unchanged contentEditable alone
AssertionError: expected [ 'true', 'true' ] to deeply equal [ 'true' ]
- Expected
+ Received
[
"true",
+ "true",
]
❯ packages/lexical/src/__tests__/unit/LexicalSlot.test.ts:1177:22
1175| );
1176| expect(slotContainer.textContent).toBe('After');
1177| expect(writes).toEqual(['true']);
| ^
1178|
1179| // An editable toggle still reaches the container.
Test Files 1 failed (1)
Tests 1 failed | 124 skipped (125)
```
### After
```
✓ |unit| packages/lexical/src/__tests__/unit/LexicalSlot.test.ts (125 tests | 124 skipped) 142ms
Test Files 1 passed (1)
Tests 1 passed | 124 skipped (125)
```
The slot, DOM slot, slot selection and editor listener suites all pass:
```
node_modules/.bin/vitest run packages/lexical/src/__tests__/unit/LexicalSlot.test.ts packages/lexical/src/__tests__/unit/LexicalDOMSlot.test.ts packages/lexical/src/__tests__/unit/LexicalSlotSelection.test.ts packages/lexical/src/__tests__/unit/LexicalEditorListener.test.ts
Test Files 4 passed (4)
Tests 200 passed (200)
```
`tsc --noEmit`, ESLint, Prettier and `check-changesets` are clean. Only jsdom was exercised here; the Chrome timing above was measured in a real browser.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks, this looks good. Skipping the no-op contentEditable write in $markSlotEditable leaves the same DOM state for every caller, and setEditable toggles still reach the slot containers through $fullReconcile. The new LexicalSlot.test.ts case fails without the LexicalUtils.ts change and passes with it. Comparing against getAttribute('contenteditable') instead of the getter is the right call, because the getter returns 'inherit' for a container with no attribute.
One optional, non-blocking suggestion:
- Describe the cost as a style recalculation (
packages/lexical/src/LexicalUtils.ts:3480-3481and.changeset/lexical-slot-contenteditable-unchanged.md). Both say the write forces a synchronous "style and layout" update. In headless Chromium 149, on a 3,000-paragraph page with style and layout both dirty, a same-value write cost about the same as agetComputedStyleread (8.8 ms vs 8.7 ms) and well under anoffsetHeightread (20.8 ms). With only layout dirty, it cost 0.1 ms. So the write seems to flush pending style work rather than layout, and its cost depends on how much style is dirty. The changeset becomes the CHANGELOG entry (CONTRIBUTING.md, "Changesets"), so wording like "forced a synchronous style recalculation" would be more accurate. The speedup itself reproduces: with style dirty across the document, the write costs 9.1 ms per container without the guard and 0 ms with it.
| // In Chrome, any write of `contenteditable` to a connected element forces a | ||
| // synchronous style and layout update, even when the value is unchanged. |
There was a problem hiding this comment.
Optional nit: in headless Chromium 149, with style and layout both dirty, a same-value write here cost about the same as a getComputedStyle read (8.8 ms vs 8.7 ms) and well under an offsetHeight read (20.8 ms). That points to a forced style recalculation rather than layout. Maybe:
// In Chrome, any write of `contenteditable` to a connected element forces a
// synchronous style recalculation, even when the value is unchanged.The same wording would fit the changeset entry.
…ced style recalculation (review item)
## Description
Follow-up to the review on this pull request. The comment in `$markSlotEditable`, the comment in its test and the changeset entry said that a same-value `contenteditable` write forces a synchronous style **and layout** update. The review measured in headless Chromium that, with style and layout both dirty, the write costs about as much as a `getComputedStyle` read (8.8 ms vs 8.7 ms) and well under an `offsetHeight` read (20.8 ms), and 0.1 ms with only layout dirty. So the write flushes pending style work, not layout. All three now say "style recalculation". No code change.
## Test plan
### After
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts
Test Files 1 passed (1)
Tests 125 passed (125)
```
Prettier, ESLint and `check-changesets` are clean on the changed files.
c03cad2 to
79ab884
Compare
There was a problem hiding this comment.
This looks good to me. The guard in $markSlotEditable (packages/lexical/src/LexicalUtils.ts:3484-3486) skips the write only when the contenteditable attribute already holds the target value, so the container ends in the same state for every caller, and setEditable toggles still reach it. The new LexicalSlot.test.ts case fails against the previous implementation (['true', 'true'] written) and passes with this change, and the cost described in the revised comment reproduces in Chromium: the same-value write tracks pending style work (about 18 ms on a 3,000-paragraph page with style dirty, 0.3 ms with only layout dirty), while the guarded path costs nothing.
One optional, non-blocking suggestion:
- Changeset scope (
.changeset/lexical-slot-contenteditable-unchanged.md:5): the summary names decorator hosts, but the skip lives in the exported$markSlotEditable, so it also applies to slot containers inside acontentEditable=falseelement shell and to apps that call it fromupdateDOM, aspackages/lexical-website/docs/concepts/named-slots.mdrecommends. CONTRIBUTING.md asks for the entry to be written for someone who uses Lexical, so naming the function could help those callers, for example:[perf] $markSlotEditable no longer rewrites an unchanged contentEditable, so reconciling a slot host inside a non-editable host no longer forces a synchronous style recalculation in Chrome.
| 'lexical': patch | ||
| --- | ||
|
|
||
| [perf] Reconciling a decorator host with named slots no longer rewrites an unchanged `contentEditable` on its slot containers, which in Chrome forced a synchronous style recalculation on every such reconcile |
There was a problem hiding this comment.
Optional: the skip lives in the exported $markSlotEditable, so it also covers slot containers in a contentEditable=false element shell and apps that call it from updateDOM (as the named-slots docs recommend). Naming the function would reach those callers, e.g. [perf] $markSlotEditable no longer rewrites an unchanged contentEditable, so reconciling a slot host inside a non-editable host no longer forces a synchronous style recalculation in Chrome.
Description
$applySlotEditableruns on every mount and every reconcile of a slot host. For a slot container inside a non-editable host (for example a decorator host), it calls$markSlotEditable, which assignedelement.contentEditableunconditionally, even when the container already had that value.That write is not free. In Chrome, any write of
contenteditableto a connected element forces a synchronous style recalculation, even when the value is unchanged (writing another attribute does not). So every reconcile that touches a slot host after other DOM work pays a forced style pass in the middle of the commit, and later writes in the same commit dirty style again.$markSlotEditablenow writescontentEditableonly when thecontenteditableattribute differs from the target value. It compares withgetAttribute('contenteditable')rather than thecontentEditablegetter, because the getter returns'inherit'when the attribute is absent and would never match. An editable toggle still reaches every container (setEditablere-applies through the same function, and the value changes then). The DOM ends in the same state for every caller.Measurement
On a page with a 3,014-block document (Chrome, 1440x900), after dirtying one style:
el.contentEditable = sameValueel.setAttribute('contenteditable', sameValue)el.setAttribute('data-x', ...)el.contentEditable/el.isContentEditableWith this patch applied to that app's first reconcile after load (4 slot containers, 12
$markSlotEditablecalls; median of 5 alternating loads per arm), time inside$markSlotEditabledropped from 64 ms to 7 ms and the editor build frame shrank by 12–29 ms. The frame saves less than the full 57 ms because most of the forced style work is the document's first style pass, which the next layout read or paint pays anyway; the patch removes the repeated part.The review on this PR measured the same thing in headless Chromium on a 3,000-paragraph page: with style dirty across the document, the write costs 9.1 ms per container without the guard and 0 ms with it; with only layout dirty it costs 0.1 ms, which is why this says "style recalculation" rather than "style and layout" (second commit).
Repro (any page with a large DOM)
Changeset
.changeset/lexical-slot-contenteditable-unchanged.md(lexicalpatch,[perf]; noVerify:paragraph, since the DOM ends in the same state).Test plan
New
LexicalSlot.test.tscase,re-reconciling a decorator-host slot leaves an unchanged contentEditable alone(lexical). It wraps thecontentEditablesetter and counts writes on slot containers:'true'once;setEditable(false)writes'false'once, so a toggle still reaches the container.Before
main (2b388b9), with the new test:
After
All unit tests (
vitest --run --project unit): 29,149 passed, 373 expected fail, 136 skipped, 0 failed. The 4lexical-devtoolstest files did not load locally because their generated.wxt/tsconfig.jsonis missing (WXT'spreparestep crashes in the environment I ran this in); they do not touchlexical, and CI'score-tests / unitjob runs them.The
ci-checksteps pass:tsc,tsc-test,tsc-scripts,tsc-website,flow,prettier,lintandcheck-changesets.tsc-extensionneeds the same WXT-generated types and could not run locally.Only jsdom was exercised locally. The Chrome cost is measured in real Chrome (above). There is no browser-mode or e2e test for it: vitest browser mode cannot observe a forced style pass deterministically, and the behavior this changes (which writes happen) is covered at the unit level.
Related (not changed here)
$applySlotEditable's other branch removescontenteditablefrom a container in an editable host. Removing an absent attribute is already a no-op in the DOM, so it needs no guard.contentEditable = 'false'writes in core (a decorator'screateDOMin the reconciler, and the block cursor element) set it on an element that was just created and is not connected yet, so they force no style pass.