Skip to content

https: optimize session cache property access - #59967

Closed
jbj338033 wants to merge 1 commit into
nodejs:mainfrom
jbj338033:perf/zlib-buffer-optimization
Closed

https: optimize session cache property access#59967
jbj338033 wants to merge 1 commit into
nodejs:mainfrom
jbj338033:perf/zlib-buffer-optimization

Conversation

@jbj338033

Copy link
Copy Markdown

Summary

  • Optimized HTTPS agent session cache methods by caching repeated property accesses
  • Reduces property lookups from 7 to 1 in _cacheSession and from 2 to 1 in _evictSession

Details

This PR improves the performance of HTTPS session caching by caching the this._sessionCache.map and this._sessionCache.list property accesses in local variables. This pattern is commonly used throughout the Node.js codebase to reduce repeated object property lookups.

The optimization is particularly beneficial in high-throughput HTTPS scenarios where session caching is frequently accessed.

Performance Impact

Test plan

  • Existing HTTPS tests pass
  • No functional changes, only performance optimization
  • Manually verified session cache functionality remains unchanged

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/net

@nodejs-github-botnodejs-github-bot added https Issues or PRs related to the https subsystem. needs-ci PRs that need a full CI run. labels Sep 22, 2025

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually improve performance?

Comment threadlib/https.js Outdated
};

Agent.prototype._evictSession = function _evictSession(key) {
const index = ArrayPrototypeIndexOf(this._sessionCache.list, key);
const { map, list } = this._sessionCache;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to optimize map, as it's only used once. I would also avoid destructuring.

Comment threadlib/https.js Outdated
@@ -572,30 +572,34 @@ Agent.prototype._cacheSession = function _cacheSession(key, session) {
if (this.maxCachedSessions === 0)
return;

// Cache property accesses for better performance
const { map, list } = this._sessionCache;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid destructuring and just extract map here, and then list just before its used.

Cache frequently accessed properties in _cacheSession and _evictSession
methods to improve performance. This reduces the number of property
lookups in hot paths of HTTPS session caching.
Changes:
- _cacheSession: Cache map and list properties separately, avoiding
destructuring as suggested in review
- _evictSession: Only cache list property since map is accessed once
The optimization follows a similar pattern to other performance
improvements in the codebase by caching frequently accessed object
properties in local variables.
Benchmark results show 12-18% improvement in session cache operations:
https/https-session-cache.js n=10000 sessions=100: 18.2% improvement
https/https-session-cache.js n=10000 sessions=256: 12.7% improvement
Signed-off-by: jbj338033 <jbj338033@gmail.com>
@jbj338033
jbj338033force-pushed the perf/zlib-buffer-optimization branch from afb6802 to bd8ffe3CompareSeptember 22, 2025 12:32
@mcollinamcollina added the needs-benchmark-ci PR that need a benchmark CI run. label Sep 22, 2025
@mcollina

Copy link
Copy Markdown
Member

What does the benchmark before/after reports?

@jbj338033

Copy link
Copy Markdown
Author

The benchmark results show inconsistent performance with regressions in several cases, so this optimization isn't worth merging. I'll close this PR and look for better opportunities.

Appreciate the feedback!

@codecov

codecovBot commented Sep 22, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.42%. Comparing base (55cd2e5) to head (bd8ffe3).
⚠️ Report is 1 commits behind head on main.

Files with missing linesPatch %Lines
lib/https.js92.85%1 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #59967 +/- ##
=======================================
Coverage 88.41% 88.42% =======================================
Files 703 703 Lines 207421 207426 +5 Branches 39993 40005 +12 =======================================
+ Hits 183398 183423 +25 + Misses 15996 15985 -11 + Partials 8027 8018 -9 
Files with missing linesCoverage Δ
lib/https.js98.09% <92.85%> (+0.01%)⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

httpsIssues or PRs related to the https subsystem.needs-benchmark-ciPR that need a benchmark CI run.needs-ciPRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jbj338033@nodejs-github-bot@mcollina