Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ddfe4a8

Browse filesBrowse files
committed
[perf] Reduce the amount of crypto.randomFillSync() calls
Use a pool of random bytes to reduce the amount of `crypto.randomFillSync()` calls. Refs: nodejs/undici#3204
1 parent b73b118 commit ddfe4a8
Copy full SHA for ddfe4a8

File tree

1 file changed

+16
-1
lines changed
Filter options

1 file changed

+16
-1
lines changed

‎lib/sender.js

Copy file name to clipboardExpand all lines: lib/sender.js
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const { mask: applyMask, toBuffer } = require('./buffer-util');
1212

1313
const kByteLength = Symbol('kByteLength');
1414
const maskBuffer = Buffer.alloc(4);
15+
const RANDOM_POOL_SIZE = 8 * 1024;
16+
let randomPool;
17+
let randomPoolPointer = RANDOM_POOL_SIZE;
1518

1619
/**
1720
* HyBi Sender implementation.
@@ -76,7 +79,19 @@ class Sender {
7679
if (options.generateMask) {
7780
options.generateMask(mask);
7881
} else {
79-
randomFillSync(mask, 0, 4);
82+
if (randomPoolPointer === RANDOM_POOL_SIZE) {
83+
if (randomPool === undefined) {
84+
randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
85+
}
86+
87+
randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
88+
randomPoolPointer = 0;
89+
}
90+
91+
mask[0] = randomPool[randomPoolPointer++];
92+
mask[1] = randomPool[randomPoolPointer++];
93+
mask[2] = randomPool[randomPoolPointer++];
94+
mask[3] = randomPool[randomPoolPointer++];
8095
}
8196

8297
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.