Skip to content

Commit 9bc4f86

Browse files
committed
crypto: make createXYZ inlineable
This commit increase by around 10% hot code paths that are hitting createXYZ functions. Before this change the createXYZ called the XYZ constructor without new. PR-URL: #16067 Reviewed-By: Bryan English <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yosuke Furukawa <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Benedikt Meurer <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 9f98989 commit 9bc4f86

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

lib/crypto.js

+58-12
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,69 @@ const {
7979
} = require('internal/crypto/util');
8080
const Certificate = require('internal/crypto/certificate');
8181

82+
// These helper functions are needed because the constructors can
83+
// use new, in which case V8 cannot inline the recursive constructor call
84+
function createHash(algorithm, options) {
85+
return new Hash(algorithm, options);
86+
}
87+
88+
function createCipher(cipher, password, options) {
89+
return new Cipher(cipher, password, options);
90+
}
91+
92+
function createCipheriv(cipher, key, iv, options) {
93+
return new Cipheriv(cipher, key, iv, options);
94+
}
95+
96+
function createDecipher(cipher, password, options) {
97+
return new Decipher(cipher, password, options);
98+
}
99+
100+
function createDecipheriv(cipher, key, iv, options) {
101+
return new Decipheriv(cipher, key, iv, options);
102+
}
103+
104+
function createDiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
105+
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
106+
}
107+
108+
function createDiffieHellmanGroup(name) {
109+
return new DiffieHellmanGroup(name);
110+
}
111+
112+
function createECDH(curve) {
113+
return new ECDH(curve);
114+
}
115+
116+
function createHmac(hmac, key, options) {
117+
return new Hmac(hmac, key, options);
118+
}
119+
120+
function createSign(algorithm, options) {
121+
return new Sign(algorithm, options);
122+
}
123+
124+
function createVerify(algorithm, options) {
125+
return new Verify(algorithm, options);
126+
}
127+
82128
module.exports = exports = {
83129
// Methods
84130
_toBuf: toBuf,
85-
createCipher: Cipher,
86-
createCipheriv: Cipheriv,
87-
createDecipher: Decipher,
88-
createDecipheriv: Decipheriv,
89-
createDiffieHellman: DiffieHellman,
90-
createDiffieHellmanGroup: DiffieHellmanGroup,
91-
createECDH: ECDH,
92-
createHash: Hash,
93-
createHmac: Hmac,
94-
createSign: Sign,
95-
createVerify: Verify,
131+
createCipher,
132+
createCipheriv,
133+
createDecipher,
134+
createDecipheriv,
135+
createDiffieHellman,
136+
createDiffieHellmanGroup,
137+
createECDH,
138+
createHash,
139+
createHmac,
140+
createSign,
141+
createVerify,
96142
getCiphers,
97143
getCurves,
98-
getDiffieHellman: DiffieHellmanGroup,
144+
getDiffieHellman: createDiffieHellmanGroup,
99145
getHashes,
100146
pbkdf2,
101147
pbkdf2Sync,

0 commit comments

Comments
 (0)