perf(graph): test a single provided token directly in the return-contract check
A @Provider or @Step provides one token, so the check no longer goes through every() and its callback. On a 3-step pipeline the 4-round A/B against the freeze commit showed -4.6% on Deno and -3.2% on Bun, both outside the noise; with this change and 8 rounds every case overlaps. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 file changed
+13
-2
+13
-2
@@ -575,8 +575,7 @@ function emptyRegistry(): Registry {
|
|||||||
* so a plain step pays a type test rather than an extra microtask on the hot path.
|
* so a plain step pays a type test rather than an extra microtask on the hot path.
|
||||||
*/
|
*/
|
||||||
function keyedRunner(kind: string, name: string, provides: readonly string[], run: Runner): Runner {
|
function keyedRunner(kind: string, name: string, provides: readonly string[], run: Runner): Runner {
|
||||||
const check = (output: unknown): unknown => {
|
const fail = (output: unknown): never => {
|
||||||
if (output !== null && typeof output === 'object' && provides.every((token) => token in output)) return output;
|
|
||||||
const example = provides.map((token) => `${token}: <value>`).join(', ');
|
const example = provides.map((token) => `${token}: <value>`).join(', ');
|
||||||
throw new TypeError(
|
throw new TypeError(
|
||||||
`${kind} '${name}' returned ${describeOutput(output)}, but it must return an object holding what it ` +
|
`${kind} '${name}' returned ${describeOutput(output)}, but it must return an object holding what it ` +
|
||||||
@@ -584,6 +583,18 @@ function keyedRunner(kind: string, name: string, provides: readonly string[], ru
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// A @Provider or @Step provides one token: test it directly rather than through `every` and its
|
||||||
|
// callback, which measured as a 3-4% drop on a 3-step pipeline under Deno and Bun.
|
||||||
|
const [only] = provides;
|
||||||
|
const check =
|
||||||
|
provides.length === 1
|
||||||
|
? (output: unknown): unknown =>
|
||||||
|
output !== null && typeof output === 'object' && only! in output ? output : fail(output)
|
||||||
|
: (output: unknown): unknown =>
|
||||||
|
output !== null && typeof output === 'object' && provides.every((token) => token in output)
|
||||||
|
? output
|
||||||
|
: fail(output);
|
||||||
|
|
||||||
return (ctx) => {
|
return (ctx) => {
|
||||||
const output = run(ctx);
|
const output = run(ctx);
|
||||||
return output instanceof Promise ? output.then(check) : check(output);
|
return output instanceof Promise ? output.then(check) : check(output);
|
||||||
|
|||||||
Reference in new issue
Block a user