-
Notifications
You must be signed in to change notification settings - Fork 85
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
Enable Wasm weak references for automatic garbage collection #694
Conversation
Enable weak references with --weak-refs. Fix garbage collection runtime alias errors due to weak references. Add lint to prevent weak reference alias errors.
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.
Looks good to me! Good idea with the lint, too.
@@ -57,6 +57,7 @@ | |||
"jsdoc-to-markdown": "^7.1.1", | |||
"mocha": "^9.2.0", | |||
"txm": "^8.0.1", | |||
"wasm-opt": "^1.3.0", | |||
"wasm-pack": "0.10.1", |
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.
Since we don't use wasm-pack anymore, I assume this can be removed.
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.
We still invoke wasm-pack
for running the unit tests (it's more complicated to set up that environment apparently).
Other then the two minor points, LGTM! Will accept now already. |
Starting with v0.2.91 "wasm-bindgen does use the TC39 weak references proposal if support is detected. At the time of this writing all major browsers do support it." |
Description of change
Enable Weak References in the Wasm bindings. This ensures that Wasm memory is freed automatically by the garbage collector, otherwise it will continue to grow unless the developer explicitly calls
.free()
on every exported instance or the instance is passed back to a function which takes ownership of it, allowing Rust to free it.Unfortunately
wasm-pack
does not support passing the--weak-refs
flag towasm-bindgen
(issue), so we have to invokecargo
,wasm-bindgen
, andwasm-opt
manually.Another downside is that
wasm-bindgen
currently has a bug where parameters passed by value fail to deregisterFinalizer
handlers. This causes runtime errors from automatic garbage collection trying to free the instance when it is no longer referenced from the Javascript side, sinceWasmRefCell
sees the instance as still borrowed.See: rustwasm/wasm-bindgen#2677
Fortunately this is easy-enough to circumvent by borrowing and cloning owned parameters instead. I added a build-time lint to prevent this problem going forward until the above PR is merged.
Type of change
How the change has been tested
Wasm tests and examples pass locally. I checked the memory usage locally with Chrome dev tools to confirm that the memory is freed by automatic garbage collection now.
Change checklist