crypto: optimize SubtleCrypto dispatch hot paths#62756
Open
panva wants to merge 2 commits intonodejs:mainfrom
Open
crypto: optimize SubtleCrypto dispatch hot paths#62756panva wants to merge 2 commits intonodejs:mainfrom
panva wants to merge 2 commits intonodejs:mainfrom
Conversation
Collaborator
|
Review requested:
|
Replace O(n) for...in loop with case-insensitive
StringPrototypeToUpperCase comparisons per algorithm
with O(1) SafeMap lookup. The map is pre-built at module
init alongside kSupportedAlgorithms.
Hoist the opts object literal used in normalizeAlgorithm
to module level to avoid allocating identical
{ prefix, context } objects on every call.
Pre-compute ObjectKeys() for simpleAlgorithmDictionaries
entries at module init to avoid allocating a new keys
array on every normalizeAlgorithm call.
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
c23467e to
62dec56
Compare
Replace SafeArrayIterator with index-based loop in the inner function
returned by createDictionaryConverter, avoiding iterator object creation
on every dictionary conversion.
Replace object spread (`{ ...opts, context }`) with explicit property
assignment when passing opts to member converters. This avoids allocating
a full spread copy per dictionary member.
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
62dec56 to
8cfb8ef
Compare
Member
Author
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
op: [
'normalizeAlgorithm-string',
'normalizeAlgorithm-dict',
'webidl-dict',
],
n: [1e6],
}, { flags: ['--expose-internals'] });
function main({ n, op }) {
const { normalizeAlgorithm } = require('internal/crypto/util');
switch (op) {
case 'normalizeAlgorithm-string': {
// String shortcut + null dictionary (cheapest path).
bench.start();
for (let i = 0; i < n; i++)
normalizeAlgorithm('SHA-256', 'digest');
bench.end(n);
break;
}
case 'normalizeAlgorithm-dict': {
// Object input with a dictionary type (no BufferSource members).
const alg = { name: 'ECDSA', hash: 'SHA-256' };
bench.start();
for (let i = 0; i < n; i++)
normalizeAlgorithm(alg, 'sign');
bench.end(n);
break;
}
case 'webidl-dict': {
// WebIDL dictionary converter in isolation.
const webidl = require('internal/crypto/webidl');
const input = { name: 'AES-GCM', iv: new Uint8Array(12) };
const opts = { prefix: 'test', context: 'test' };
bench.start();
for (let i = 0; i < n; i++)
webidl.converters.AeadParams(input, opts);
bench.end(n);
break;
}
}
}
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62756 +/- ##
==========================================
- Coverage 89.69% 89.61% -0.08%
==========================================
Files 706 706
Lines 218127 217978 -149
Branches 41734 41652 -82
==========================================
- Hits 195651 195345 -306
- Misses 14400 14564 +164
+ Partials 8076 8069 -7
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This improves two hot paths in Web Cryptography
crypto: optimize normalizeAlgorithm hot path
Replace O(n) for...in loop with case-insensitive StringPrototypeToUpperCase comparisons per algorithm with O(1) SafeMap lookup. The map is pre-built at module init alongside kSupportedAlgorithms.
Hoist the opts object literal used in normalizeAlgorithm to module level to avoid allocating identical { prefix, context } objects on every call.
Pre-compute ObjectKeys() for simpleAlgorithmDictionaries entries at module init to avoid allocating a new keys array on every normalizeAlgorithm call.
crypto: reduce allocations in WebIDL dictionary converter
Replace SafeArrayIterator with index-based loop in the inner function returned by createDictionaryConverter, avoiding iterator object creation on every dictionary conversion.
Replace object spread (
{ ...opts, context }) with explicit property assignment when passing opts to member converters. This avoids allocating a full spread copy per dictionary member.