Skip to content
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

Optimization of websocket masking #3265

Closed
tsctx opened this issue May 15, 2024 · 9 comments
Closed

Optimization of websocket masking #3265

tsctx opened this issue May 15, 2024 · 9 comments
Labels
websocket Pull requests or issues related to websocket and its standard

Comments

@tsctx
Copy link
Member

tsctx commented May 15, 2024

as titled

This is a cause of poor performance.

// mask body
for (let i = 0; i < bodyLength; ++i) {
buffer[offset + i] = frameData[i] ^ maskKey[i & 3]
}

@tsctx tsctx added the websocket Pull requests or issues related to websocket and its standard label May 15, 2024
@Uzlopak
Copy link
Contributor

Uzlopak commented May 15, 2024

Well. we could reduce the for loop overhead by doing 4 masking operations instead of one.

@ronag
Copy link
Member

ronag commented May 15, 2024

As I indicated earlier. The masking is as far as I know totally useless for backend and could just be skipped...

@Uzlopak
Copy link
Contributor

Uzlopak commented May 15, 2024

Maybe instead of i + 4 < length, you precalculate that too

const lengthFor4 = length - (length & 3)

@tsctx

This comment was marked as resolved.

@Uzlopak
Copy link
Contributor

Uzlopak commented May 15, 2024

maybe for the last potential 3 operations a duffs device?

@tsctx
Copy link
Member Author

tsctx commented May 15, 2024

yes?

@Uzlopak
Copy link
Contributor

Uzlopak commented May 15, 2024

Something like this. Sry, I still suffer from a heat stroke and cant provide better code.

switch (length & 3) {
  case 3:
    buffer[i + 3] ^ mask[3]
  case 2:
    buffer[i + 2] ^ mask[2]
  case 2:
    buffer[i + 1] ^ mask[1]
  ...

@tsctx
Copy link
Member Author

tsctx commented May 15, 2024

Take care and get well soon.

@tsctx
Copy link
Member Author

tsctx commented May 19, 2024

I give this up

@tsctx tsctx closed this as completed May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
websocket Pull requests or issues related to websocket and its standard
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants