Skip to content

Commit

Permalink
crypto: make createXYZ inlineable
Browse files Browse the repository at this point in the history
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 <bryan@bryanenglish.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
mcollina authored and gibfahn committed Dec 19, 2017
1 parent 8dddc7e commit 64164c7
Showing 1 changed file with 71 additions and 13 deletions.
84 changes: 71 additions & 13 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const assert = require('assert');
const StringDecoder = require('string_decoder').StringDecoder;


exports.createHash = exports.Hash = Hash;
exports.Hash = Hash;
function Hash(algorithm, options) {
if (!(this instanceof Hash))
return new Hash(algorithm, options);
Expand Down Expand Up @@ -108,7 +108,7 @@ Hash.prototype.digest = function digest(outputEncoding) {
};


exports.createHmac = exports.Hmac = Hmac;
exports.Hmac = Hmac;

function Hmac(hmac, key, options) {
if (!(this instanceof Hmac))
Expand All @@ -134,7 +134,7 @@ function getDecoder(decoder, encoding) {
}


exports.createCipher = exports.Cipher = Cipher;
exports.Cipher = Cipher;
function Cipher(cipher, password, options) {
if (!(this instanceof Cipher))
return new Cipher(cipher, password, options);
Expand Down Expand Up @@ -211,7 +211,7 @@ Cipher.prototype.setAAD = function setAAD(aadbuf) {
return this;
};

exports.createCipheriv = exports.Cipheriv = Cipheriv;
exports.Cipheriv = Cipheriv;
function Cipheriv(cipher, key, iv, options) {
if (!(this instanceof Cipheriv))
return new Cipheriv(cipher, key, iv, options);
Expand All @@ -233,7 +233,7 @@ Cipheriv.prototype.getAuthTag = Cipher.prototype.getAuthTag;
Cipheriv.prototype.setAuthTag = Cipher.prototype.setAuthTag;
Cipheriv.prototype.setAAD = Cipher.prototype.setAAD;

exports.createDecipher = exports.Decipher = Decipher;
exports.Decipher = Decipher;
function Decipher(cipher, password, options) {
if (!(this instanceof Decipher))
return new Decipher(cipher, password, options);
Expand All @@ -258,7 +258,7 @@ Decipher.prototype.setAuthTag = Cipher.prototype.setAuthTag;
Decipher.prototype.setAAD = Cipher.prototype.setAAD;


exports.createDecipheriv = exports.Decipheriv = Decipheriv;
exports.Decipheriv = Decipheriv;
function Decipheriv(cipher, key, iv, options) {
if (!(this instanceof Decipheriv))
return new Decipheriv(cipher, key, iv, options);
Expand All @@ -283,7 +283,7 @@ Decipheriv.prototype.setAuthTag = Cipher.prototype.setAuthTag;
Decipheriv.prototype.setAAD = Cipher.prototype.setAAD;


exports.createSign = exports.Sign = Sign;
exports.Sign = Sign;
function Sign(algorithm, options) {
if (!(this instanceof Sign))
return new Sign(algorithm, options);
Expand Down Expand Up @@ -339,7 +339,7 @@ Sign.prototype.sign = function sign(options, encoding) {
};


exports.createVerify = exports.Verify = Verify;
exports.Verify = Verify;
function Verify(algorithm, options) {
if (!(this instanceof Verify))
return new Verify(algorithm, options);
Expand Down Expand Up @@ -410,7 +410,7 @@ exports.privateDecrypt = rsaPrivate(binding.privateDecrypt,
constants.RSA_PKCS1_OAEP_PADDING);


exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
exports.DiffieHellman = DiffieHellman;

function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
if (!(this instanceof DiffieHellman))
Expand Down Expand Up @@ -452,9 +452,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
}


exports.DiffieHellmanGroup =
exports.createDiffieHellmanGroup =
exports.getDiffieHellman = DiffieHellmanGroup;
exports.DiffieHellmanGroup = DiffieHellmanGroup;

function DiffieHellmanGroup(name) {
if (!(this instanceof DiffieHellmanGroup))
Expand Down Expand Up @@ -561,7 +559,7 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) {
};


exports.createECDH = exports.ECDH = ECDH;
exports.ECDH = ECDH;
function ECDH(curve) {
if (!(this instanceof ECDH))
return new ECDH(curve);
Expand Down Expand Up @@ -607,6 +605,66 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
};


// These helper functions are needed because the constructors can
// use new, in which case V8 cannot inline the recursive constructor call
function createHash(algorithm, options) {
return new Hash(algorithm, options);
}

function createCipher(cipher, password, options) {
return new Cipher(cipher, password, options);
}

function createCipheriv(cipher, key, iv, options) {
return new Cipheriv(cipher, key, iv, options);
}

function createDecipher(cipher, password, options) {
return new Decipher(cipher, password, options);
}

function createDecipheriv(cipher, key, iv, options) {
return new Decipheriv(cipher, key, iv, options);
}

function createDiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
}

function createDiffieHellmanGroup(name) {
return new DiffieHellmanGroup(name);
}

function createECDH(curve) {
return new ECDH(curve);
}

function createHmac(hmac, key, options) {
return new Hmac(hmac, key, options);
}

function createSign(algorithm, options) {
return new Sign(algorithm, options);
}

function createVerify(algorithm, options) {
return new Verify(algorithm, options);
}

exports.createHash = createHash;
exports.createCipher = createCipher;
exports.createCipheriv = createCipheriv;
exports.createDecipher = createDecipher;
exports.createDecipheriv = createDecipheriv;
exports.createDiffieHellman = createDiffieHellman;
exports.createDiffieHellmanGroup =
exports.getDiffieHellman = createDiffieHellmanGroup;
exports.createECDH = createECDH;
exports.createHmac = createHmac;
exports.createSign = createSign;
exports.createVerify = createVerify;


exports.pbkdf2 = function(password,
salt,
iterations,
Expand Down

0 comments on commit 64164c7

Please sign in to comment.