refactor(internals): iterative mountAtom (scale)#3281
refactor(internals): iterative mountAtom (scale)#3281dmaskasky wants to merge 2 commits intopmndrs:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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. |
commit: |
|
| Playground | Link |
|---|---|
| React demo | https://livecodes.io?x=id/3BDGXU74B |
See documentations for usage instructions.
8de8cc5 to
c46c396
Compare
dai-shi
left a comment
There was a problem hiding this comment.
Do you think you can create a failing test case with recursive mountAtom?
| const atomStack: AnyAtom[] = [atom] | ||
| const stateStack: (AtomState | undefined)[] = [undefined] |
There was a problem hiding this comment.
Have you tried const stack: [AnyAtom, AtomState][] and compare the performance?
There was a problem hiding this comment.
Yeah, its worse because of the added overhead of creating new arrays on every iteration.
There was a problem hiding this comment.
Oh, really... That means push/pop are more lightweight, correct?
There was a problem hiding this comment.
Parallel arrays are faster because they avoid allocating a tiny tuple array for every entry.
With tuple arrays, each push does all of this:
- allocate a new
[a, aState]array - write its two elements
- push a reference to that tuple into the outer stack
Then each pop does this:
- pop the tuple reference
- dereference that tuple object
- read element
0and element1 - later let GC reclaim that tuple
With parallel arrays, each entry is just:
- push
aintoaStack - push
aStateintostateStack
and later:
- pop from
aStack - pop from
stateStack
So parallel arrays usually win because they have:
- no per-entry object allocation
- less garbage collection
- less pointer indirection
- better memory locality
The extra push/pop calls are usually cheaper than creating and later collecting a fresh tuple object for every item.
Summary
Refactors mountAtom to use an iterative solution.
Check List
pnpm run fixfor formatting and linting code and docs