Skip to content

[lexical] Refactor: avoid repeated traversal of dirty subtrees during GC - #46

Closed
etrepum wants to merge 1 commit into
mainfrom
codex/caret-split-performance
Closed

etrepum wants to merge 1 commit into
mainfrom
codex/caret-split-performance

Conversation

@etrepum

@etrepum etrepum commented Sep 28, 2026 •

Copy link
Copy Markdown
Owner

Description

Garbage collection can revisit a detached subtree once for each dirty ancestor. This PR leaves dirty child elements to the outer dirty-element iteration, avoiding repeated descendant walks, and reads child/sibling links directly from the active pending node map.

The skip relies on live Map iteration and deferred node-map deletion. Skipped dirty elements remain available for their own visit, including cleanup of newly created nodes. The comment documents that contract and explains why the skip does not apply to the dirty-leaves path.

The final diff changes only garbage collection and its tests. No public API or intended behavior change.

Test plan

After

node node_modules/vitest/vitest.mjs run --project unit packages/lexical packages/lexical-yjs packages/lexical-table packages/lexical-list --maxWorkers=2

Test Files  344 passed (344)
Tests  5733 passed | 1 skipped (5734)

The two new GC tests check that skipped subtrees are still collected, including newly created nodes. They also pass on main and are safety coverage for the optimization.

Changed-file ESLint and Prettier pass. Browser tests were not run locally.

## Description

Detached-subtree collection repeatedly resolves nodes through the active editor state and can walk dirty descendants more than once. GC now follows the pending node map directly and leaves dirty child elements to the existing outer loop, preserving deferred deletion. Regression tests cover newly created dirty subtrees with different node-creation orders.

The reconciler's structurally unchanged child walk collects text fragments and joins them once, reducing intermediate string allocations while preserving text caches, slot prefixes, and first-text format/style propagation.

No public behavior change is intended. This branch is based directly on main and does not require the caret or selection refactors.

## Test plan

### After

```sh
node node_modules/vitest/vitest.mjs run --project unit --maxWorkers=2
```

```text
 Test Files  345 passed (345)
      Tests  5776 passed | 1 skipped (5777)
```

Source and test TypeScript checks, changed-file ESLint/Prettier, and `git diff --check` pass.

Browser tests were not run in this environment.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@etrepum etrepum closed this Sep 28, 2026
@etrepum etrepum changed the title [lexical] Refactor: reduce redundant work in large-document updates [lexical] Refactor: avoid repeated traversal of dirty subtrees during GC Sep 28, 2026
etrepum pushed a commit that referenced this pull request Oct 31, 2026
…ling the link

## Description

Export wrote no character reference inside a bare URL or `www.` host, or for the letter right before a scheme, so formatting that needed one there was lost. It also wrote markers and code spans right after a URL, which GFM readers (micromark for `@lexical/mdast`, and cmark-gfm) take into the link, so the formatting came back as literal markers inside a link: `**www.x.co**a` and `see https://x.com/a**“x”**`.

Export now writes the reference, and where a URL that GFM readers would link would hold one, or would take in the markers, code span or other raw output after it, it also writes the `.` of `www.` or the `:` after the scheme as a reference (`**www.x.co**a`, `see https://x.com/&meta#97;**“x”**`). No reader then links it, and the text and formats come through in `@lexical/markdown`, `@lexical/mdast` and cmark-gfm. A reference that already lands in the prefix spoils the link by itself, and a URL that a reference before it would start is spoiled the same way. Emails and `mailto:` addresses need nothing: both readers find them in the decoded text and take in no format.

A URL is found as GFM readers start one: `www.` only at the start of a run or after `*_~(`, a scheme after anything but a letter. Raw output keeps its letters and `*_~(` for that scan, so a URL runs on into it as it does for the readers. This also fixes plain text such as `[hello]](https://lexical.dev)` before a link, whose URL GFM readers ran on into the link.

## Test plan

Command: `pnpm exec vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts`

### Before

With this commit's tests and the previous `MarkdownExport.ts`:

```
     × can export "[hello]](https://lexical.dev)[world](https://lexical.dev)" 17ms
     × exports formatting beside a `www.` host, which both importers read back 2ms
     × exports formatting beside an email, whose references readers decode first, which both importers read back 1ms
     × exports formatting beside a URL, which both importers read back 1ms
     × exports formatting beside an `xmpp:` address, whose references readers decode first, which both importers read back 1ms
     × exports formatting beside a `www.` host after `_`, which both importers read back 1ms
     × exports formatting beside a URL with a Unicode host, which both importers read back 1ms
     × exports formatting beside a URL that holds an emoji, which both importers read back 1ms
     × exports formatting beside the start of a `www.` host, which its reference spoils, which both importers read back 1ms
     × exports formatting beside the start of a URL, which its reference spoils, which both importers read back 1ms
     × exports formatting beside the start of `mailto:`, which its reference spoils, which both importers read back 1ms
     × exports formatting beside the start of an email, which both importers read back 1ms
     × exports formatting beside a URL that a reference before it would start, which both importers read back 1ms
     × exports formatting beside `mailto:`, which a reference before it does not change, which both importers read back 1ms
     × exports formatting beside a URL right after a code span, which both importers read back 1ms
     × exports formatting beside an email before a URL, which both importers read back 1ms
     × exports formatting beside a URL that would take in a code span, which both importers read back 1ms
     × exports formatting beside a URL that runs on through formats, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in markers, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in italic, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in markers after `:`, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in a code span, which both importers read back 1ms
     × exports formatting beside a URL that would take in strikethrough, which both importers read back 1ms
     × exports formatting beside a URL that holds a `www.` host, which both importers read back 1ms
     × exports formatting beside a URL that would take in the backslash of an escape at its end, which both importers read back 1ms
     × exports formatting beside a URL that holds an email, which both importers read back 1ms
     × spoils a URL for the reference to a backslash before a marker 1ms
     × spoils a URL for the reference to a tag character before its marker 1ms
⎯⎯⎯⎯⎯⎯ Failed Tests 28 ⎯⎯⎯⎯⎯⎯⎯
 Test Files  1 failed (1)
      Tests  28 failed | 747 passed (775)
```

### After

```
 Test Files  1 passed (1)
      Tests  775 passed (775)
```

The new expectations were also checked by hand against cmark-gfm. On 80,000 random paragraphs of URL fragments and formats, against the previous commit, no export reads back worse in `@lexical/markdown`, `@lexical/mdast` or cmark-gfm, and about 1,700 in 20,000 more read back right in each GFM reader.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 31, 2026
… of times, and leave a URL alone whose prefix holds a custom marker

## Description

Two problems found in the review of the previous round:

- Import searched for the link syntax again after every escaped `[`, and after every code span in a link label, so a line of 100 `\[` before a link ran the search 101 times, and 100 `` [`a` `` before `](u)` ran it 200 times. Each of those searches walks on to the end of the line. The link search now stops at the last `](`, jumps over a run of escaped openers in one step, and starts a link at the real opener its label holds, so such lines take a few searches.
- Export spoiled a bare `www.` host or URL by writing its `.` or `:` as a character reference even when that character was the marker of a custom text-format transformer (a `.` tag, for example), which turned the marker into text and lost the format. Such a URL is now left as it is. When a reference already lands in the URL's prefix, no second one is written.

## Test plan

Command: `pnpm exec vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts`

### Before

New tests with the source of the previous commit:

```
     × writes no reference for the `.` of `www.` that is a format's marker 15ms
     × searches the line for a link a few times with escaped openers 16ms
     × searches the line for a link a few times with code spans before a link 22ms
AssertionError: expected 'www&#46;x.co. or www.y.co' to be 'www.x.co. or www.y.co' // Object.is equality
AssertionError: expected 101 to be less than 5
AssertionError: expected 200 to be less than 5
      Tests  3 failed | 778 passed (781)
```

### After

```
 Test Files  1 passed (1)
      Tests  781 passed (781)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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