-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
feat(runtime): Prevent stack overflow while collecting large lists #2248
Conversation
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.
Can you explain what parentPtr
is for? It's not obvious to me reading through the code.
Essentially |
That behavior isn't used by |
The internal |
I made that change, that simplifies the pointer handling semantics quite a bit. |
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.
Nice.
Co-authored-by: Oscar Spencer <[email protected]>
Summary
This pr prevents stack overflows during the collection of large and extremely large lists.
Notes
decRefChildren
Problem
While working on #2247 I noticed that
decRef
was overflowing the stack with lists that were only around1350
elements.Solution (For those interested)
The reason for this is that
decRef
anddecRefChildren
were not tco optimized, so the two options were to either handle lists separately or find a way to optimizedecRef
for tail calls. I decided on the second approach as it is more generic and provides improvements for other data structures as well.The way I went about this was with a slightly cursed but valid modification where we
free
the parent before the children, this only works because we immediatelydecRef
the children and when wefree
we don't zero the memory but instead just merge the blocks which will leave our child pointers unaffected. When freeing children we free most of the children in a loop and then free the final child outside of the loop in the tail position ofdecRefChildren
allowing for the tco optimization. Initially this bloated our smallest program size by 100 bytes but I noticed that we are doing effectively the same operations for all dataTypes so I combined everything into a shared helper function that actually reduced our bundle size by47
bytes.Why not handle lists separately
Handling lists separately would have certainly been easier and if we are not up for the changes I made here it is still a viable approach however, it would add overhead to every ADT decRef and likely result in a hundred or so additional bytes being added to our smallest program size.