Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR cleans up function entries upon disposal by removing them from the internal map and reusing freed function IDs via a freelist instead of always incrementing the counter.
- Extended
heapValueHandleto accept an extra disposal callback. - Updated
newFunctionto pull IDs fromfnIdFreelistand schedulefreeFunctionon handle disposal. - Introduced
fnIdFreeliststorage andfreeFunctionmethod for recycling IDs.
Comments suppressed due to low confidence (2)
packages/quickjs-emscripten-core/src/context.ts:1331
- Add a JSDoc comment for
fnIdFreelistto explain its role in recycling function IDs upon disposal.
protected fnIdFreelist: number[] = []
packages/quickjs-emscripten-core/src/context.ts:608
- Add unit tests to verify that IDs from
fnIdFreelistare correctly reused when disposing and creating new functions.
const fnId = this.fnIdFreelist.pop() ?? ++this.fnNextId
| } | ||
|
|
||
| protected freeFunction(fn_id: number) { | ||
| const map_id = fn_id >> 8 |
There was a problem hiding this comment.
Extract the magic number 8 used in the bit shift into a named constant (e.g., ID_MAP_SHIFT_BITS) to clarify intent.
Suggested change
| const map_id = fn_id >> 8 | |
| const map_id = fn_id >> QuickJSContext.ID_MAP_SHIFT_BITS |
| protected freeFunction(fn_id: number) { | ||
| const map_id = fn_id >> 8 | ||
| const fnMap = this.fnMaps.get(map_id) | ||
| if (!fnMap) { |
There was a problem hiding this comment.
After deleting the last function in a map, consider removing the empty fnMap entry from fnMaps to avoid retaining unused maps.
a0e1f38 to
17a585b
Compare
d8d5c91 to
f12b5e9
Compare
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.
newFunctionto use a different proxying strategy based on a new abstraction,HostRef. HostRef allows tracking references of host values from guest handles. Once all references to the HostRef object are disposed (either in the host, or GC'd in the guest), the host value will be dereferenced and become garbage collectable.newFunctionare interned in the runtime'sHostRefMapuntil dereferenced.HostRefis also exposed directly for use fromQuickJSContext, seenewHostRef,toHostRef,unwrapHostRefin the docs.QuickJSContext.newConstructorFunction, such functions will have theirthisset to the newly constructed object, although they may also return a new object.Fixes #223