From 23ac4bb6b51b20a30418b9ec26af42cb2920dee8 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Mon, 17 Feb 2020 19:17:57 +0100 Subject: [PATCH] Performance improvements --- source/agent.js | 18 +++++++++++------- source/client-request.js | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/source/agent.js b/source/agent.js index 76c58d9..777db8e 100644 --- a/source/agent.js +++ b/source/agent.js @@ -6,6 +6,7 @@ const QuickLRU = require('quick-lru'); const kCurrentStreamsCount = Symbol('currentStreamsCount'); const kRequest = Symbol('request'); +const kOriginSet = Symbol('cachedOriginSet'); const nameKeys = [ // `http2.connect()` options @@ -71,7 +72,7 @@ const addSession = (where, name, session) => { const getSessions = (where, name, normalizedOrigin) => { if (Reflect.has(where, name)) { return where[name].filter(session => { - return !session.closed && !session.destroyed && session.originSet.includes(normalizedOrigin); + return !session.closed && !session.destroyed && session[kOriginSet].includes(normalizedOrigin); }); } @@ -90,10 +91,10 @@ const closeCoveredSessions = (where, name, session) => { for (const coveredSession of where[name]) { if ( // The set is a proper subset when its length is less than the other set. - coveredSession.originSet.length < session.originSet.length && + coveredSession[kOriginSet].length < session[kOriginSet].length && // And the other set includes all elements of the subset. - coveredSession.originSet.every(origin => session.originSet.includes(origin)) && + coveredSession[kOriginSet].every(origin => session[kOriginSet].includes(origin)) && // Makes sure that the session can handle all requests from the covered session. // TODO: can the session become uncovered when a stream is closed after checking this condition? @@ -109,8 +110,8 @@ const closeCoveredSessions = (where, name, session) => { const closeSessionIfCovered = (where, name, coveredSession) => { for (const session of where[name]) { if ( - coveredSession.originSet.length < session.originSet.length && - coveredSession.originSet.every(origin => session.originSet.includes(origin)) && + coveredSession[kOriginSet].length < session[kOriginSet].length && + coveredSession[kOriginSet].every(origin => session[kOriginSet].includes(origin)) && coveredSession[kCurrentStreamsCount] + session[kCurrentStreamsCount] <= session.remoteSettings.maxConcurrentStreams ) { coveredSession.close(); @@ -293,7 +294,7 @@ class Agent extends EventEmitter { // Tries to free the session. const freeSession = () => { // Fetch the smallest amount of free sessions of any origin we have. - const freeSessionsCount = session.originSet.reduce((accumulator, origin) => { + const freeSessionsCount = session[kOriginSet].reduce((accumulator, origin) => { return Math.min(accumulator, getSessions(this.freeSessions, normalizedOptions, origin).length); }, Infinity); @@ -372,7 +373,7 @@ class Agent extends EventEmitter { return; } - for (const origin of session.originSet) { + for (const origin of session[kOriginSet]) { if (Reflect.has(this.queue[normalizedOptions], origin)) { const {listeners} = this.queue[normalizedOptions][origin]; @@ -402,6 +403,8 @@ class Agent extends EventEmitter { // The Origin Set cannot shrink. No need to check if it suddenly became covered by another one. session.once('origin', () => { + session[kOriginSet] = session.originSet; + if (!isFree()) { // The session is full. return; @@ -431,6 +434,7 @@ class Agent extends EventEmitter { return; } + session[kOriginSet] = session.originSet; this.emit('session', session); if (freeSession()) { diff --git a/source/client-request.js b/source/client-request.js index 8500e55..9ef1327 100644 --- a/source/client-request.js +++ b/source/client-request.js @@ -78,7 +78,7 @@ class ClientRequest extends Writable { } const {timeout} = options; - delete options.timeout; + options.timeout = undefined; this[kHeaders] = Object.create(null);