Skip to content
Permalink
Browse files

http2: improve http2 code a bit

Multiple general improvements to http2 internals for
readability and efficiency

[This backport applied to v10.x cleanly.]

Backport-PR-URL: #29123
PR-URL: #23984
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information...
jasnell authored and BethGriggs committed Oct 30, 2018
1 parent b743000 commit 00f153da1352b941d8749758edb6083a6acda715
Showing with 212 additions and 194 deletions.
  1. +1 −2 benchmark/http2/headers.js
  2. +3 −3 benchmark/http2/respond-with-fd.js
  3. +3 −3 benchmark/http2/simple.js
  4. +25 −19 lib/internal/http2/util.js
  5. +180 −167 src/node_http2.cc
@@ -40,8 +40,7 @@ function main({ n, nheaders }) {


function doRequest(remaining) { function doRequest(remaining) {
const req = client.request(headersObject); const req = client.request(headersObject);
req.end(); req.resume();
req.on('data', () => {});
req.on('end', () => { req.on('end', () => {
if (remaining > 0) { if (remaining > 0) {
doRequest(remaining - 1); doRequest(remaining - 1);
@@ -7,9 +7,9 @@ const fs = require('fs');
const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html'); const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html');


const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
requests: [100, 1000, 10000, 100000], requests: [100, 1000, 5000],
streams: [100, 200, 1000], streams: [1, 10, 20, 40, 100, 200],
clients: [1, 2], clients: [2],
benchmarker: ['h2load'] benchmarker: ['h2load']
}, { flags: ['--no-warnings', '--expose-http2'] }); }, { flags: ['--no-warnings', '--expose-http2'] });


@@ -6,9 +6,9 @@ const fs = require('fs');
const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html'); const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html');


const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
requests: [100, 1000, 10000, 100000], requests: [100, 1000, 5000],
streams: [100, 200, 1000], streams: [1, 10, 20, 40, 100, 200],
clients: [1, 2], clients: [2],
benchmarker: ['h2load'] benchmarker: ['h2load']
}, { flags: ['--no-warnings', '--expose-http2'] }); }, { flags: ['--no-warnings', '--expose-http2'] });


@@ -430,14 +430,20 @@ function mapToHeaders(map,
let count = 0; let count = 0;
const keys = Object.keys(map); const keys = Object.keys(map);
const singles = new Set(); const singles = new Set();
for (var i = 0; i < keys.length; i++) { let i;
let key = keys[i]; let isArray;
let value = map[key]; let key;
let value;
let isSingleValueHeader;
let err;
for (i = 0; i < keys.length; i++) {
key = keys[i];
value = map[key];
if (value === undefined || key === '') if (value === undefined || key === '')
continue; continue;
key = key.toLowerCase(); key = key.toLowerCase();
const isSingleValueHeader = kSingleValueHeaders.has(key); isSingleValueHeader = kSingleValueHeaders.has(key);
let isArray = Array.isArray(value); isArray = Array.isArray(value);
if (isArray) { if (isArray) {
switch (value.length) { switch (value.length) {
case 0: case 0:
@@ -459,26 +465,26 @@ function mapToHeaders(map,
singles.add(key); singles.add(key);
} }
if (key[0] === ':') { if (key[0] === ':') {
const err = assertValuePseudoHeader(key); err = assertValuePseudoHeader(key);
if (err !== undefined) if (err !== undefined)
return err; return err;
ret = `${key}\0${value}\0${ret}`; ret = `${key}\0${value}\0${ret}`;
count++; count++;
} else { continue;
if (isIllegalConnectionSpecificHeader(key, value)) { }
return new ERR_HTTP2_INVALID_CONNECTION_HEADERS(key); if (isIllegalConnectionSpecificHeader(key, value)) {
} return new ERR_HTTP2_INVALID_CONNECTION_HEADERS(key);
if (isArray) { }
for (var k = 0; k < value.length; k++) { if (isArray) {
const val = String(value[k]); for (var k = 0; k < value.length; k++) {
ret += `${key}\0${val}\0`; const val = String(value[k]);
} ret += `${key}\0${val}\0`;
count += value.length;
} else {
ret += `${key}\0${value}\0`;
count++;
} }
count += value.length;
continue;
} }
ret += `${key}\0${value}\0`;
count++;
} }


return [ret, count]; return [ret, count];

0 comments on commit 00f153d

Please sign in to comment.
You can’t perform that action at this time.