1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

perf: remove knowledge of promise IDs from deno (#21132)

We can move all promise ID knowledge to deno_core, allowing us to better
experiment with promise implementation in deno_core.

`{un,}refOpPromise(promise)` is equivalent to
`{un,}refOp(promise[promiseIdSymbol])`
This commit is contained in:
Matt Mastracci 2023-11-09 13:57:26 -07:00 committed by GitHub
parent c4029f6af2
commit 9010b8df53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 70 additions and 124 deletions

55
Cargo.lock generated
View file

@ -1084,19 +1084,17 @@ dependencies = [
[[package]]
name = "deno_core"
version = "0.228.0"
version = "0.229.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78996b42de9975a052cfc9234be39eabf3d6f7721e7cfe79f14a5bd0a252b552"
checksum = "6bba7ed998f57ecd03640a82e6ddef281328b6d4c48c55e9e17cd906bab08020"
dependencies = [
"anyhow",
"bytes",
"deno_ops",
"deno_unsync 0.3.0",
"futures",
"indexmap 2.0.2",
"libc",
"log",
"once_cell",
"parking_lot 0.12.1",
"pin-project",
"serde",
@ -1510,18 +1508,13 @@ dependencies = [
[[package]]
name = "deno_ops"
version = "0.104.0"
version = "0.105.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06291034a0ad5293efcfa01826e4af6adabdfd513b766e2f2cc60dd6c75a94f"
checksum = "32976e42a50a1ac64d065a9219f5daf82a3ad6938da9d4aa3071890c08e1cd97"
dependencies = [
"lazy-regex",
"once_cell",
"pmutil",
"proc-macro-crate",
"proc-macro-rules",
"proc-macro2",
"quote",
"regex",
"strum",
"strum_macros",
"syn 2.0.37",
@ -3943,15 +3936,6 @@ dependencies = [
"elliptic-curve",
]
[[package]]
name = "proc-macro-crate"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8"
dependencies = [
"toml_edit",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -4756,15 +4740,14 @@ dependencies = [
[[package]]
name = "serde_v8"
version = "0.137.0"
version = "0.138.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c22cbbd634a5b13e9c6a0c6718ae9a8da3696aec5e5eff48fb90e4c9be0809e"
checksum = "add36cea4acc8cbfa4a1614a9e985e1057fd6748b672c8b4c4496f889d25e539"
dependencies = [
"bytes",
"derive_more",
"num-bigint",
"serde",
"serde_bytes",
"smallvec",
"thiserror",
"v8",
@ -5844,23 +5827,6 @@ dependencies = [
"serde",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
[[package]]
name = "toml_edit"
version = "0.20.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81"
dependencies = [
"indexmap 2.0.2",
"toml_datetime",
"winnow",
]
[[package]]
name = "tower"
version = "0.4.13"
@ -6548,15 +6514,6 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winnow"
version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc"
dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.50.0"

View file

@ -40,7 +40,7 @@ repository = "https://github.com/denoland/deno"
[workspace.dependencies]
deno_ast = { version = "0.31.2", features = ["transpiling"] }
deno_core = { version = "0.228.0" }
deno_core = { version = "0.229.0" }
deno_runtime = { version = "0.130.0", path = "./runtime" }
napi_sym = { version = "0.52.0", path = "./cli/napi/sym" }

View file

@ -2,11 +2,11 @@
import { assertNotEquals, execCode } from "./test_util.ts";
Deno.test("[unrefOp] unref'ing invalid ops does not have effects", async () => {
Deno.test("[unrefOpPromise] unref'ing invalid ops does not have effects", async () => {
const [statusCode, _] = await execCode(`
Deno[Deno.internal].core.unrefOp(-1);
Deno[Deno.internal].core.unrefOpPromise(new Promise(r => null));
setTimeout(() => { throw new Error() }, 10)
`);
// Invalid unrefOp call doesn't affect exit condition of event loop
// Invalid unrefOpPromise call doesn't affect exit condition of event loop
assertNotEquals(statusCode, 0);
});

View file

@ -32,7 +32,6 @@ const {
SafeMap,
SafeArrayIterator,
SafeWeakMap,
SymbolFor,
} = primordials;
import { pathFromURL } from "ext:deno_web/00_infra.js";
@ -52,8 +51,6 @@ function getBufferSourceByteLength(source) {
}
return ArrayBufferPrototypeGetByteLength(source);
}
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
const U32_BUFFER = new Uint32Array(2);
const U64_BUFFER = new BigUint64Array(TypedArrayPrototypeGetBuffer(U32_BUFFER));
const I64_BUFFER = new BigInt64Array(TypedArrayPrototypeGetBuffer(U32_BUFFER));
@ -422,7 +419,7 @@ class UnsafeCallback {
if (this.#refcount++ === 0) {
if (this.#refpromise) {
// Re-refing
core.refOp(this.#refpromise[promiseIdSymbol]);
core.refOpPromise(this.#refpromise);
} else {
this.#refpromise = core.opAsync(
"op_ffi_unsafe_callback_ref",
@ -437,7 +434,7 @@ class UnsafeCallback {
// Only decrement refcount if it is positive, and only
// unref the callback if refcount reaches zero.
if (this.#refcount > 0 && --this.#refcount === 0) {
core.unrefOp(this.#refpromise[promiseIdSymbol]);
core.unrefOpPromise(this.#refpromise);
}
return this.#refcount;
}

View file

@ -44,7 +44,6 @@ const {
ObjectPrototypeIsPrototypeOf,
PromisePrototypeCatch,
Symbol,
SymbolFor,
TypeError,
Uint8Array,
Uint8ArrayPrototype,
@ -642,7 +641,6 @@ function serveHttpOnConnection(connection, signal, handler, onError, onListen) {
function serveHttpOn(context, callback) {
let ref = true;
let currentPromise = null;
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
const promiseErrorHandler = (error) => {
// Abnormal exit
@ -666,7 +664,7 @@ function serveHttpOn(context, callback) {
}
currentPromise = op_http_wait(rid);
if (!ref) {
core.unrefOp(currentPromise[promiseIdSymbol]);
core.unrefOpPromise(currentPromise);
}
req = await currentPromise;
currentPromise = null;
@ -708,13 +706,13 @@ function serveHttpOn(context, callback) {
ref() {
ref = true;
if (currentPromise) {
core.refOp(currentPromise[promiseIdSymbol]);
core.refOpPromise(currentPromise);
}
},
unref() {
ref = false;
if (currentPromise) {
core.unrefOp(currentPromise[promiseIdSymbol]);
core.unrefOpPromise(currentPromise);
}
},
[SymbolAsyncDispose]() {

View file

@ -13,23 +13,21 @@ import { SymbolDispose } from "ext:deno_web/00_infra.js";
const primordials = globalThis.__bootstrap.primordials;
const {
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypePush,
Error,
Number,
ObjectPrototypeIsPrototypeOf,
PromiseResolve,
SafeSet,
SetPrototypeAdd,
SetPrototypeDelete,
SetPrototypeForEach,
SymbolAsyncIterator,
Symbol,
SymbolFor,
TypeError,
TypedArrayPrototypeSubarray,
Uint8Array,
} = primordials;
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
async function write(rid, data) {
return await core.write(rid, data);
}
@ -70,7 +68,7 @@ class Conn {
#remoteAddr = null;
#localAddr = null;
#unref = false;
#pendingReadPromiseIds = [];
#pendingReadPromises = new SafeSet();
#readable;
#writable;
@ -102,19 +100,15 @@ class Conn {
return 0;
}
const promise = core.read(this.rid, buffer);
const promiseId = promise[promiseIdSymbol];
if (this.#unref) core.unrefOp(promiseId);
ArrayPrototypePush(this.#pendingReadPromiseIds, promiseId);
if (this.#unref) core.unrefOpPromise(promise);
SetPrototypeAdd(this.#pendingReadPromises, promise);
let nread;
try {
nread = await promise;
} catch (e) {
throw e;
} finally {
this.#pendingReadPromiseIds = ArrayPrototypeFilter(
this.#pendingReadPromiseIds,
(id) => id !== promiseId,
);
SetPrototypeDelete(this.#pendingReadPromises, promise);
}
return nread === 0 ? null : nread;
}
@ -149,7 +143,11 @@ class Conn {
if (this.#readable) {
readableStreamForRidUnrefableRef(this.#readable);
}
ArrayPrototypeForEach(this.#pendingReadPromiseIds, (id) => core.refOp(id));
SetPrototypeForEach(
this.#pendingReadPromises,
(promise) => core.refOpPromise(promise),
);
}
unref() {
@ -157,9 +155,9 @@ class Conn {
if (this.#readable) {
readableStreamForRidUnrefableUnref(this.#readable);
}
ArrayPrototypeForEach(
this.#pendingReadPromiseIds,
(id) => core.unrefOp(id),
SetPrototypeForEach(
this.#pendingReadPromises,
(promise) => core.unrefOpPromise(promise),
);
}
@ -184,7 +182,7 @@ class Listener {
#rid = 0;
#addr = null;
#unref = false;
#promiseId = null;
#promise = null;
constructor(rid, addr) {
this.#rid = rid;
@ -211,10 +209,10 @@ class Listener {
default:
throw new Error(`Unsupported transport: ${this.addr.transport}`);
}
this.#promiseId = promise[promiseIdSymbol];
if (this.#unref) core.unrefOp(this.#promiseId);
this.#promise = promise;
if (this.#unref) core.unrefOpPromise(promise);
const { 0: rid, 1: localAddr, 2: remoteAddr } = await promise;
this.#promiseId = null;
this.#promise = null;
if (this.addr.transport == "tcp") {
localAddr.transport = "tcp";
remoteAddr.transport = "tcp";
@ -265,15 +263,15 @@ class Listener {
ref() {
this.#unref = false;
if (typeof this.#promiseId === "number") {
core.refOp(this.#promiseId);
if (this.#promise !== null) {
core.refOpPromise(this.#promise);
}
}
unref() {
this.#unref = true;
if (typeof this.#promiseId === "number") {
core.unrefOp(this.#promiseId);
if (this.#promise !== null) {
core.unrefOpPromise(this.#promise);
}
}
}

View file

@ -68,7 +68,7 @@ const kDenoResponse = Symbol("kDenoResponse");
const kDenoRid = Symbol("kDenoRid");
const kDenoClientRid = Symbol("kDenoClientRid");
const kDenoConnRid = Symbol("kDenoConnRid");
const kPollConnPromiseId = Symbol("kPollConnPromiseId");
const kPollConnPromise = Symbol("kPollConnPromise");
const STREAM_FLAGS_PENDING = 0x0;
const STREAM_FLAGS_READY = 0x1;
@ -364,7 +364,7 @@ export class ClientHttp2Session extends Http2Session {
this[kPendingRequestCalls] = null;
this[kDenoClientRid] = undefined;
this[kDenoConnRid] = undefined;
this[kPollConnPromiseId] = undefined;
this[kPollConnPromise] = undefined;
socket.on("error", socketOnError);
socket.on("close", socketOnClose);
@ -394,8 +394,7 @@ export class ClientHttp2Session extends Http2Session {
"op_http2_poll_client_connection",
this[kDenoConnRid],
);
this[kPollConnPromiseId] =
promise[Symbol.for("Deno.core.internalPromiseId")];
this[kPollConnPromise] = promise;
if (!this.#refed) {
this.unref();
}
@ -410,15 +409,15 @@ export class ClientHttp2Session extends Http2Session {
ref() {
this.#refed = true;
if (this[kPollConnPromiseId]) {
core.refOp(this[kPollConnPromiseId]);
if (this[kPollConnPromise]) {
core.refOpPromise(this[kPollConnPromise]);
}
}
unref() {
this.#refed = false;
if (this[kPollConnPromiseId]) {
core.unrefOp(this[kPollConnPromiseId]);
if (this[kPollConnPromise]) {
core.unrefOpPromise(this[kPollConnPromise]);
}
}

View file

@ -16,7 +16,6 @@ const {
PromisePrototypeThen,
SafeArrayIterator,
SafeMap,
SymbolFor,
TypedArrayPrototypeGetBuffer,
TypeError,
indirectEval,
@ -75,7 +74,7 @@ function handleTimerMacrotask() {
* The keys in this map correspond to the key ID's in the spec's map of active
* timers. The values are the timeout's cancel rid.
*
* @type {Map<number, { cancelRid: number, isRef: boolean, promiseId: number }>}
* @type {Map<number, { cancelRid: number, isRef: boolean, promise: Promise<void> }>}
*/
const activeTimers = new SafeMap();
@ -114,7 +113,7 @@ function initializeTimer(
// https://github.com/whatwg/html/issues/7358
id = nextId++;
const cancelRid = ops.op_timer_handle();
timerInfo = { cancelRid, isRef: true, promiseId: -1 };
timerInfo = { cancelRid, isRef: true, promise: null };
// Step 4 in "run steps after a timeout".
MapPrototypeSet(activeTimers, id, timerInfo);
@ -216,7 +215,7 @@ const scheduledTimers = { head: null, tail: null };
* @param { {action: () => void, nestingLevel: number}[] } task Will be run
* after the timeout, if it hasn't been cancelled.
* @param {number} millis
* @param {{ cancelRid: number, isRef: boolean, promiseId: number }} timerInfo
* @param {{ cancelRid: number, isRef: boolean, promise: Promise<void> }} timerInfo
*/
function runAfterTimeout(task, millis, timerInfo) {
const cancelRid = timerInfo.cancelRid;
@ -230,9 +229,9 @@ function runAfterTimeout(task, millis, timerInfo) {
} else {
sleepPromise = op_sleep(millis, cancelRid);
}
timerInfo.promiseId = sleepPromise[SymbolFor("Deno.core.internalPromiseId")];
timerInfo.promise = sleepPromise;
if (!timerInfo.isRef) {
core.unrefOp(timerInfo.promiseId);
core.unrefOpPromise(timerInfo.promise);
}
/** @type {ScheduledTimer} */
@ -376,7 +375,7 @@ function refTimer(id) {
return;
}
timerInfo.isRef = true;
core.refOp(timerInfo.promiseId);
core.refOpPromise(timerInfo.promise);
}
function unrefTimer(id) {
@ -385,7 +384,7 @@ function unrefTimer(id) {
return;
}
timerInfo.isRef = false;
core.unrefOp(timerInfo.promiseId);
core.unrefOpPromise(timerInfo.promise);
}
export {

View file

@ -967,7 +967,7 @@ function readableStreamForRid(rid, autoClose = true) {
return stream;
}
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
const promiseSymbol = SymbolFor("__promise");
const _isUnref = Symbol("isUnref");
/**
* Create a new ReadableStream object that is backed by a Resource that
@ -981,7 +981,7 @@ const _isUnref = Symbol("isUnref");
*/
function readableStreamForRidUnrefable(rid) {
const stream = new ReadableStream(_brand);
stream[promiseIdSymbol] = undefined;
stream[promiseSymbol] = undefined;
stream[_isUnref] = false;
stream[_resourceBackingUnrefable] = { rid, autoClose: true };
const underlyingSource = {
@ -990,10 +990,10 @@ function readableStreamForRidUnrefable(rid) {
const v = controller.byobRequest.view;
try {
const promise = core.read(rid, v);
const promiseId = stream[promiseIdSymbol] = promise[promiseIdSymbol];
if (stream[_isUnref]) core.unrefOp(promiseId);
stream[promiseSymbol] = promise;
if (stream[_isUnref]) core.unrefOpPromise(promise);
const bytesRead = await promise;
stream[promiseIdSymbol] = undefined;
stream[promiseSymbol] = undefined;
if (bytesRead === 0) {
core.tryClose(rid);
controller.close();
@ -1030,8 +1030,8 @@ function readableStreamForRidUnrefableRef(stream) {
throw new TypeError("Not an unrefable stream");
}
stream[_isUnref] = false;
if (stream[promiseIdSymbol] !== undefined) {
core.refOp(stream[promiseIdSymbol]);
if (stream[promiseSymbol] !== undefined) {
core.refOpPromise(stream[promiseSymbol]);
}
}
@ -1040,8 +1040,8 @@ function readableStreamForRidUnrefableUnref(stream) {
throw new TypeError("Not an unrefable stream");
}
stream[_isUnref] = true;
if (stream[promiseIdSymbol] !== undefined) {
core.unrefOp(stream[promiseIdSymbol]);
if (stream[promiseSymbol] !== undefined) {
core.unrefOpPromise(stream[promiseSymbol]);
}
}
@ -1064,10 +1064,11 @@ async function readableStreamCollectIntoUint8Array(stream) {
readableStreamDisturb(stream);
const promise = core.opAsync("op_read_all", resourceBacking.rid);
if (readableStreamIsUnrefable(stream)) {
const promiseId = stream[promiseIdSymbol] = promise[promiseIdSymbol];
if (stream[_isUnref]) core.unrefOp(promiseId);
stream[promiseSymbol] = promise;
if (stream[_isUnref]) core.unrefOpPromise(promise);
}
const buf = await promise;
stream[promiseSymbol] = undefined;
readableStreamThrowIfErrored(stream);
readableStreamClose(stream);
return buf;

View file

@ -13,7 +13,6 @@ const {
ObjectPrototypeIsPrototypeOf,
PromisePrototypeThen,
SafePromiseAll,
SymbolFor,
Symbol,
} = primordials;
import { FsFile } from "ext:deno_fs/30_fs.js";
@ -148,7 +147,6 @@ function run({
}
const illegalConstructorKey = Symbol("illegalConstructorKey");
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
function spawnChildInner(opFn, command, apiName, {
args = [],
@ -203,7 +201,7 @@ function collectOutput(readableStream) {
class ChildProcess {
#rid;
#waitPromiseId;
#waitPromise;
#waitComplete = false;
#pid;
@ -266,7 +264,7 @@ class ChildProcess {
signal?.[abortSignal.add](onAbort);
const waitPromise = core.opAsync("op_spawn_wait", this.#rid);
this.#waitPromiseId = waitPromise[promiseIdSymbol];
this.#waitPromise = waitPromise;
this.#status = PromisePrototypeThen(waitPromise, (res) => {
signal?.[abortSignal.remove](onAbort);
this.#waitComplete = true;
@ -333,13 +331,13 @@ class ChildProcess {
}
ref() {
core.refOp(this.#waitPromiseId);
core.refOpPromise(this.#waitPromise);
if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout);
if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr);
}
unref() {
core.unrefOp(this.#waitPromiseId);
core.unrefOpPromise(this.#waitPromise);
if (this.#stdout) readableStreamForRidUnrefableUnref(this.#stdout);
if (this.#stderr) readableStreamForRidUnrefableUnref(this.#stderr);
}

View file

@ -8,7 +8,6 @@ const {
SafeSetIterator,
SetPrototypeAdd,
SetPrototypeDelete,
SymbolFor,
TypeError,
} = primordials;
@ -18,7 +17,7 @@ function bindSignal(signo) {
function pollSignal(rid) {
const promise = core.opAsync("op_signal_poll", rid);
core.unrefOp(promise[SymbolFor("Deno.core.internalPromiseId")]);
core.unrefOpPromise(promise);
return promise;
}