immer icon indicating copy to clipboard operation
immer copied to clipboard

feature: Support copy-on-write in TypedArrays

Open Gelio opened this issue 5 years ago • 5 comments

This is an attempt to fix #696

This MR adds support for copy-on-write for TypedArrays (e.g. Uint16Array).

It correctly handles TypedArrays with the same underlying ArrayBuffer, so when copying 2 arrays with the same underlying buffer, the drafts will also have the same underlying buffer.

I was not sure how to handle ProxyTypes and Archtypes, so feel free to suggest another way to handle those. Also, do not hesitate listing things I could have done better 🙂

Limitations

I did not spend enough time to fix those. The known limitations/missing features are:

  1. No support for patches
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray will not return a draft

Possible optimizations

  1. Not copying the array when the value is set to the same number

Testing

Aside from the added automated tests, I did not test this feature extensively.

Gelio avatar Nov 21 '20 19:11 Gelio

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1f703607d8c93a0ed54877bf71c6feab49397a31:

Sandbox Source
Immer sandbox Configuration

codesandbox-ci[bot] avatar Nov 21 '20 19:11 codesandbox-ci[bot]

I have just noticed a mistake: instanceof and ArrayBuffer.isView will not work on the draft.

I'll add a test to ensure that those work and try to fix those

Gelio avatar Nov 22 '20 07:11 Gelio

I have added a few more tests and commits, and changed the approach for what the proxy target should be in the case of typed arrays. I believe it should behave as expected

I'm looking forward to receiving feedback 🙂

Gelio avatar Dec 02 '20 22:12 Gelio

Sorry, currently not having a lot of bandwidth. Since this is quite a meaty change, probably will only review it in January or something, hope that isn't too much of a problem.

mweststrate avatar Dec 07 '20 09:12 mweststrate

@mweststrate Sure, no problem :slightly_smiling_face: Take your time

Gelio avatar Dec 07 '20 09:12 Gelio

Closing as this feature never landed

mweststrate avatar Jan 02 '23 18:01 mweststrate

I don't mind not having this feature in immer. I am wondering what do you mean by saying that this feature never landed. What feature never landed?

Gelio avatar Jan 02 '23 18:01 Gelio

This PR :)

mweststrate avatar Jan 02 '23 19:01 mweststrate

Oh. It is disappointing to see a PR closed because it was not merged without any more justification. Especially that there was no delay and no asks from me at any point. I feel like my time was wasted. https://github.com/immerjs/immer/issues/696 is not closed and still encourages implementing this idea.

Gelio avatar Jan 02 '23 19:01 Gelio

Yeah sorry about that! Currently Immer has 5 million dependent packages on Github, and the idea has 3 upvotes, so it seems hard to justify at this moment to ship the additional code and increased maintenance complexity to everyone. At this moment it'd become a classic example of feature creep I think :)

mweststrate avatar Jan 02 '23 20:01 mweststrate

Sure, makes sense, thanks for a more thorough explanation :+1:

Gelio avatar Jan 02 '23 20:01 Gelio