Skip to content
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

Merged
merged 1 commit into from
Nov 10, 2023

Conversation

phryneas
Copy link
Collaborator

@phryneas phryneas commented Nov 7, 2023

This would delay the new WeakRef and this.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 and registry.register take significantly longer.

This is from a local run with 100000 nodes (which will probably take even longer in CI here).

filling up cache: 403.389ms
finalizing a chunk: 8.936ms
finalizing a chunk: 14.83ms
finalizing a chunk: 8.447ms
finalizing a chunk: 16.229ms
finalizing a chunk: 15.989ms
finalizing a chunk: 11.666ms
finalizing a chunk: 21.831ms
[...snip...]
finalizing a chunk: 11.245ms
finalizing a chunk: 16.684ms
finalizing a chunk: 58.023ms
finalizing a chunk: 30.567ms
finalizing a chunk: 27.232ms
finalizing a chunk: 63.833ms
finalizing a chunk: 26.154ms
finalizing a chunk: 17.152ms
finalizing a chunk: 50.656ms
finalizing a chunk: 118.573ms
waiting for finalization: 1.822s

const cache = new WeakCache();
const count = 1000000;
const keys = [];

console.time("filling up cache");
Copy link
Collaborator Author

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);
Copy link
Collaborator Author

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?

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);
});
Copy link
Collaborator Author

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.

Copy link
Owner

@benjamn benjamn left a 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.

@benjamn benjamn merged commit dced7eb into introducing-@wry/caches Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants