Skip to content

Use WeakMap and FinalizationRegistry when available. - #132

Closed
benjamn wants to merge 1 commit into
mainfrom
delete-unreachable-keys
Closed

benjamn wants to merge 1 commit into
mainfrom
delete-unreachable-keys

Conversation

@benjamn

@benjamn benjamn commented Jan 19, 2021

Copy link
Copy Markdown
Owner

Although WeakRef and FinalizationRegistry are not yet supported by all browsers, I was pleasantly surprised to learn that more than 68% of browsers already have support for these new APIs.

Using a FinalizationRegistry is appealing for this library, because it allows deleting keys from the internal LRU cache when they become unreachable, which makes sense because an unreachable key can never be looked up in the cache again. Without this functionality, a naive LRU cache will keep keys alive that could otherwise be garbage collected.

This PR uses a FinalizationRegistry when possible, but also allows the behavior to be configured with the useWeakKeys option for the wrap function, since the additional overhead of the registration and cleanup has a noticeable performance cost (the performance stress test run by npm test now takes ~1325ms instead of ~950ms, on my machine).

@benjamn
benjamn force-pushed the delete-unreachable-keys branch from 8715d17 to b6e5f8b Compare January 19, 2021 20:48
Comment thread src/index.ts
Comment on lines +137 to +171
if (useWeakKeys && key && typeof key === "object") {
let ref = weakRefs!.get(key)!;
if (!ref) {
weakRefs!.set(key, ref = {});
registry!.register(key, ref);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ! coercions here are safe because useWeakKeys implies weakRefs and registry must be defined.

Comment thread src/index.ts
Comment on lines +114 to +151
// Optional WeakMap mapping object keys returned by makeCacheKey to
// empty object references that will be stored in the cache instead of
// the original key object. Undefined/unused if useWeakKeys is false.
// It's tempting to use WeakRef objects instead of empty objects, but
// we never actually need to call .deref(), and using WeakRef here
// noticeably slows down cache performance.
const weakRefs = useWeakKeys
? new WeakMap<object, {}>()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realizing that WeakRef is not necessary was the biggest (pleasant) surprise of this work.

Comment thread tsconfig.json
"declaration": true,
"importHelpers": true,
"lib": ["es2015"],
"lib": ["esnext"],

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change brings in the TypeScript typings for WeakRef and FinalizationRegistry.

@benjamn

benjamn commented Nov 29, 2023

Copy link
Copy Markdown
Owner Author

Closing in favor of making the LRU cache user-configurable (PR #615, thanks @phryneas), which enables the same use cases in a more general way.

@benjamn benjamn closed this Nov 29, 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.

1 participant