diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-11-09 13:57:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-09 13:57:26 -0700 |
commit | 9010b8df53cd37f0410e08c43a194667974686a2 (patch) | |
tree | 97c13d696ba1216e74b745f5ce1b25ed34afa2cd /ext | |
parent | c4029f6af22b373bf22383453cb4e2159f3b5b72 (diff) |
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])`
Diffstat (limited to 'ext')
-rw-r--r-- | ext/ffi/00_ffi.js | 7 | ||||
-rw-r--r-- | ext/http/00_serve.js | 8 | ||||
-rw-r--r-- | ext/net/01_net.js | 50 | ||||
-rw-r--r-- | ext/node/polyfills/http2.ts | 15 | ||||
-rw-r--r-- | ext/web/02_timers.js | 15 | ||||
-rw-r--r-- | ext/web/06_streams.js | 23 |
6 files changed, 55 insertions, 63 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index fe7344e17..233935be9 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -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; } diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 5b931ccd1..fbd2014a7 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -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]() { diff --git a/ext/net/01_net.js b/ext/net/01_net.js index 05aca07e5..b4b8ca218 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -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); } } } diff --git a/ext/node/polyfills/http2.ts b/ext/node/polyfills/http2.ts index 9ebdabb79..50d85f694 100644 --- a/ext/node/polyfills/http2.ts +++ b/ext/node/polyfills/http2.ts @@ -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]); } } diff --git a/ext/web/02_timers.js b/ext/web/02_timers.js index aa7e74673..ee974345b 100644 --- a/ext/web/02_timers.js +++ b/ext/web/02_timers.js @@ -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 { diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index 66be90a61..c5306ca9c 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -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; |