Skip to content

fix: preserve removed drafts with optimized array methods - #1349

Open
jipeng6036-del wants to merge 2 commits into
immerjs:mainfrom
jipeng6036-del:fix/array-methods-preserve-removed-drafts
Open

jipeng6036-del wants to merge 2 commits into
immerjs:mainfrom
jipeng6036-del:fix/array-methods-preserve-removed-drafts

Conversation

@jipeng6036-del

Copy link
Copy Markdown

With enableArrayMethods(), a recipe that moves an object between lists and then updates it can mutate the original state, or throw when the base is frozen:

produce({todo: [{done: false}], done: []}, draft => {
  const item = draft.todo.pop()
  draft.done.push(item)
  item.done = true
})

Read only the removed element through the existing draft proxy before optimized pop / shift. Use the normal proxy path for splice, preserving removed drafts, native argument coercion and sparse-array behavior. Newly inserted external objects keep their existing undrafted behavior.

splice consequently has the standard proxy cost; pop / shift retain their internal-copy optimization. The documentation makes that tradeoff explicit.

Validation:

  • 32 regression cases: original source 10 failures / 22 controls pass; candidate all pass, including frozen inputs, patches/undo, prior draft identity, external values, getters and no-op sharing.
  • Windows and Linux: yarn test (4,244 source and 3,693 production tests pass; 8 skips in each), Flow, coverage and all four existing performance scripts passed. The unchanged fork Test workflow also passed on this commit.
  • Fresh consumers installed actual local npm pack artifacts: 102 public CJS/ESM checks pass across Windows/Linux; baseline defects reproduce. These are local artifacts, not a registry release.

Reproduction, exact artifacts, independent checks and size/performance limitations. The size tool needed a size-only OpenSSL compatibility option; the original failed measurements are retained separately.

Prepared and validated with Codex.

@mweststrate mweststrate left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I noticed that the tests test cases like sparse arrays and getters. These kind of non-idiomatic usage of arrays is not supported by Immer, and hence its behavior should not be hitched with tests. Could you drop those?

Pop /shift fixes sound correct.

I suspect with the first note, that the old, more optimized(?) splice implementation could be kept, if you just add drafting the returned res array instead of doing a full unoptimized fallback. If not, this is fine

@jipeng6036-del

Copy link
Copy Markdown
Author

Thanks for the review. I removed the sparse-array and getter tests in 9fe517d.

I kept splice on the regular proxy path. Once splice has shifted the array, the removed values alone don't carry their original positions. Some are base entries that need drafts, while newly inserted objects must retain their identity and stay undrafted. The regular proxy path already handles that distinction and preserves native argument coercion.

I ran the source and production suites and Flow on the PR branch and with current main merged locally. Coverage and the performance suite also ran successfully on the merged tree.

This branch has not been deployed

No deployments
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.

2 participants