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

Changed externref_table to use geometric resizing, giving amortized constant time additions #2294

Merged
merged 1 commit into from
Aug 24, 2020

Conversation

richardlett
Copy link
Contributor

Previously __wbindgen_externref_table_grow was called in alloc adding a constant size each time.

This results in taking Theta(N) time amortized to pass a single new JsValue to wasm, where N is number/size of existing items.

Changes

This pull request resizes geometrically, giving amortized constant additions to the externref_table.

Specifically, max(128, curr_len), so that we the larger of 128*usize, or the size of the current array.

This results in a growth factor of 2, the same as Rust's Vec growth factor

Testing:

It fixes #2291, where it took over 150 seconds to add 1.2 million JsValues's to a Vec on my PC with reference types enabled.
Now, it takes only 0.7-0.9 seconds.


Pros
Reduces complexity from Theta(N^2) to pass N JsValues to wasm to Theta(N)
Cons
Slightly more memory required -- After resizing, the table will be half full.

I'm setting this as draft, since this is my first PR! :)
All tests passed for me. Tell me if I missed anything!

@alexcrichton
Copy link
Contributor

Thanks for the investigation here, and thanks for the PR! This does indeed look like a mistake on my end, 2x resizing is definitely intended here rather than "always add a fixed amount". This solution looks good to me, would you ok landing it or did you want to add any more tests?

@richardlett richardlett marked this pull request as ready for review August 24, 2020 17:24
@richardlett
Copy link
Contributor Author

No problem! I think its all good - Submitted as draft, as I was unfamiliar with the pull request system. 👍

@alexcrichton alexcrichton merged commit 520e2ad into rustwasm:master Aug 24, 2020
@alexcrichton
Copy link
Contributor

Ok no worries, thanks again!

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.

Adding JsValues to Vec<JsValue> takes quadratic time with reference types enabled for first run.
2 participants