Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical] Refactor: [RFC] LexicalNode.afterCloneFrom to simplify clone implementation #6505

Merged
merged 3 commits into from
Aug 12, 2024

Conversation

etrepum
Copy link
Collaborator

@etrepum etrepum commented Aug 8, 2024

Description

The code path for cloning lexical nodes is a bit weird with some branching for different node types and no extensibility. We can clean it up by moving that part of the clone process to an instance method.

Unlike #3920 this does not try and make a generic clone method, the boilerplate is still there – but all for good reason. It's at least 2-3x faster to have the boilerplate than to use Object.create, Object.assign, etc. presumably due to JIT de-optimization reasons. Using the constructor in this way (the status quo) also prevents sharing of mutable objects that could happen unintentionally with a shallow clone of all properties.

This changes the $cloneWithProperties function to first call latestNode.constructor.clone(latestNode), then call mutableNode.afterCloneFrom(latestNode) to handle all of the post-construction state updates. This means the clone static method can "simply" call the constructor and any other update to the node can be done in afterCloneFrom in the correct order (super first) rather than have it happen later in a way where users have no visibility or control over the process.

The change is entirely backwards compatible, other than reserving a new name on the API.

Test plan

Will write additional docs and tests if this is deemed to be a worthwhile endeavor.

Copy link

vercel bot commented Aug 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 12, 2024 2:55pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 12, 2024 2:55pm

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 8, 2024
Copy link

github-actions bot commented Aug 8, 2024

size-limit report 📦

Path Size
lexical - cjs 29.39 KB (0%)
lexical - esm 29.17 KB (0%)
@lexical/rich-text - cjs 37.74 KB (0%)
@lexical/rich-text - esm 31.02 KB (0%)
@lexical/plain-text - cjs 36.32 KB (0%)
@lexical/plain-text - esm 28.37 KB (0%)
@lexical/react - cjs 39.65 KB (0%)
@lexical/react - esm 32.47 KB (0%)

Copy link
Member

@zurfyx zurfyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better than the hardcoded version, but now we're saying that everyone has to implement a clone and afterCloneFrom? Because theoretically every Node can be inherited.

Have we considered automating the static clone and declaring the properties to clone via this other method so that users still only have to implement one? Similarly, we'd have to advocate for no constructor props (except for NodeKey), but if this happens it also indirectly fixes the reflection issue we're having on collab.

@etrepum
Copy link
Collaborator Author

etrepum commented Aug 9, 2024

afterCloneFrom is not usually needed, because most of the time there's not a lot of state that isn't already set in the constructor. You can see that it was used very sparingly but all the tests still pass.

Other solutions that boil down to enumerating property names suffer from the problem where you can end up easily sharing state across node versions that you don't want to (arrays, maps, etc.), or you pay the performance penalty of doing a deep clone, or the complexity penalty of having one way to do it most of the time and another way if there is any non-primitive state.

For collab I'm not sure what's best because I haven't read through all of it, but maybe it should be using json to round-trip nodes. Maybe another method or methods could be added for performance reasons if necessary. Note that collab does have the versioning problem that the json serialization codec at least has the convention to solve, although probably ideally toJSON would have a version argument so new clients could force older serializations for backwards compatibility during an upgrade phase.

@zurfyx
Copy link
Member

zurfyx commented Aug 9, 2024

Other solutions that boil down to enumerating property names suffer from the problem where you can end up easily sharing state across node versions that you don't want to (arrays, maps, etc.), or you pay the performance penalty of doing a deep clone

@etrepum I do agree, which is why I was thinking on doubling down on your proposal to potentially drop the use of the constructor to pass parameters and having everything consolidated in one place. I haven't thought deeply about this and as usual, doesn't play great with backward compatibility.

I made a similar proposal when we introduced what is now $applyNodeReplacement. Instead of users being able to freely instantiate Nodes, we can instead make it more framework-like and have full control over the creation which would enable us to do things like some validation ahead of time.

@etrepum
Copy link
Collaborator Author

etrepum commented Aug 9, 2024

I don't think that really solves anything from a collab perspective, although making construction and init two phase (like Obj-C's +alloc -init) might reduce some boilerplate at a large backwards compatibility expense (and possibly perf regression if everything ends up going through getWritable setters). AFAICT the design of lexical-yjs is just fundamentally flawed, it should've always been using an explicit codec like the existing JSON one (or equivalent). Trying to sync all of the enumerable properties and not run through the normal constructor is just a bad solution. It seems like a given that we'll want to make a big API break for collab but I don't think it's as necessary or beneficial to force that on everything else.

Possibly we could have some utility node subclasses that could use slower reflection or deep cloning to make it easier for users' custom nodes to implement all of these protocols without breaking API compatibility, but leave the option open for people to work at the lower level that we have now that trades boilerplate for performance.

zurfyx
zurfyx previously approved these changes Aug 12, 2024
Copy link
Member

@zurfyx zurfyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, why is this called afterCloneFrom instead of afterClone?

@etrepum
Copy link
Collaborator Author

etrepum commented Aug 12, 2024

I called it afterCloneFrom to make it more clear what the argument was for and which instance to use

@etrepum
Copy link
Collaborator Author

etrepum commented Aug 12, 2024

Had to manually fix some conflicts in LexicalNode.test.ts so the review was dismissed

@etrepum etrepum added this pull request to the merge queue Aug 12, 2024
Merged via the queue into facebook:main with commit 43caf29 Aug 12, 2024
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants