Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: avoid calling Buffer.byteLength multiple times #7236

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions benchmark/tls/convertprotocols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common.js');
const tls = require('tls');

const bench = common.createBenchmark(main, {
n: [1, 50000]
});

function main(conf) {
const n = +conf.n;

var i = 0;
var m = {};
common.v8ForceOptimization(
tls.convertNPNProtocols, ['ABC', 'XYZ123', 'FOO'], m);
bench.start();
for (; i < n; i++) tls.convertNPNProtocols(['ABC', 'XYZ123', 'FOO'], m);
bench.end(n);
}
20 changes: 11 additions & 9 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ exports.getCiphers = internalUtil.cachedResult(() => {
// Convert protocols array into valid OpenSSL protocols list
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
function convertProtocols(protocols) {
var buff = Buffer.allocUnsafe(protocols.reduce(function(p, c) {
return p + 1 + Buffer.byteLength(c);
const lens = Array(protocols.length);
const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => {
var len = Buffer.byteLength(c);
lens[i] = len;
return p + 1 + len;
}, 0));

protocols.reduce(function(offset, c) {
var clen = Buffer.byteLength(c);
buff[offset] = clen;
buff.write(c, offset + 1);

return offset + 1 + clen;
}, 0);
var offset = 0;
for (var i = 0, c = protocols.length; i < c; i++) {
buff[offset++] = lens[i];
buff.write(protocols[i], offset);
offset += lens[i];
}

return buff;
}
Expand Down