Skip to content

Commit d043a6d

Browse files
perf(ext/websocket): various performance improvements (#18862)
- No need to wrap buffer in a `new DataView()` - Deferred ops are still eagerly polled, but resolved on the next tick of the event loop, we don't want them to be eagerly polled - Using "core.opAsync"/"core.opAsync2" incurs additional cost of looking up these functions on each call. Similarly with "ops.*" --------- Co-authored-by: Divy Srivastava <[email protected]>
1 parent 1e331a4 commit d043a6d

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

ext/websocket/01_websocket.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
/// <reference path="../../core/internal.d.ts" />
44

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

213-
ops.op_ws_check_permission_and_cancel_handle(
216+
op_ws_check_permission_and_cancel_handle(
214217
"WebSocket.abort()",
215218
this[_url],
216219
false,
@@ -247,7 +250,7 @@ class WebSocket extends EventTarget {
247250
}
248251

249252
PromisePrototypeThen(
250-
core.opAsync(
253+
opAsync(
251254
"op_ws_create",
252255
"new WebSocket()",
253256
wsURL.href,
@@ -260,7 +263,7 @@ class WebSocket extends EventTarget {
260263

261264
if (this[_readyState] === CLOSING) {
262265
PromisePrototypeThen(
263-
core.opAsync("op_ws_close", this[_rid]),
266+
opAsync("op_ws_close", this[_rid]),
264267
() => {
265268
this[_readyState] = CLOSED;
266269

@@ -316,7 +319,7 @@ class WebSocket extends EventTarget {
316319
const sendTypedArray = (view, byteLength) => {
317320
this[_bufferedAmount] += byteLength;
318321
PromisePrototypeThen(
319-
core.opAsync2(
322+
opAsync2(
320323
"op_ws_send_binary",
321324
this[_rid],
322325
view,
@@ -345,16 +348,13 @@ class WebSocket extends EventTarget {
345348
sendTypedArray(data, TypedArrayPrototypeGetByteLength(data));
346349
}
347350
} else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
348-
sendTypedArray(
349-
new DataView(data),
350-
ArrayBufferPrototypeGetByteLength(data),
351-
);
351+
sendTypedArray(data, ArrayBufferPrototypeGetByteLength(data));
352352
} else {
353353
const string = String(data);
354354
const d = core.encode(string);
355355
this[_bufferedAmount] += TypedArrayPrototypeGetByteLength(d);
356356
PromisePrototypeThen(
357-
core.opAsync2(
357+
opAsync2(
358358
"op_ws_send_text",
359359
this[_rid],
360360
string,
@@ -413,7 +413,7 @@ class WebSocket extends EventTarget {
413413
this[_readyState] = CLOSING;
414414

415415
PromisePrototypeCatch(
416-
core.opAsync(
416+
opAsync(
417417
"op_ws_close",
418418
this[_rid],
419419
code,
@@ -438,7 +438,7 @@ class WebSocket extends EventTarget {
438438

439439
async [_eventLoop]() {
440440
while (this[_readyState] !== CLOSED) {
441-
const { 0: kind, 1: value } = await core.opAsync2(
441+
const { 0: kind, 1: value } = await opAsync2(
442442
"op_ws_next_event",
443443
this[_rid],
444444
);
@@ -501,7 +501,7 @@ class WebSocket extends EventTarget {
501501

502502
if (prevState === OPEN) {
503503
try {
504-
await core.opAsync(
504+
await opAsync(
505505
"op_ws_close",
506506
this[_rid],
507507
code,
@@ -530,12 +530,12 @@ class WebSocket extends EventTarget {
530530
clearTimeout(this[_idleTimeoutTimeout]);
531531
this[_idleTimeoutTimeout] = setTimeout(async () => {
532532
if (this[_readyState] === OPEN) {
533-
await core.opAsync("op_ws_send_ping", this[_rid]);
533+
await opAsync("op_ws_send_ping", this[_rid]);
534534
this[_idleTimeoutTimeout] = setTimeout(async () => {
535535
if (this[_readyState] === OPEN) {
536536
this[_readyState] = CLOSING;
537537
const reason = "No response from ping frame.";
538-
await core.opAsync(
538+
await opAsync(
539539
"op_ws_close",
540540
this[_rid],
541541
1001,

ext/websocket/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub async fn op_ws_close(
427427
Ok(())
428428
}
429429

430-
#[op(deferred)]
430+
#[op(fast)]
431431
pub async fn op_ws_next_event(
432432
state: Rc<RefCell<OpState>>,
433433
rid: ResourceId,

0 commit comments

Comments
 (0)