-
-
Notifications
You must be signed in to change notification settings - Fork 874
fix: draft relocated base refs after reverse/sort in array-methods plugin #1255
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
Open
spokodev
wants to merge
1
commit into
immerjs:main
Choose a base branch
from
spokodev:fix/array-reorder-base-mutation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the indexOf operation here is expensive, do we need it, or should we just return true in any case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. It is needed, and it is doing more than a perf guard:
indexOfis the only thing that tells a relocated base reference (which we must draft, or a later write reaches the base) apart from any other draftable value the reorder moved into this slot. An assigned value can land here too, and immer's rule is that assigned values are never drafted.I checked it. With
return true, a freshly pushed object gets drafted after areverse(); with theindexOfmembership test it does not:The earlier guards (
allIndicesReassigned_,!assigned_.get(prop),isDraftable,!value[DRAFT_STATE]) narrow it, but they cannot tell base refs from assigned refs, because after a native reverse/sort theassigned_indices no longer line up with the new positions. The base-membership test is what keeps us from drafting values immer promised to leave alone. In my tests the final output was the same either way (an untouched draft finalizes back to its source), so this is about the invariant and about not creating proxies for assigned values, not a visible output bug.On the cost you are right:
indexOfis O(n) per hit, so reading a whole reordered array is O(n^2) worst case. If you prefer, I can build aSetof the base references once whenallIndicesReassigned_is set and test membership against that. That makes it O(1) per hit and O(n) overall with the same behavior. I can push that if you want it.