Open
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.
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.
When disposed, delete the function from the internal map and add the function id to a "freelist" that will be re-used for the next function created. Only increment ID if no freelist id is available.
Fixes #223