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

perf(ext/websocket): efficient event kind serialization #18509

Merged
merged 6 commits into from
Mar 31, 2023

Conversation

littledivy
Copy link
Member

@littledivy littledivy commented Mar 30, 2023

Use u16 to represent the kind of event (0 - 6) & event code > 6 is treated as the close code. This way we can represent all events + the close code in a single JS number. This is safe because (as per RFC 6455) close code from 0-999 are reserved & not used.

name avg msg/sec/core
deno_main 127820.750000
deno #18506 140079.000000
deno #18506 + this 150104.250000

Copy link
Collaborator

@aapoalas aapoalas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. One suggestion I'd throw is to setup a custom op for ping sending as well. No reason to leave that as the only op calling ws_send with a string kind parameter.

@bartlomieju
Copy link
Member

Looks good to me. One suggestion I'd throw is to setup a custom op for ping sending as well. No reason to leave that as the only op calling ws_send with a string kind parameter.

Agreed, that probably causes V8 to deopt the function anyway

@littledivy littledivy marked this pull request as ready for review March 30, 2023 16:47
@bartlomieju bartlomieju self-requested a review March 30, 2023 20:46
}
Some(Ok(Message::Ping(_))) => (
MessageKind::Ping as u16,
StringOrBuffer::Buffer(vec![].into()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to send these empty buffers? Could we replace them with undefined? That still wouldn't address the fact that this function is megamorphic and will get deoptimized by V8 every time data received is different than when the function got jitted (if it gets jitted at all)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea to explore: store the received data on the Websocket struct, return only the number to JS and immediately call a sync op that either gets a buffer or a string (ie. two separate ops). That way we wouldn't make V8 deoptimize this function

Copy link
Member Author

@littledivy littledivy Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to send these empty buffers? Could we replace them with undefined?

Option<StringOrBuffer> makes it a little slow, since Ping and Pong are not frequent events i've left them as-is.

That still wouldn't address the fact that this function is megamorphic and will get deoptimized by V8 every time data received is different than when the function got jitted (if it gets jitted at all)

It won't get deoptimized everytime return value changes. AFAIK V8 can learn that this function returns either a AB or string back. It'll have JIT code prepared to handle that after a few calls.

@littledivy littledivy merged commit 0f41aff into denoland:main Mar 31, 2023
littledivy added a commit that referenced this pull request Mar 31, 2023
This commit adds a new core API `opAsync2` to call an async op with
atmost 2 arguments. Spread argument iterators has a pretty big perf hit
when calling ops.

| name | avg msg/sec/core |
| --- | --- |
| 1.32.1 | `127820.750000` |
| #18506 | `140079.000000` |
| #18506 + #18509 | `150104.250000` |
| #18506 + #18509 + this | `157340.000000` |
mmastrac pushed a commit that referenced this pull request Mar 31, 2023
Use u16 to represent the kind of event (0 - 6) & event code > 6 is
treated as the close code. This way we can represent all events + the
close code in a single JS number. This is safe because (as per RFC 6455)
close code from 0-999 are reserved & not used.

| name | avg msg/sec/core |
| --- | --- |
| deno_main | `127820.750000` |
| deno #18506 | `140079.000000` |
| deno #18506 + this | `150104.250000` |
mmastrac pushed a commit that referenced this pull request Mar 31, 2023
This commit adds a new core API `opAsync2` to call an async op with
atmost 2 arguments. Spread argument iterators has a pretty big perf hit
when calling ops.

| name | avg msg/sec/core |
| --- | --- |
| 1.32.1 | `127820.750000` |
| #18506 | `140079.000000` |
| #18506 + #18509 | `150104.250000` |
| #18506 + #18509 + this | `157340.000000` |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants