Skip to content

Commit 5962cc9

Browse files
committed
perf(runtime): weak collections look a value up as is before unwrapping it
Every WeakMap, WeakSet and FinalizationRegistry.unregister call with an object asked native code for the object's lifetime key, so plain JS keys and wrappers that are not canaries paid for a native call even when they were present. A canary is never stored as a key itself (its lifetime key is), so each method now tries the value as is and asks for the lifetime key only when that misses, and looks again only when the lifetime key is another object. set, add and getOrInsert check the receiver with has(value), which also skips the unwrap when the value is already present. Measured on a phone with the reference implementation, a hit: get with a plain JS key 38 -> 17 ns (16 ns in resurrecting mode), with a wrapper that is not a canary under scope "extended" 111 -> 16 ns; canary keys cost 2-5 ns more. A miss still makes the native call.
1 parent 7c95324 commit 5962cc9

1 file changed

Lines changed: 53 additions & 16 deletions

File tree

‎test-app/runtime/src/main/cpp/js/weak-collections.js‎

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,64 @@ function canBeHeldWeakly(value) {
4949
// once the native method can no longer reject the call, so a failed call leaves
5050
// no state behind: the receiver is checked first with a method that cannot
5151
// insert anything.
52-
function lookupKey(value) {
53-
return isObject(value) ? unwrap(value, false) : value;
52+
//
53+
// A canary is never stored as a key itself (its lifetime key is), so every
54+
// method first tries the value as is and unwraps it only when that misses:
55+
// plain JS keys never reach native code.
56+
57+
// The lifetime key value stands for, or undefined when that is value itself.
58+
function otherKey(value) {
59+
if (!isObject(value)) {
60+
return undefined;
61+
}
62+
const key = unwrap(value, false);
63+
return key !== value ? key : undefined;
5464
}
5565

5666
// Unregistering it finds nothing: it is never a token.
5767
const receiverProbe = ObjectFreeze({ __proto__: null });
5868

5969
class WeakMap extends NativeWeakMap {
6070
get(key) {
61-
return WeakMapPrototypeGet(this, lookupKey(key));
71+
const direct = WeakMapPrototypeGet(this, key);
72+
if (direct !== undefined) {
73+
return direct;
74+
}
75+
const other = otherKey(key);
76+
return other === undefined ? undefined : WeakMapPrototypeGet(this, other);
6277
}
6378

6479
set(key, value) {
65-
if (isObject(key)) {
66-
WeakMapPrototypeHas(this, undefined);
80+
// Also checks the receiver before a key is made.
81+
if (isObject(key) && !WeakMapPrototypeHas(this, key)) {
6782
key = unwrap(key, true);
6883
}
6984
WeakMapPrototypeSet(this, key, value);
7085
return this;
7186
}
7287

7388
has(key) {
74-
return WeakMapPrototypeHas(this, lookupKey(key));
89+
if (WeakMapPrototypeHas(this, key)) {
90+
return true;
91+
}
92+
const other = otherKey(key);
93+
return other !== undefined && WeakMapPrototypeHas(this, other);
7594
}
7695

7796
delete(key) {
78-
return WeakMapPrototypeDelete(this, lookupKey(key));
97+
if (WeakMapPrototypeDelete(this, key)) {
98+
return true;
99+
}
100+
const other = otherKey(key);
101+
return other !== undefined && WeakMapPrototypeDelete(this, other);
79102
}
80103
}
81104

82105
// V8 ships the upsert proposal; inherited, these would key on the wrapper.
83106
if (WeakMapPrototypeGetOrInsert !== undefined) {
84107
ObjectDefineProperty(WeakMap.prototype, "getOrInsert", {
85108
value: function getOrInsert(key, value) {
86-
if (isObject(key)) {
87-
WeakMapPrototypeHas(this, undefined);
109+
if (isObject(key) && !WeakMapPrototypeHas(this, key)) {
88110
key = unwrap(key, true);
89111
}
90112
return WeakMapPrototypeGetOrInsert(this, key, value);
@@ -99,8 +121,11 @@ if (WeakMapPrototypeGetOrInsertComputed !== undefined) {
99121
if (!isObject(key) || typeof callbackfn !== "function") {
100122
return WeakMapPrototypeGetOrInsertComputed(this, key, callbackfn);
101123
}
102-
const existing = unwrap(key, false);
103-
if (WeakMapPrototypeHas(this, existing)) {
124+
if (WeakMapPrototypeHas(this, key)) {
125+
return WeakMapPrototypeGet(this, key);
126+
}
127+
const existing = otherKey(key);
128+
if (existing !== undefined && WeakMapPrototypeHas(this, existing)) {
104129
return WeakMapPrototypeGet(this, existing);
105130
}
106131
// Before the key exists, so a callback that throws leaves none; it sees
@@ -116,20 +141,28 @@ if (WeakMapPrototypeGetOrInsertComputed !== undefined) {
116141

117142
class WeakSet extends NativeWeakSet {
118143
add(value) {
119-
if (isObject(value)) {
120-
WeakSetPrototypeHas(this, undefined);
144+
// Also checks the receiver before a key is made.
145+
if (isObject(value) && !WeakSetPrototypeHas(this, value)) {
121146
value = unwrap(value, true);
122147
}
123148
WeakSetPrototypeAdd(this, value);
124149
return this;
125150
}
126151

127152
has(value) {
128-
return WeakSetPrototypeHas(this, lookupKey(value));
153+
if (WeakSetPrototypeHas(this, value)) {
154+
return true;
155+
}
156+
const other = otherKey(value);
157+
return other !== undefined && WeakSetPrototypeHas(this, other);
129158
}
130159

131160
delete(value) {
132-
return WeakSetPrototypeDelete(this, lookupKey(value));
161+
if (WeakSetPrototypeDelete(this, value)) {
162+
return true;
163+
}
164+
const other = otherKey(value);
165+
return other !== undefined && WeakSetPrototypeDelete(this, other);
133166
}
134167
}
135168

@@ -192,7 +225,11 @@ class FinalizationRegistry extends NativeFinalizationRegistry {
192225
}
193226

194227
unregister(unregisterToken) {
195-
return FinalizationRegistryPrototypeUnregister(this, lookupKey(unregisterToken));
228+
if (FinalizationRegistryPrototypeUnregister(this, unregisterToken)) {
229+
return true;
230+
}
231+
const other = otherKey(unregisterToken);
232+
return other !== undefined && FinalizationRegistryPrototypeUnregister(this, other);
196233
}
197234
}
198235

0 commit comments

Comments
 (0)