-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
perf: use a random pool #1986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: use a random pool #1986
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| const net = require('net'); | ||
| const tls = require('tls'); | ||
| const { randomFillSync } = require('crypto'); | ||
| const { randomBytes } = require('crypto'); | ||
|
|
||
| const PerMessageDeflate = require('./permessage-deflate'); | ||
| const { EMPTY_BUFFER } = require('./constants'); | ||
|
|
@@ -13,6 +13,22 @@ const { mask: applyMask, toBuffer } = require('./buffer-util'); | |
|
|
||
| const mask = Buffer.alloc(4); | ||
|
|
||
| const RANDOM_POOL_SIZE = 8192; | ||
| const RANDOM_POOL_REFRESH = 1024; | ||
|
|
||
| let randomPool = Buffer.alloc(0); | ||
| let randomPoolIdx = 0; | ||
| let randomPending = true; | ||
|
|
||
| function onRandomBytes(err, buf) { | ||
| randomPending = false; | ||
| if (!err) { | ||
| randomPool = buf; | ||
| randomPoolIdx = 0; | ||
| } | ||
| } | ||
| randomBytes(RANDOM_POOL_SIZE, onRandomBytes); | ||
|
|
||
| /** | ||
| * HyBi Sender implementation. | ||
| */ | ||
|
|
@@ -81,13 +97,24 @@ class Sender { | |
|
|
||
| if (!options.mask) return [target, data]; | ||
|
|
||
| randomFillSync(mask, 0, 4); | ||
| if (randomPool.length - randomPoolIdx < 4) { | ||
| randomPool = randomBytes(32); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced this is much better than the status quo. It would only take 8
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just the fallback path. We shouldn't hit this often, if at all.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are misunderstanding something here...? |
||
| randomPoolIdx = 0; | ||
| } | ||
|
|
||
| target[1] |= 0x80; | ||
| target[offset - 4] = mask[0]; | ||
| target[offset - 3] = mask[1]; | ||
| target[offset - 2] = mask[2]; | ||
| target[offset - 1] = mask[3]; | ||
| target[offset - 4] = mask[0] = randomPool[randomPoolIdx++]; | ||
| target[offset - 3] = mask[1] = randomPool[randomPoolIdx++]; | ||
| target[offset - 2] = mask[2] = randomPool[randomPoolIdx++]; | ||
| target[offset - 1] = mask[3] = randomPool[randomPoolIdx++]; | ||
|
|
||
| if ( | ||
| !randomPending && | ||
| randomPool.length - randomPoolIdx < RANDOM_POOL_REFRESH | ||
| ) { | ||
| randomPending = true; | ||
| randomBytes(RANDOM_POOL_SIZE, onRandomBytes); | ||
| } | ||
|
|
||
| if (merge) { | ||
| applyMask(data, mask, target, offset, data.length); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.