-
Notifications
You must be signed in to change notification settings - Fork 15
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
WeakCache: delayed finalization of nodes #569
WeakCache: delayed finalization of nodes #569
Conversation
const cache = new WeakCache(); | ||
const count = 1000000; | ||
const keys = []; | ||
|
||
console.time("filling up cache"); |
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.
Comments are in for local testing right now, we can remove those after you had a chance to play with it.
for (let i = 0; i < count; ++i) { | ||
const key = {}; | ||
cache.set(key, String(i)); | ||
keys[i] = key; | ||
} | ||
console.timeEnd("filling up cache"); | ||
console.time("waiting for finalization"); | ||
await waitForCache(cache); |
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.
This makes sense for display reasons, but in the final test we would (probably?) omit this part and just let it happen by itself?
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); | ||
}); |
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.
Added a test for GC to show that this is still garbage-collecting nicely.
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.
Just what I had in mind! Thanks @phryneas.
This would delay the
new WeakRef
andthis.registry.register
calls 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 WeakRef
andregistry.register
take significantly longer.This is from a local run with 100000 nodes (which will probably take even longer in CI here).