Skip to content

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
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions benchmarks/websocket/generate-mask.mjs
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()
30 changes: 22 additions & 8 deletions lib/web/websocket/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down