Repository navigation
Conversation
benjamn
force-pushed
the
delete-unreachable-keys
branch
from
January 19, 2021 20:48
8715d17 to
b6e5f8b
Compare
benjamn
commented
Jan 19, 2021
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); |
Owner
Author
There was a problem hiding this comment.
The ! coercions here are safe because useWeakKeys implies weakRefs and registry must be defined.
benjamn
commented
Jan 19, 2021
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, {}>() |
Owner
Author
There was a problem hiding this comment.
Realizing that WeakRef is not necessary was the biggest (pleasant) surprise of this work.
benjamn
commented
Jan 19, 2021
| "declaration": true, | ||
| "importHelpers": true, | ||
| "lib": ["es2015"], | ||
| "lib": ["esnext"], |
Owner
Author
There was a problem hiding this comment.
This change brings in the TypeScript typings for WeakRef and FinalizationRegistry.
benjamn
force-pushed
the
delete-unreachable-keys
branch
from
March 10, 2021 01:24
b6e5f8b to
e1de530
Compare
1 of 2 tasks
benjamn
force-pushed
the
delete-unreachable-keys
branch
from
May 8, 2021 22:25
e1de530 to
995cff9
Compare
Owner
Author
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.
Although
WeakRefandFinalizationRegistryare 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
FinalizationRegistryis 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
FinalizationRegistrywhen possible, but also allows the behavior to be configured with theuseWeakKeysoption for thewrapfunction, since the additional overhead of the registration and cleanup has a noticeable performance cost (the performance stress test run bynpm testnow takes ~1325ms instead of ~950ms, on my machine).