Skip to content

Commit 64164c7

Browse files
mcollinagibfahn
authored andcommitted
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 Backport-PR-URL: #16446 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 8dddc7e commit 64164c7

File tree

1 file changed

+71
-13
lines changed

1 file changed

+71
-13
lines changed

lib/crypto.js

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const assert = require('assert');
7474
const StringDecoder = require('string_decoder').StringDecoder;
7575

7676

77-
exports.createHash = exports.Hash = Hash;
77+
exports.Hash = Hash;
7878
function Hash(algorithm, options) {
7979
if (!(this instanceof Hash))
8080
return new Hash(algorithm, options);
@@ -108,7 +108,7 @@ Hash.prototype.digest = function digest(outputEncoding) {
108108
};
109109

110110

111-
exports.createHmac = exports.Hmac = Hmac;
111+
exports.Hmac = Hmac;
112112

113113
function Hmac(hmac, key, options) {
114114
if (!(this instanceof Hmac))
@@ -134,7 +134,7 @@ function getDecoder(decoder, encoding) {
134134
}
135135

136136

137-
exports.createCipher = exports.Cipher = Cipher;
137+
exports.Cipher = Cipher;
138138
function Cipher(cipher, password, options) {
139139
if (!(this instanceof Cipher))
140140
return new Cipher(cipher, password, options);
@@ -211,7 +211,7 @@ Cipher.prototype.setAAD = function setAAD(aadbuf) {
211211
return this;
212212
};
213213

214-
exports.createCipheriv = exports.Cipheriv = Cipheriv;
214+
exports.Cipheriv = Cipheriv;
215215
function Cipheriv(cipher, key, iv, options) {
216216
if (!(this instanceof Cipheriv))
217217
return new Cipheriv(cipher, key, iv, options);
@@ -233,7 +233,7 @@ Cipheriv.prototype.getAuthTag = Cipher.prototype.getAuthTag;
233233
Cipheriv.prototype.setAuthTag = Cipher.prototype.setAuthTag;
234234
Cipheriv.prototype.setAAD = Cipher.prototype.setAAD;
235235

236-
exports.createDecipher = exports.Decipher = Decipher;
236+
exports.Decipher = Decipher;
237237
function Decipher(cipher, password, options) {
238238
if (!(this instanceof Decipher))
239239
return new Decipher(cipher, password, options);
@@ -258,7 +258,7 @@ Decipher.prototype.setAuthTag = Cipher.prototype.setAuthTag;
258258
Decipher.prototype.setAAD = Cipher.prototype.setAAD;
259259

260260

261-
exports.createDecipheriv = exports.Decipheriv = Decipheriv;
261+
exports.Decipheriv = Decipheriv;
262262
function Decipheriv(cipher, key, iv, options) {
263263
if (!(this instanceof Decipheriv))
264264
return new Decipheriv(cipher, key, iv, options);
@@ -283,7 +283,7 @@ Decipheriv.prototype.setAuthTag = Cipher.prototype.setAuthTag;
283283
Decipheriv.prototype.setAAD = Cipher.prototype.setAAD;
284284

285285

286-
exports.createSign = exports.Sign = Sign;
286+
exports.Sign = Sign;
287287
function Sign(algorithm, options) {
288288
if (!(this instanceof Sign))
289289
return new Sign(algorithm, options);
@@ -339,7 +339,7 @@ Sign.prototype.sign = function sign(options, encoding) {
339339
};
340340

341341

342-
exports.createVerify = exports.Verify = Verify;
342+
exports.Verify = Verify;
343343
function Verify(algorithm, options) {
344344
if (!(this instanceof Verify))
345345
return new Verify(algorithm, options);
@@ -410,7 +410,7 @@ exports.privateDecrypt = rsaPrivate(binding.privateDecrypt,
410410
constants.RSA_PKCS1_OAEP_PADDING);
411411

412412

413-
exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
413+
exports.DiffieHellman = DiffieHellman;
414414

415415
function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
416416
if (!(this instanceof DiffieHellman))
@@ -452,9 +452,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
452452
}
453453

454454

455-
exports.DiffieHellmanGroup =
456-
exports.createDiffieHellmanGroup =
457-
exports.getDiffieHellman = DiffieHellmanGroup;
455+
exports.DiffieHellmanGroup = DiffieHellmanGroup;
458456

459457
function DiffieHellmanGroup(name) {
460458
if (!(this instanceof DiffieHellmanGroup))
@@ -561,7 +559,7 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) {
561559
};
562560

563561

564-
exports.createECDH = exports.ECDH = ECDH;
562+
exports.ECDH = ECDH;
565563
function ECDH(curve) {
566564
if (!(this instanceof ECDH))
567565
return new ECDH(curve);
@@ -607,6 +605,66 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
607605
};
608606

609607

608+
// These helper functions are needed because the constructors can
609+
// use new, in which case V8 cannot inline the recursive constructor call
610+
function createHash(algorithm, options) {
611+
return new Hash(algorithm, options);
612+
}
613+
614+
function createCipher(cipher, password, options) {
615+
return new Cipher(cipher, password, options);
616+
}
617+
618+
function createCipheriv(cipher, key, iv, options) {
619+
return new Cipheriv(cipher, key, iv, options);
620+
}
621+
622+
function createDecipher(cipher, password, options) {
623+
return new Decipher(cipher, password, options);
624+
}
625+
626+
function createDecipheriv(cipher, key, iv, options) {
627+
return new Decipheriv(cipher, key, iv, options);
628+
}
629+
630+
function createDiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
631+
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
632+
}
633+
634+
function createDiffieHellmanGroup(name) {
635+
return new DiffieHellmanGroup(name);
636+
}
637+
638+
function createECDH(curve) {
639+
return new ECDH(curve);
640+
}
641+
642+
function createHmac(hmac, key, options) {
643+
return new Hmac(hmac, key, options);
644+
}
645+
646+
function createSign(algorithm, options) {
647+
return new Sign(algorithm, options);
648+
}
649+
650+
function createVerify(algorithm, options) {
651+
return new Verify(algorithm, options);
652+
}
653+
654+
exports.createHash = createHash;
655+
exports.createCipher = createCipher;
656+
exports.createCipheriv = createCipheriv;
657+
exports.createDecipher = createDecipher;
658+
exports.createDecipheriv = createDecipheriv;
659+
exports.createDiffieHellman = createDiffieHellman;
660+
exports.createDiffieHellmanGroup =
661+
exports.getDiffieHellman = createDiffieHellmanGroup;
662+
exports.createECDH = createECDH;
663+
exports.createHmac = createHmac;
664+
exports.createSign = createSign;
665+
exports.createVerify = createVerify;
666+
667+
610668
exports.pbkdf2 = function(password,
611669
salt,
612670
iterations,

0 commit comments

Comments
 (0)