From a3cd5b125556387a741c7c26f10c0604ea262b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 13 Jun 2020 23:07:31 +0200 Subject: [PATCH 1/3] wip dispatch op by name --- cli/js/globals.ts | 5 +++++ cli/js/ops/dispatch_minimal.ts | 8 ++++---- cli/js/ops/io.ts | 27 ++++----------------------- core/core.js | 9 ++++++++- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 09be63315b44a4..2b40ab7fa2bd4b 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -82,6 +82,11 @@ declare global { control: Uint8Array, ...zeroCopy: ArrayBufferView[] ): Uint8Array | null; + dispatchNew( + opName: string, + control: Uint8Array, + ...zeroCopy: ArrayBufferView[] + ): Uint8Array | null; setAsyncHandler(opId: number, cb: (msg: Uint8Array) => void): void; sharedQueue: { head(): number; diff --git a/cli/js/ops/dispatch_minimal.ts b/cli/js/ops/dispatch_minimal.ts index 570463583f0627..7a29fecee20b30 100644 --- a/cli/js/ops/dispatch_minimal.ts +++ b/cli/js/ops/dispatch_minimal.ts @@ -83,7 +83,7 @@ export function asyncMsgFromRust(ui8: Uint8Array): void { } export async function sendAsyncMinimal( - opId: number, + opName: string, arg: number, zeroCopy: Uint8Array ): Promise { @@ -92,7 +92,7 @@ export async function sendAsyncMinimal( scratch32[1] = arg; scratch32[2] = 0; // result const promise = util.createResolvable(); - const buf = core.dispatch(opId, scratchBytes, zeroCopy); + const buf = core.dispatchNew(opName, scratchBytes, zeroCopy); if (buf) { const record = recordFromBufMinimal(buf); // Sync result. @@ -107,13 +107,13 @@ export async function sendAsyncMinimal( } export function sendSyncMinimal( - opId: number, + opName: string, arg: number, zeroCopy: Uint8Array ): number { scratch32[0] = 0; // promiseId 0 indicates sync scratch32[1] = arg; - const res = core.dispatch(opId, scratchBytes, zeroCopy)!; + const res = core.dispatchNew(opName, scratchBytes, zeroCopy)!; const resRecord = recordFromBufMinimal(res); return unwrapResponse(resRecord); } diff --git a/cli/js/ops/io.ts b/cli/js/ops/io.ts index b4ef837e014dae..cc3d1d1701181c 100644 --- a/cli/js/ops/io.ts +++ b/cli/js/ops/io.ts @@ -1,22 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts"; -// TODO(bartlomieju): remove this import and maybe lazy-initialize -// OPS_CACHE that belongs only to this module -import { OPS_CACHE } from "../runtime.ts"; - -// This is done because read/write are extremely performance sensitive. -let OP_READ = -1; -let OP_WRITE = -1; export function readSync(rid: number, buffer: Uint8Array): number | null { if (buffer.length == 0) { return 0; } - if (OP_READ < 0) { - OP_READ = OPS_CACHE["op_read"]; - } - const nread = sendSyncMinimal(OP_READ, rid, buffer); + const nread = sendSyncMinimal("op_read", rid, buffer); if (nread < 0) { throw new Error("read error"); } else if (nread == 0) { @@ -33,10 +23,7 @@ export async function read( if (buffer.length == 0) { return 0; } - if (OP_READ < 0) { - OP_READ = OPS_CACHE["op_read"]; - } - const nread = await sendAsyncMinimal(OP_READ, rid, buffer); + const nread = await sendAsyncMinimal("op_read", rid, buffer); if (nread < 0) { throw new Error("read error"); } else if (nread == 0) { @@ -47,10 +34,7 @@ export async function read( } export function writeSync(rid: number, data: Uint8Array): number { - if (OP_WRITE < 0) { - OP_WRITE = OPS_CACHE["op_write"]; - } - const result = sendSyncMinimal(OP_WRITE, rid, data); + const result = sendSyncMinimal("op_write", rid, data); if (result < 0) { throw new Error("write error"); } else { @@ -59,10 +43,7 @@ export function writeSync(rid: number, data: Uint8Array): number { } export async function write(rid: number, data: Uint8Array): Promise { - if (OP_WRITE < 0) { - OP_WRITE = OPS_CACHE["op_write"]; - } - const result = await sendAsyncMinimal(OP_WRITE, rid, data); + const result = await sendAsyncMinimal("op_write", rid, data); if (result < 0) { throw new Error("write error"); } else { diff --git a/core/core.js b/core/core.js index 23cc325abfeabe..f97e6319812e90 100644 --- a/core/core.js +++ b/core/core.js @@ -37,6 +37,7 @@ SharedQueue Binary Layout let asyncHandlers; let initialized = false; + const opsCache = {}; function maybeInit() { if (!initialized) { @@ -61,7 +62,8 @@ SharedQueue Binary Layout // op id 0 is a special value to retrieve the map of registered ops. const opsMapBytes = send(0, new Uint8Array([])); const opsMapJson = String.fromCharCode.apply(null, opsMapBytes); - return JSON.parse(opsMapJson); + Object.assign(opsCache, JSON.parse(opsMapJson)); + return { ...opsCache }; } function assert(cond) { @@ -181,9 +183,14 @@ SharedQueue Binary Layout } } + function dispatch(opName, control, zeroCopy) { + return send(opsCache[opName], control, zeroCopy); + } + Object.assign(window.Deno.core, { setAsyncHandler, dispatch: send, + dispatchNew: dispatch, ops, // sharedQueue is private but exposed for testing. sharedQueue: { From 3c07bfc2b6016a1f400e135c4cc637b88872a489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 20 Jun 2020 15:35:27 +0200 Subject: [PATCH 2/3] cleanup --- cli/js/globals.ts | 2 +- cli/js/ops/dispatch_json.ts | 13 ++++--------- cli/js/ops/dispatch_minimal.ts | 4 ++-- cli/js/runtime.ts | 6 ++---- core/core.js | 6 +++--- 5 files changed, 12 insertions(+), 19 deletions(-) diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 2b40ab7fa2bd4b..5746b224f8bcd2 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -82,7 +82,7 @@ declare global { control: Uint8Array, ...zeroCopy: ArrayBufferView[] ): Uint8Array | null; - dispatchNew( + dispatchByName( opName: string, control: Uint8Array, ...zeroCopy: ArrayBufferView[] diff --git a/cli/js/ops/dispatch_json.ts b/cli/js/ops/dispatch_json.ts index 6292c188a339ac..6a91f6a4f12fdb 100644 --- a/cli/js/ops/dispatch_json.ts +++ b/cli/js/ops/dispatch_json.ts @@ -1,7 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as util from "../util.ts"; import { core } from "../core.ts"; -import { OPS_CACHE } from "../runtime.ts"; import { ErrorKind, getErrorClass } from "../errors.ts"; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -61,12 +60,10 @@ export function sendSync( args: object = {}, ...zeroCopy: Uint8Array[] ): Ok { - const opId = OPS_CACHE[opName]; - util.log("sendSync", opName, opId); + util.log("sendSync", opName); const argsUi8 = encode(args); - const resUi8 = core.dispatch(opId, argsUi8, ...zeroCopy); + const resUi8 = core.dispatchByName(opName, argsUi8, ...zeroCopy); util.assert(resUi8 != null); - const res = decode(resUi8); util.assert(res.promiseId == null); return unwrapResponse(res); @@ -77,14 +74,12 @@ export async function sendAsync( args: object = {}, ...zeroCopy: Uint8Array[] ): Promise { - const opId = OPS_CACHE[opName]; - util.log("sendAsync", opName, opId); + util.log("sendAsync", opName); const promiseId = nextPromiseId(); args = Object.assign(args, { promiseId }); const promise = util.createResolvable(); - const argsUi8 = encode(args); - const buf = core.dispatch(opId, argsUi8, ...zeroCopy); + const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy); if (buf) { // Sync result. const res = decode(buf); diff --git a/cli/js/ops/dispatch_minimal.ts b/cli/js/ops/dispatch_minimal.ts index 7a29fecee20b30..0d1dac44e8be18 100644 --- a/cli/js/ops/dispatch_minimal.ts +++ b/cli/js/ops/dispatch_minimal.ts @@ -92,7 +92,7 @@ export async function sendAsyncMinimal( scratch32[1] = arg; scratch32[2] = 0; // result const promise = util.createResolvable(); - const buf = core.dispatchNew(opName, scratchBytes, zeroCopy); + const buf = core.dispatchByName(opName, scratchBytes, zeroCopy); if (buf) { const record = recordFromBufMinimal(buf); // Sync result. @@ -113,7 +113,7 @@ export function sendSyncMinimal( ): number { scratch32[0] = 0; // promiseId 0 indicates sync scratch32[1] = arg; - const res = core.dispatchNew(opName, scratchBytes, zeroCopy)!; + const res = core.dispatchByName(opName, scratchBytes, zeroCopy)!; const resRecord = recordFromBufMinimal(res); return unwrapResponse(resRecord); } diff --git a/cli/js/runtime.ts b/cli/js/runtime.ts index e16cee2282fad7..39d158f7150d10 100644 --- a/cli/js/runtime.ts +++ b/cli/js/runtime.ts @@ -9,8 +9,6 @@ import { setPrepareStackTrace } from "./error_stack.ts"; import { Start, opStart } from "./ops/runtime.ts"; import { handleTimerMacrotask } from "./web/timers.ts"; -export let OPS_CACHE: { [name: string]: number }; - function getAsyncHandler(opName: string): (msg: Uint8Array) => void { switch (opName) { case "op_write": @@ -24,8 +22,8 @@ function getAsyncHandler(opName: string): (msg: Uint8Array) => void { // TODO(bartlomieju): temporary solution, must be fixed when moving // dispatches to separate crates export function initOps(): void { - OPS_CACHE = core.ops(); - for (const [name, opId] of Object.entries(OPS_CACHE)) { + const opsMap = core.ops(); + for (const [name, opId] of Object.entries(opsMap)) { core.setAsyncHandler(opId, getAsyncHandler(name)); } core.setMacrotaskCallback(handleTimerMacrotask); diff --git a/core/core.js b/core/core.js index f97e6319812e90..0efbeeb0cc4aae 100644 --- a/core/core.js +++ b/core/core.js @@ -37,7 +37,7 @@ SharedQueue Binary Layout let asyncHandlers; let initialized = false; - const opsCache = {}; + let opsCache = {}; function maybeInit() { if (!initialized) { @@ -62,7 +62,7 @@ SharedQueue Binary Layout // op id 0 is a special value to retrieve the map of registered ops. const opsMapBytes = send(0, new Uint8Array([])); const opsMapJson = String.fromCharCode.apply(null, opsMapBytes); - Object.assign(opsCache, JSON.parse(opsMapJson)); + opsCache = JSON.parse(opsMapJson); return { ...opsCache }; } @@ -190,7 +190,7 @@ SharedQueue Binary Layout Object.assign(window.Deno.core, { setAsyncHandler, dispatch: send, - dispatchNew: dispatch, + dispatchByName: dispatch, ops, // sharedQueue is private but exposed for testing. sharedQueue: { From 39ce71b46ab0a3df815100474c1545a108324c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 20 Jun 2020 15:48:21 +0200 Subject: [PATCH 3/3] fix --- core/core.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/core.js b/core/core.js index 0efbeeb0cc4aae..b3c0ddc13cc5d4 100644 --- a/core/core.js +++ b/core/core.js @@ -183,8 +183,8 @@ SharedQueue Binary Layout } } - function dispatch(opName, control, zeroCopy) { - return send(opsCache[opName], control, zeroCopy); + function dispatch(opName, control, ...zeroCopy) { + return send(opsCache[opName], control, ...zeroCopy); } Object.assign(window.Deno.core, {