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/web | |
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/web')
-rw-r--r-- | ext/web/02_timers.js | 15 | ||||
-rw-r--r-- | ext/web/06_streams.js | 23 |
2 files changed, 19 insertions, 19 deletions
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; |