Skip to content

WeakCache: delayed finalization of nodes - #569

Merged
benjamn merged 1 commit into
introducing-@wry/cachesfrom
pr/cache-delayed-finalization
Nov 10, 2023
Merged

benjamn merged 1 commit into
introducing-@wry/cachesfrom
pr/cache-delayed-finalization

Conversation

@phryneas

@phryneas phryneas commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator

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 count = 1000000;
const keys = [];

console.time("filling up cache");

Copy link
Copy Markdown
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.

}
console.timeEnd("filling up cache");
console.time("waiting for finalization");
await waitForCache(cache);

Copy link
Copy Markdown
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
Copy Markdown
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.

@benjamn benjamn left a comment

Copy link
Copy Markdown
Owner

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