Repository navigation
WeakCache: delayed finalization of nodes - #569
Merged
Merged
Conversation
phryneas
commented
Nov 7, 2023
| const count = 1000000; | ||
| const keys = []; | ||
|
|
||
| console.time("filling up cache"); |
Collaborator
Author
There was a problem hiding this comment.
Comments are in for local testing right now, we can remove those after you had a chance to play with it.
phryneas
commented
Nov 7, 2023
| } | ||
| console.timeEnd("filling up cache"); | ||
| console.time("waiting for finalization"); | ||
| await waitForCache(cache); |
Collaborator
Author
There was a problem hiding this comment.
This makes sense for display reasons, but in the final test we would (probably?) omit this part and just let it happen by itself?
phryneas
commented
Nov 7, 2023
Comment on lines
+56
to
+80
| it("evicts elements that are garbage collected", async function () { | ||
| const cache = new WeakCache(); | ||
|
|
||
| const count = 100; | ||
| const keys: Array<String | null> = []; | ||
| for (let i = 0; i < count; ++i) { | ||
| keys[i] = new String(i); | ||
| cache.set(keys[i], String(i)); | ||
| } | ||
|
|
||
| assert.strictEqual(cache.size, 100); | ||
| await waitForCache(cache); | ||
| assert.strictEqual(cache.size, 100); | ||
|
|
||
| for (let i = 0; i < 50; ++i) { | ||
| keys[i] = null; | ||
| } | ||
|
|
||
| global.gc!(); | ||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| global.gc!(); | ||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
|
|
||
| assert.strictEqual(cache.size, 50); | ||
| }); |
Collaborator
Author
There was a problem hiding this comment.
Added a test for GC to show that this is still garbage-collecting nicely.
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.
This would delay the
new WeakRefandthis.registry.registercalls into a later microtask (that is further chunked into multiple batches if necessary).As you can see from the test output, the basic "adding items to the cache" is very fast, but both
new WeakRefandregistry.registertake significantly longer.This is from a local run with 100000 nodes (which will probably take even longer in CI here).