crypto: optimize key slot caching · nodejs/node@c63aa33 · GitHub
Skip to content

Commit c63aa33

Browse files
panvaaduh95
authored andcommitted
crypto: optimize key slot caching
Cache constructor-known KeyObject types in the existing private slot until a native handle is needed. Prime normal CryptoKey slot arrays from constructor arguments while retaining the native fallback for transferred keys. Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65518 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 21fed10 commit c63aa33

3 files changed

Lines changed: 72 additions & 41 deletions

File tree

‎lib/internal/crypto/keys.js‎

Lines changed: 44 additions & 19 deletions

‎src/crypto/README.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,23 @@ JavaScript needs access to those operations and is kept out of user-visible
171171

172172
A `KeyObject` is the public Node.js-specific API for keys. It extends a
173173
native `NativeKeyObject`, which stores `KeyObjectData` for structured
174-
cloning. The JavaScript API surface reads its key type and a
175-
`KeyObjectHandle` through a hidden native-backed slot tuple, caching that
176-
tuple in a private field outside user-visible own properties. Derived
177-
metadata, such as symmetric key size and asymmetric key details, is read
178-
from the cached handle and appended lazily to the same private-field cache.
174+
cloning. The JavaScript constructor caches the known key type in a private
175+
field outside user-visible own properties. When a `KeyObjectHandle` is first
176+
needed, JavaScript replaces that value with a hidden native-backed slot tuple.
177+
Derived metadata, such as symmetric key size and asymmetric key details, is
178+
read from the cached handle and appended lazily to the same private-field
179+
cache.
179180

180181
#### `CryptoKey`
181182

182183
A `CryptoKey` is the Web Crypto API key type. In the Node.js implementation,
183184
public `CryptoKey` instances are backed by a native `NativeCryptoKey`, not by
184185
a `KeyObject`. `NativeCryptoKey` stores the same `KeyObjectData`
185186
representation as `KeyObject`, plus the Web Crypto internal slots
186-
(`[[extractable]]`, `[[algorithm]]`, and `[[usages]]`).
187+
(`[[extractable]]`, `[[algorithm]]`, and `[[usages]]`). Normal construction
188+
primes a private JavaScript slot cache from the constructor arguments.
189+
Partially initialized transferred keys populate that cache from the native
190+
slots on first access.
187191

188192
### `CryptoJob`
189193

‎test/parallel/test-crypto-keyobject-clone-transfer.js‎

Lines changed: 18 additions & 16 deletions

0 commit comments

Comments
 (0)