diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-02-24 01:20:15 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-23 19:50:15 +0000 |
commit | da781280b8422b4116473b366fb7d207909a31da (patch) | |
tree | bef59ac9dd8e26d97d7d494de87d53ab537724cc /core/01_core.js | |
parent | 4773d07974167b565ba84d5603a6d6f70cd12660 (diff) |
fix(core): remove async op inlining optimization (#17899)
Runtime generation of async op wrappers contributed to increased startup
time and core became unusable with
`--disallow-code-generation-from-strings` flag. The optimization only
affects very small microbenchmarks so this revert will not cause any
regressions.
Diffstat (limited to 'core/01_core.js')
-rw-r--r-- | core/01_core.js | 87 |
1 files changed, 18 insertions, 69 deletions
diff --git a/core/01_core.js b/core/01_core.js index bea9c4290..5a622b0ea 100644 --- a/core/01_core.js +++ b/core/01_core.js @@ -10,24 +10,19 @@ TypeError, URIError, Array, - ArrayFrom, ArrayPrototypeFill, - ArrayPrototypeJoin, ArrayPrototypePush, ArrayPrototypeMap, ErrorCaptureStackTrace, - Function, Promise, ObjectAssign, ObjectFromEntries, - ObjectPrototypeHasOwnProperty, Map, MapPrototypeGet, MapPrototypeHas, MapPrototypeDelete, MapPrototypeSet, PromisePrototypeThen, - ReflectApply, SafePromisePrototypeFinally, StringPrototypeSlice, SymbolFor, @@ -175,61 +170,20 @@ return nextPromiseId++; } - // Generate async op wrappers. See core/bindings.rs - function initializeAsyncOps() { - function genAsyncOp(op, name, args) { - return new Function( - "setPromise", - "getPromise", - "promiseIdSymbol", - "rollPromiseId", - "handleOpCallTracing", - "op", - "unwrapOpResult", - "PromisePrototypeThen", - ` - return function ${name}(${args}) { - const id = rollPromiseId(); - let promise = PromisePrototypeThen(setPromise(id), unwrapOpResult); - try { - op(id, ${args}); - } catch (err) { - // Cleanup the just-created promise - getPromise(id); - // Rethrow the error - throw err; - } - promise = handleOpCallTracing("${name}", id, promise); - promise[promiseIdSymbol] = id; - return promise; - } - `, - )( - setPromise, - getPromise, - promiseIdSymbol, - rollPromiseId, - handleOpCallTracing, - op, - unwrapOpResult, - PromisePrototypeThen, - ); - } - - // { <name>: <argc>, ... } - const info = ops.asyncOpsInfo(); - for (const name in info) { - if (!ObjectPrototypeHasOwnProperty(info, name)) { - continue; - } - const argc = info[name]; - const op = ops[name]; - const args = ArrayPrototypeJoin( - ArrayFrom({ length: argc }, (_, i) => `arg${i}`), - ", ", - ); - ops[name] = genAsyncOp(op, name, args); + function opAsync(name, ...args) { + const id = rollPromiseId(); + let promise = PromisePrototypeThen(setPromise(id), unwrapOpResult); + try { + ops[name](id, ...args); + } catch (err) { + // Cleanup the just-created promise + getPromise(id); + // Rethrow the error + throw err; } + promise = handleOpCallTracing(name, id, promise); + promise[promiseIdSymbol] = id; + return promise; } function handleOpCallTracing(opName, promiseId, p) { @@ -245,10 +199,6 @@ } } - function opAsync(opName, ...args) { - return ReflectApply(ops[opName], ops, args); - } - function refOp(promiseId) { if (!hasPromise(promiseId)) { return; @@ -401,7 +351,6 @@ // Extra Deno.core.* exports const core = ObjectAssign(globalThis.Deno.core, { opAsync, - initializeAsyncOps, resources, metrics, registerErrorBuilder, @@ -421,11 +370,11 @@ setPromiseHooks, close: (rid) => ops.op_close(rid), tryClose: (rid) => ops.op_try_close(rid), - read: (rid, buffer) => ops.op_read(rid, buffer), - readAll: (rid) => ops.op_read_all(rid), - write: (rid, buffer) => ops.op_write(rid, buffer), - writeAll: (rid, buffer) => ops.op_write_all(rid, buffer), - shutdown: (rid) => ops.op_shutdown(rid), + read: opAsync.bind(null, "op_read"), + readAll: opAsync.bind(null, "op_read_all"), + write: opAsync.bind(null, "op_write"), + writeAll: opAsync.bind(null, "op_write_all"), + shutdown: opAsync.bind(null, "op_shutdown"), print: (msg, isErr) => ops.op_print(msg, isErr), setMacrotaskCallback: (fn) => ops.op_set_macrotask_callback(fn), setNextTickCallback: (fn) => ops.op_set_next_tick_callback(fn), |