Replace manual memory management with FinalizationRegistry#1908
Closed
NickGerleman wants to merge 1 commit into
Closed
Replace manual memory management with FinalizationRegistry#1908NickGerleman wants to merge 1 commit into
NickGerleman wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@NickGerleman has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95014519. |
928508c to
b2b6bae
Compare
NickGerleman
added a commit
to NickGerleman/yoga
that referenced
this pull request
Mar 6, 2026
Summary: Pull Request resolved: react#1908 Yoga's JavaScript API currently requires users to manually call `node.free()`, `node.freeRecursive()`, or `config.free()` to release WASM memory. This is error-prone and unusual for a JavaScript library. This diff uses `FinalizationRegistry` (available in all modern JS engines) to automatically free WASM memory when JS objects are garbage collected, removing this footgun entirely. Changes: - Added `FinalizationRegistry` instances for `Node` and `Config` in `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it is registered with the appropriate registry. The weak reference callback receives the WASM pointer and calls the appropriate C free function. - The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to safely deallocate without disconnecting nodes from their owner/children, since an entire tree may be garbage collected together. - Removed `free()` and `freeRecursive()` from the public `Node` type and `free()` from the public `Config` type. - Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory methods. - Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing `this` directly in the closure stored in the dirtied func Map. The previous `() => dirtiedFunc(this)` pattern created a strong reference from the Map to the Node, preventing GC of detached nodes with a dirtied func set. - Updated `gentest-javascript.js` to stop generating `try`/`finally` cleanup blocks and `let root` declarations in test prologues/epilogues. - Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25 generated test files and 9 hand-written test files. - Re-signed all generated test files with signedsource. - Updated `README.md` to remove manual memory management documentation. Differential Revision: D95014519
NickGerleman
added a commit
to NickGerleman/yoga
that referenced
this pull request
Mar 6, 2026
Summary: Pull Request resolved: react#1908 Yoga's JavaScript API currently requires users to manually call `node.free()`, `node.freeRecursive()`, or `config.free()` to release WASM memory. This is error-prone and unusual for a JavaScript library. This diff uses `FinalizationRegistry` (available in all modern JS engines) to automatically free WASM memory when JS objects are garbage collected, removing this footgun entirely. Changes: - Added `FinalizationRegistry` instances for `Node` and `Config` in `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it is registered with the appropriate registry. The weak reference callback receives the WASM pointer and calls the appropriate C free function. - The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to safely deallocate without disconnecting nodes from their owner/children, since an entire tree may be garbage collected together. - Removed `free()` and `freeRecursive()` from the public `Node` type and `free()` from the public `Config` type. - Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory methods. - Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing `this` directly in the closure stored in the dirtied func Map. The previous `() => dirtiedFunc(this)` pattern created a strong reference from the Map to the Node, preventing GC of detached nodes with a dirtied func set. - Updated `gentest-javascript.js` to stop generating `try`/`finally` cleanup blocks and `let root` declarations in test prologues/epilogues. - Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25 generated test files and 9 hand-written test files. - Re-signed all generated test files with signedsource. - Updated `README.md` to remove manual memory management documentation. Differential Revision: D95014519
b2b6bae to
a869bbe
Compare
NickGerleman
added a commit
to NickGerleman/yoga
that referenced
this pull request
Mar 6, 2026
Summary: Pull Request resolved: react#1908 Yoga's JavaScript API currently requires users to manually call `node.free()`, `node.freeRecursive()`, or `config.free()` to release WASM memory. This is error-prone and unusual for a JavaScript library. This diff uses `FinalizationRegistry` (available in all modern JS engines) to automatically free WASM memory when JS objects are garbage collected, removing this footgun entirely. Changes: - Added `FinalizationRegistry` instances for `Node` and `Config` in `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it is registered with the appropriate registry. The weak reference callback receives the WASM pointer and calls the appropriate C free function. - The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to safely deallocate without disconnecting nodes from their owner/children, since an entire tree may be garbage collected together. - Removed `free()` and `freeRecursive()` from the public `Node` type and `free()` from the public `Config` type. - Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory methods. - Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing `this` directly in the closure stored in the dirtied func Map. The previous `() => dirtiedFunc(this)` pattern created a strong reference from the Map to the Node, preventing GC of detached nodes with a dirtied func set. - Updated `gentest-javascript.js` to stop generating `try`/`finally` cleanup blocks and `let root` declarations in test prologues/epilogues. - Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25 generated test files and 9 hand-written test files. - Re-signed all generated test files with signedsource. - Updated `README.md` to remove manual memory management documentation. Differential Revision: D95014519
a869bbe to
27150eb
Compare
NickGerleman
added a commit
to NickGerleman/yoga
that referenced
this pull request
Mar 6, 2026
Summary: Pull Request resolved: react#1908 Fixes react#1818 Fixes react#1572 Yoga's JavaScript API currently requires users to manually call `node.free()`, `node.freeRecursive()`, or `config.free()` to release WASM memory. This is error-prone and unusual for a JavaScript library. This diff uses `FinalizationRegistry` (available in all modern JS engines) to automatically free WASM memory when JS objects are garbage collected, removing this footgun entirely. Changes: - Added `FinalizationRegistry` instances for `Node` and `Config` in `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it is registered with the appropriate registry. The weak reference callback receives the WASM pointer and calls the appropriate C free function. - The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to safely deallocate without disconnecting nodes from their owner/children, since an entire tree may be garbage collected together. - Removed `free()` and `freeRecursive()` from the public `Node` type and `free()` from the public `Config` type. - Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory methods. - Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing `this` directly in the closure stored in the dirtied func Map. The previous `() => dirtiedFunc(this)` pattern created a strong reference from the Map to the Node, preventing GC of detached nodes with a dirtied func set. - Updated `gentest-javascript.js` to stop generating `try`/`finally` cleanup blocks and `let root` declarations in test prologues/epilogues. - Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25 generated test files and 9 hand-written test files. - Re-signed all generated test files with signedsource. - Updated `README.md` to remove manual memory management documentation. Differential Revision: D95014519
27150eb to
0f928e3
Compare
NickGerleman
added a commit
to NickGerleman/yoga
that referenced
this pull request
Mar 6, 2026
Summary: Pull Request resolved: react#1908 Fixes react#1818 Fixes react#1572 Yoga's JavaScript API currently requires users to manually call `node.free()`, `node.freeRecursive()`, or `config.free()` to release WASM memory. This is error-prone and unusual for a JavaScript library. This diff uses `FinalizationRegistry` (available in all modern JS engines) to automatically free WASM memory when JS objects are garbage collected, removing this footgun entirely. Changes: - Added `FinalizationRegistry` instances for `Node` and `Config` in `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it is registered with the appropriate registry. The weak reference callback receives the WASM pointer and calls the appropriate C free function. - The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to safely deallocate without disconnecting nodes from their owner/children, since an entire tree may be garbage collected together. - Removed `free()` and `freeRecursive()` from the public `Node` type and `free()` from the public `Config` type. - Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory methods. - Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing `this` directly in the closure stored in the dirtied func Map. The previous `() => dirtiedFunc(this)` pattern created a strong reference from the Map to the Node, preventing GC of detached nodes with a dirtied func set. - Updated `gentest-javascript.js` to stop generating `try`/`finally` cleanup blocks and `let root` declarations in test prologues/epilogues. - Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25 generated test files and 9 hand-written test files. - Re-signed all generated test files with signedsource. - Updated `README.md` to remove manual memory management documentation. Reviewed By: cipolleschi Differential Revision: D95014519
0f928e3 to
a070f5e
Compare
Summary: Pull Request resolved: react#1908 Fixes react#1818 Fixes react#1572 Yoga's JavaScript API currently requires users to manually call `node.free()`, `node.freeRecursive()`, or `config.free()` to release WASM memory. This is error-prone and unusual for a JavaScript library. This diff uses `FinalizationRegistry` (available in all modern JS engines) to automatically free WASM memory when JS objects are garbage collected, removing this footgun entirely. Changes: - Added `FinalizationRegistry` instances for `Node` and `Config` in `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it is registered with the appropriate registry. The weak reference callback receives the WASM pointer and calls the appropriate C free function. - The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to safely deallocate without disconnecting nodes from their owner/children, since an entire tree may be garbage collected together. - Removed `free()` and `freeRecursive()` from the public `Node` type and `free()` from the public `Config` type. - Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory methods. - Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing `this` directly in the closure stored in the dirtied func Map. The previous `() => dirtiedFunc(this)` pattern created a strong reference from the Map to the Node, preventing GC of detached nodes with a dirtied func set. - Updated `gentest-javascript.js` to stop generating `try`/`finally` cleanup blocks and `let root` declarations in test prologues/epilogues. - Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25 generated test files and 9 hand-written test files. - Re-signed all generated test files with signedsource. - Updated `README.md` to remove manual memory management documentation. Reviewed By: cipolleschi Differential Revision: D95014519
a070f5e to
f4dd443
Compare
|
This pull request has been merged in e00b766. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Yoga's JavaScript API currently requires users to manually call
node.free(),node.freeRecursive(), orconfig.free()to releaseWASM memory. This is error-prone and unusual for a JavaScript library.
This diff uses
FinalizationRegistry(available in all modern JSengines) to automatically free WASM memory when JS objects are garbage
collected, removing this footgun entirely.
Changes:
FinalizationRegistryinstances forNodeandConfiginwrapAssembly.ts. When aNodeImpl/ConfigImplis constructed, itis registered with the appropriate registry. The weak reference
callback receives the WASM pointer and calls the appropriate C free
function.
YGNodeFinalize(notYGNodeFree) tosafely deallocate without disconnecting nodes from their
owner/children, since an entire tree may be garbage collected
together.
free()andfreeRecursive()from the publicNodetypeand
free()from the publicConfigtype.Yoga.Node.destroy()andYoga.Config.destroy()factorymethods.
setDirtiedFuncto useWeakRef(this)instead of capturingthisdirectly in the closure stored in the dirtied func Map. Theprevious
() => dirtiedFunc(this)pattern created a strong referencefrom the Map to the Node, preventing GC of detached nodes with a
dirtied func set.
gentest-javascript.jsto stop generatingtry/finallycleanup blocks and
let rootdeclarations in test prologues/epilogues.free()/freeRecursive()/config.free()calls from 25generated test files and 9 hand-written test files.
README.mdto remove manual memory management documentation.Differential Revision: D95014519