perf(test): use fast ops for deno test register (#20670)

Use fast ops for test registration. This speeds up `Deno.test` and
`t.step()` significantly (2x over Deno 1.37.0).
This commit is contained in:
Luca Casonato 2023-09-26 11:21:18 +09:00 committed by GitHub
parent a879f8c9fa
commit 8e1304ced4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 139 additions and 84 deletions

View file

@ -675,6 +675,9 @@ let currentBenchUserExplicitStart = null;
/** @type {number | null} */
let currentBenchUserExplicitEnd = null;
const registerTestIdRetBuf = new Uint32Array(1);
const registerTestIdRetBufU8 = new Uint8Array(registerTestIdRetBuf.buffer);
function testInner(
nameOrFnOrOptions,
optionsOrFn,
@ -774,8 +777,17 @@ function testInner(
testDesc.fn = wrapTest(testDesc);
testDesc.name = escapeName(testDesc.name);
const { id, origin } = ops.op_register_test(testDesc);
testDesc.id = id;
const origin = ops.op_register_test(
testDesc.fn,
testDesc.name,
testDesc.ignore,
testDesc.only,
testDesc.location.fileName,
testDesc.location.lineNumber,
testDesc.location.columnNumber,
registerTestIdRetBufU8,
);
testDesc.id = registerTestIdRetBuf[0];
testDesc.origin = origin;
MapPrototypeSet(testStates, testDesc.id, {
context: createTestContext(testDesc),
@ -1212,9 +1224,13 @@ function stepReportResult(desc, result, elapsed) {
for (const childDesc of state.children) {
stepReportResult(childDesc, { failed: "incomplete" }, 0);
}
ops.op_dispatch_test_event({
stepResult: [desc.id, result, elapsed],
});
if (result === "ok") {
ops.op_test_event_step_result_ok(desc.id, elapsed);
} else if (result === "ignored") {
ops.op_test_event_step_result_ignored(desc.id, elapsed);
} else {
ops.op_test_event_step_result_failed(desc.id, result.failed, elapsed);
}
}
/** @param desc {TestDescription | TestStepDescription} */
@ -1300,9 +1316,18 @@ function createTestContext(desc) {
stepDesc.name = escapeName(stepDesc.name);
stepDesc.rootName = escapeName(rootName);
stepDesc.fn = wrapTest(stepDesc);
const { id, origin } = ops.op_register_test_step(stepDesc);
const id = ops.op_register_test_step(
stepDesc.name,
stepDesc.location.fileName,
stepDesc.location.lineNumber,
stepDesc.location.columnNumber,
stepDesc.level,
stepDesc.parent.id,
stepDesc.rootId,
stepDesc.rootName,
);
stepDesc.id = id;
stepDesc.origin = origin;
stepDesc.origin = desc.origin;
const state = {
context: createTestContext(stepDesc),
children: [],
@ -1315,7 +1340,7 @@ function createTestContext(desc) {
stepDesc,
);
ops.op_dispatch_test_event({ stepWait: stepDesc.id });
ops.op_test_event_step_wait(stepDesc.id);
const earlier = DateNow();
const result = await stepDesc.fn(stepDesc);
const elapsed = DateNow() - earlier;