Skip to content
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

Fix CallbackStack leak on JS #3361

Merged

Conversation

armanbilge
Copy link
Member

Fixes the JS version of #3359, since CallbackStack is specialized on JS.

clearCurrent() is now implemented by invoking the JS delete operator on that array index. This transforms the js.Array into a sparse (aka "holey") array. This does de-optimize the data structure compared to a packed array, but hopefully this is good enough for our purposes (and better than a userland data structure).

For real-world coding patterns, the performance difference between accessing holey or packed arrays is usually too small to matter or even be measurable.

https://v8.dev/blog/elements-kinds#avoid-creating-holes

Copy link
Member

@djspiewak djspiewak left a comment

Choose a reason for hiding this comment

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

This definitely staunches the bleeding, but I'd also be curious as to how this compares to the old LongMap approach, since that's effectively what a sparse array is.

@djspiewak djspiewak merged commit 83412c5 into typelevel:series/3.4.x Jan 15, 2023
@armanbilge
Copy link
Member Author

armanbilge commented Jan 15, 2023

but I'd also be curious as to how this compares to the old LongMap approach, since that's effectively what a sparse array is.

You mean this LongMap?

private[this] val ref = new AtomicReference[State[A]](
State.Unset(LongMap.empty, State.initialId)
)

That's all immutable state there, which means allocations, which means this will blast it out of the water 😛

Edit: also note that JS doesn't natively support 64 bit ints, so anything with Long has lots of overhead.

@armanbilge
Copy link
Member Author

This transforms the js.Array into a sparse (aka "holey") array. This does de-optimize the data structure compared to a packed array, but hopefully this is good enough for our purposes

Tangential, but while investigating this I discovered that Scala.js is (unintentionally) de-optimizing all uses of js.Array in its stdlib/javalib, so performance can't be that bad.

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