[WIP][lexical] Feature: GenMap - A generational copy-on-write NodeMap implementation#7674
Closed
etrepum wants to merge 10 commits into
Closed
[WIP][lexical] Feature: GenMap - A generational copy-on-write NodeMap implementation#7674etrepum wants to merge 10 commits into
etrepum wants to merge 10 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
etrepum
commented
Jul 6, 2025
Comment on lines
-541
to
-543
| for (const [key, node] of editorState._nodeMap) { | ||
| nodeMap.set(key, $cloneWithProperties(node)); | ||
| } |
Collaborator
Author
There was a problem hiding this comment.
I couldn't figure out why it was done this way, there aren't really any tests that exercise this properly. I could only guess that this was a bodge that would be better fixed by updating _cloneNotNeeded accordingly?
| editor._dirtyLeaves = new Set(); | ||
| editor._dirtyElements.clear(); | ||
| } else { | ||
| editor._editorState = editorState; |
Collaborator
Author
There was a problem hiding this comment.
The code didn't do anything at all if there was no active state, this seemed like the right thing to do? Not really sure without good tests either way.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replace the naïve Map implementation of NodeMap with a version optimized for structural sharing when the Map is over a size threshold (>= 1000 entries currently).
GenMap has two "modes" based on the
_mutableflag:_oldfrom the previous_oldand_nursery) or make a Shallow Copy of_nurserybefore changing to mutable mode. This is the default state.Each GenMap contains two maps (both created on demand):
_old- A read-only snapshot of some previous state (equivalent to an old Map based NodeMap), generated by the last collection_nursery- The working set of nodes (or tombstones) that have changed from_old. Tombstones are used to mark deletions of nodes that are still present in_oldbut will be removed at the next collection. Whether or not this is safe to mutate directly is based on the_mutableflag.A GenMap can be cloned from another GenMap. This will mark the previous GenMap as not mutable (so its nursery must be copied before use) and reference its
_oldand_nurserydirectly.Terms
Collection is when the next
_oldis generated. Pseudocode:Shallow Copy is when a copy or the nursery is made (
_oldis not changed). Pseudocode:Notes
This is an optimization because the cost of copying a Map is linear in both memory and time. In large documents, most nodes are not updated on each update cycle, so we are only paying the cost to copy the smaller nursery. Occasionally we pay the full cost to do a collection, but this is amortized since we don't do it often.
Related:
Test plan
All existing suites pass
TODO