-
-
Notifications
You must be signed in to change notification settings - Fork 660
websocket: 200x faster generate mask #3204
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
Merged
tsctx
merged 10 commits into
nodejs:main
from
tsctx:websocket/improve-performance-of-generate-mask
May 7, 2024
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
61fd2e8
websocket: improve performance of generate mask
tsctx 6b588b0
apply comments
tsctx 5e27ed2
Apply suggestions from code review
tsctx 277bf41
Apply suggestions from code review
tsctx 7def4f4
Apply suggestions from code review
tsctx afb2f8b
Apply suggestions from code review
tsctx efa1de5
Update lib/web/websocket/frame.js
tsctx 56b69b5
Update lib/web/websocket/frame.js
tsctx a2d4ae1
Merge branch 'nodejs:main' into websocket/improve-performance-of-gene…
tsctx 720e8d7
Update lib/web/websocket/frame.js
tsctx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { randomFillSync, randomBytes } from 'node:crypto' | ||
import { bench, group, run } from 'mitata' | ||
|
||
const BUFFER_SIZE = 16384 | ||
|
||
const buf = Buffer.allocUnsafe(BUFFER_SIZE) | ||
let bufIdx = BUFFER_SIZE | ||
|
||
function generateMask () { | ||
if (bufIdx === BUFFER_SIZE) { | ||
bufIdx = 0 | ||
randomFillSync(buf, 0, BUFFER_SIZE) | ||
} | ||
return [buf[bufIdx++], buf[bufIdx++], buf[bufIdx++], buf[bufIdx++]] | ||
} | ||
|
||
group('generate', () => { | ||
bench('generateMask', () => generateMask()) | ||
bench('crypto.randomBytes(4)', () => randomBytes(4)) | ||
}) | ||
|
||
await run() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,40 @@ | |
|
||
const { maxUnsigned16Bit } = require('./constants') | ||
|
||
const BUFFER_SIZE = 16386 | ||
|
||
/** @type {import('crypto')} */ | ||
let crypto | ||
let buffer = null | ||
let bufIdx = BUFFER_SIZE | ||
|
||
try { | ||
crypto = require('node:crypto') | ||
/* c8 ignore next 3 */ | ||
} catch { | ||
|
||
} | ||
|
||
function generateMask () { | ||
if (bufIdx === BUFFER_SIZE) { | ||
bufIdx = 0 | ||
crypto.randomFillSync((buffer ??= Buffer.allocUnsafe(BUFFER_SIZE)), 0, BUFFER_SIZE) | ||
} | ||
return [buffer[bufIdx++], buffer[bufIdx++], buffer[bufIdx++], buffer[bufIdx++]] | ||
} | ||
|
||
class WebsocketFrameSend { | ||
/** | ||
* @param {Buffer|undefined} data | ||
*/ | ||
constructor (data) { | ||
this.frameData = data | ||
this.maskKey = crypto.randomBytes(4) | ||
} | ||
|
||
createFrame (opcode) { | ||
const bodyLength = this.frameData?.byteLength ?? 0 | ||
const frameData = this.frameData | ||
const maskKey = generateMask() | ||
const bodyLength = frameData?.byteLength ?? 0 | ||
|
||
/** @type {number} */ | ||
let payloadLength = bodyLength // 0-125 | ||
|
@@ -43,10 +57,10 @@ class WebsocketFrameSend { | |
buffer[0] = (buffer[0] & 0xF0) + opcode // opcode | ||
|
||
/*! ws. MIT License. Einar Otto Stangvik <[email protected]> */ | ||
buffer[offset - 4] = this.maskKey[0] | ||
buffer[offset - 3] = this.maskKey[1] | ||
buffer[offset - 2] = this.maskKey[2] | ||
buffer[offset - 1] = this.maskKey[3] | ||
buffer[offset - 4] = maskKey[0] | ||
buffer[offset - 3] = maskKey[1] | ||
buffer[offset - 2] = maskKey[2] | ||
buffer[offset - 1] = maskKey[3] | ||
|
||
buffer[1] = payloadLength | ||
|
||
|
@@ -61,8 +75,8 @@ class WebsocketFrameSend { | |
buffer[1] |= 0x80 // MASK | ||
|
||
// mask body | ||
for (let i = 0; i < bodyLength; i++) { | ||
buffer[offset + i] = this.frameData[i] ^ this.maskKey[i % 4] | ||
for (let i = 0; i < bodyLength; ++i) { | ||
buffer[offset + i] = frameData[i] ^ maskKey[i & 3] | ||
} | ||
|
||
return buffer | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.