Skip to content

perf(ext/websocket): various performance improvements #18862

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 5 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 15 additions & 15 deletions ext/websocket/01_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
/// <reference path="../../core/internal.d.ts" />

const core = globalThis.Deno.core;
const ops = core.ops;
const { opAsync, opAsync2 } = core;
// deno-lint-ignore camelcase
const op_ws_check_permission_and_cancel_handle =
core.ops.op_ws_check_permission_and_cancel_handle;
import { URL } from "ext:deno_url/00_url.js";
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { HTTP_TOKEN_CODE_POINT_RE } from "ext:deno_web/00_infra.js";
Expand Down Expand Up @@ -210,7 +213,7 @@ class WebSocket extends EventTarget {
this[_url] = wsURL.href;
this[_role] = CLIENT;

ops.op_ws_check_permission_and_cancel_handle(
op_ws_check_permission_and_cancel_handle(
"WebSocket.abort()",
this[_url],
false,
Expand Down Expand Up @@ -247,7 +250,7 @@ class WebSocket extends EventTarget {
}

PromisePrototypeThen(
core.opAsync(
opAsync(
"op_ws_create",
"new WebSocket()",
wsURL.href,
Expand All @@ -260,7 +263,7 @@ class WebSocket extends EventTarget {

if (this[_readyState] === CLOSING) {
PromisePrototypeThen(
core.opAsync("op_ws_close", this[_rid]),
opAsync("op_ws_close", this[_rid]),
() => {
this[_readyState] = CLOSED;

Expand Down Expand Up @@ -316,7 +319,7 @@ class WebSocket extends EventTarget {
const sendTypedArray = (view, byteLength) => {
this[_bufferedAmount] += byteLength;
PromisePrototypeThen(
core.opAsync2(
opAsync2(
"op_ws_send_binary",
this[_rid],
view,
Expand Down Expand Up @@ -345,16 +348,13 @@ class WebSocket extends EventTarget {
sendTypedArray(data, TypedArrayPrototypeGetByteLength(data));
}
} else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
sendTypedArray(
new DataView(data),
ArrayBufferPrototypeGetByteLength(data),
);
sendTypedArray(data, ArrayBufferPrototypeGetByteLength(data));
} else {
const string = String(data);
const d = core.encode(string);
this[_bufferedAmount] += TypedArrayPrototypeGetByteLength(d);
PromisePrototypeThen(
core.opAsync2(
opAsync2(
"op_ws_send_text",
this[_rid],
string,
Expand Down Expand Up @@ -413,7 +413,7 @@ class WebSocket extends EventTarget {
this[_readyState] = CLOSING;

PromisePrototypeCatch(
core.opAsync(
opAsync(
"op_ws_close",
this[_rid],
code,
Expand All @@ -438,7 +438,7 @@ class WebSocket extends EventTarget {

async [_eventLoop]() {
while (this[_readyState] !== CLOSED) {
const { 0: kind, 1: value } = await core.opAsync2(
const { 0: kind, 1: value } = await opAsync2(
"op_ws_next_event",
this[_rid],
);
Expand Down Expand Up @@ -501,7 +501,7 @@ class WebSocket extends EventTarget {

if (prevState === OPEN) {
try {
await core.opAsync(
await opAsync(
"op_ws_close",
this[_rid],
code,
Expand Down Expand Up @@ -530,12 +530,12 @@ class WebSocket extends EventTarget {
clearTimeout(this[_idleTimeoutTimeout]);
this[_idleTimeoutTimeout] = setTimeout(async () => {
if (this[_readyState] === OPEN) {
await core.opAsync("op_ws_send_ping", this[_rid]);
await opAsync("op_ws_send_ping", this[_rid]);
this[_idleTimeoutTimeout] = setTimeout(async () => {
if (this[_readyState] === OPEN) {
this[_readyState] = CLOSING;
const reason = "No response from ping frame.";
await core.opAsync(
await opAsync(
"op_ws_close",
this[_rid],
1001,
Expand Down
2 changes: 1 addition & 1 deletion ext/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ pub async fn op_ws_close(
Ok(())
}

#[op(deferred)]
#[op(fast)]
pub async fn op_ws_next_event(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
Expand Down