From 17adca388c986be0f5c919ca51c6599aeaeb68f8 Mon Sep 17 00:00:00 2001 From: Dirk de Visser Date: Sun, 8 Nov 2026 17:58:09 +0100 Subject: [PATCH 1/2] perf(runtime): elide unobserved RegExp result arrays - Retain captured callee authority while projecting unused or null-tested matches. - Preserve conversion, lastIndex and legacy effects in one runtime call with generic fallback. - Validate focused compiler and native checks, GC stress and disabled builds; seven matched fixed-work pairs improve 3.4%. --- docs/decisions/runtime-region-contracts.md | 5 + runtime/src/builtin_regexp.c | 37 ++++ runtime/src/builtin_regexp.h | 5 + runtime/src/perf_stats.c | 5 +- runtime/src/perf_stats.h | 1 + src/compiler/core/core-ir-provenance.ts | 2 +- src/compiler/core/core-ir-region-selection.ts | 15 +- src/compiler/core/core-ir-region-validity.ts | 2 +- src/compiler/core/core-ir-regions.ts | 9 +- .../target/compiler-artifact-codec.ts | 5 +- src/compiler/target/lower-core-target.ts | 2 +- src/compiler/target/program-image.ts | 3 +- src/compiler/target/render-native-c.ts | 28 ++- tests/local/regexp-discarded-result.js | 177 ++++++++++++++++++ tests/native/regexp-discarded-result.test.ts | 66 +++++++ tests/program-image-emission.test.ts | 48 +++++ tests/test-suite-native-sanitizer.txt | 1 + 17 files changed, 392 insertions(+), 19 deletions(-) create mode 100644 tests/local/regexp-discarded-result.js create mode 100644 tests/native/regexp-discarded-result.test.ts diff --git a/docs/decisions/runtime-region-contracts.md b/docs/decisions/runtime-region-contracts.md index 46144f976..1e1f8ed3d 100644 --- a/docs/decisions/runtime-region-contracts.md +++ b/docs/decisions/runtime-region-contracts.md @@ -13,6 +13,11 @@ operation. Neither a property name nor a receiver constructor establishes method identity. Realm checks also protect delayed materialization, whose prototype must belong to the original operation's realm. +RegExp exec projections retain the ordinary input conversion, lastIndex access +and legacy match state. When no capture is observed, the call is the sole region +anchor and matching can omit the result Array. A null comparison still observes +the same success or failure; escaping match objects retain the original call. + Exact callback contexts carry the callee, creation environment, canonical compiled entry, and stable function index. Runtime image growth can replace the function table, so contexts resolve rows through that index. Dynamically scoped contexts diff --git a/runtime/src/builtin_regexp.c b/runtime/src/builtin_regexp.c index 8362c7acf..477665371 100644 --- a/runtime/src/builtin_regexp.c +++ b/runtime/src/builtin_regexp.c @@ -3814,4 +3814,41 @@ int mal_regexp_try_exact_iterator_step( #endif // MAL_REGEXP +MalValue mal_regexp_exec_result_projection( + MalVm *vm, MalValue callee, MalValue regexp, MalValue string +) { + MalValue args[1] = {string}; +#if MAL_REGEXP + // The captured exec only observes the RegExp brand and lastIndex; unrelated + // prototype mutations do not change its authority or matching algorithm. + if (mal_value_is_native_function_object(callee) && + mal_native_function_object_callback(mal_value_to_native_function_object(callee)) == regexp_proto_exec && + mal_value_is_regexp_object(regexp) +#if MAL_REALMS + && mal_vm_callee_realm(vm, callee) == vm->current_realm +#endif + ) { + MalCalleeRoots roots; + mal_gc_callee_roots_begin( + &roots, regexp, mal_value_new_undefined(), callee, args, 1); + vm->gc_native_frames++; + MalValue result = mal_value_new_undefined(); + MalString *subject; + if (mal_vm_to_string(vm, args[0], &subject)) { + args[0] = mal_value_from_string(subject); + result = regexp_builtin_exec( + vm, mal_value_to_regexp_object(regexp), regexp, subject, + REGEXP_BUILTIN_EXEC_MATCH_ONLY, false, nullptr); + if (!regexp_threw(vm) && mal_value_is_boolean(result)) { + MAL_PERF_COUNT(regexp_exec_result_elisions); + } + } + vm->gc_native_frames--; + mal_gc_callee_roots_end(&roots); + return result; + } +#endif + return mal_vm_call_cached_value(vm, callee, regexp, args, 1); +} + #include "generated/known_native_builtin_regexp_c.inc" diff --git a/runtime/src/builtin_regexp.h b/runtime/src/builtin_regexp.h index 6462492cc..4a75ff4e2 100644 --- a/runtime/src/builtin_regexp.h +++ b/runtime/src/builtin_regexp.h @@ -62,6 +62,11 @@ bool mal_regexp_exec_capture_projection( MalValue *result_out ); +/** Preserve the captured call while projecting a result observed only as null. */ +MalValue mal_regexp_exec_result_projection( + MalVm *vm, MalValue callee, MalValue regexp, MalValue string +); + /** * Locked-world form for a fresh unaliased RegExp literal. Its property lookup, * callback identity, and Realm are compiler-proven; arbitrary input coercion is diff --git a/runtime/src/perf_stats.c b/runtime/src/perf_stats.c index 2328edd2f..ca72ad6e5 100644 --- a/runtime/src/perf_stats.c +++ b/runtime/src/perf_stats.c @@ -609,7 +609,7 @@ static void mal_perf_stats_print(void) { stderr, "[perf-regexp-stats] exec_calls=%llu fast_exec_calls=%llu ascii_exec_calls=%llu " "ascii_cache_hits=%llu ascii_cache_fills=%llu utf16_exec_calls=%llu " - "utf16_cache_hits=%llu utf16_cache_fills=%llu latin1_exec_calls=%llu\n", + "utf16_cache_hits=%llu utf16_cache_fills=%llu latin1_exec_calls=%llu result_elisions=%llu\n", (unsigned long long) mal_perf_stats.regexp_exec_calls, (unsigned long long) mal_perf_stats.regexp_fast_exec_calls, (unsigned long long) mal_perf_stats.regexp_ascii_exec_calls, @@ -618,7 +618,8 @@ static void mal_perf_stats_print(void) { (unsigned long long) mal_perf_stats.regexp_utf16_exec_calls, (unsigned long long) mal_perf_stats.regexp_utf16_cache_hits, (unsigned long long) mal_perf_stats.regexp_utf16_cache_fills, - (unsigned long long) mal_perf_stats.regexp_latin1_exec_calls + (unsigned long long) mal_perf_stats.regexp_latin1_exec_calls, + (unsigned long long) mal_perf_stats.regexp_exec_result_elisions ); fprintf( stderr, diff --git a/runtime/src/perf_stats.h b/runtime/src/perf_stats.h index a0c6c40dc..f27ebd9fb 100644 --- a/runtime/src/perf_stats.h +++ b/runtime/src/perf_stats.h @@ -209,6 +209,7 @@ typedef struct MalPerfStats { u64 regexp_utf16_cache_hits; u64 regexp_utf16_cache_fills; u64 regexp_latin1_exec_calls; + u64 regexp_exec_result_elisions; u64 intrinsic_ascii_calls; u64 intrinsic_ascii_bytes; diff --git a/src/compiler/core/core-ir-provenance.ts b/src/compiler/core/core-ir-provenance.ts index ec8b62666..030f41edb 100644 --- a/src/compiler/core/core-ir-provenance.ts +++ b/src/compiler/core/core-ir-provenance.ts @@ -4430,7 +4430,7 @@ function regexpExecProjectionCandidates( safe = false; break; } - if (!safe || loads.length === 0 || loads.length > 8) continue; + if (!safe || loads.length > 8) continue; for (const load of loads) { const capture = root(instructionResult(fn, load.instruction, 0)!); diff --git a/src/compiler/core/core-ir-region-selection.ts b/src/compiler/core/core-ir-region-selection.ts index 3a4e34e4f..17c369b74 100644 --- a/src/compiler/core/core-ir-region-selection.ts +++ b/src/compiler/core/core-ir-region-selection.ts @@ -571,12 +571,14 @@ function pendingLocalCandidate( closes === undefined ? instructions : instructions.filter((instruction) => !closes.includes(instruction)); + const fusedExecResult = + candidate.kind === "regexp-exec-projection" && candidate.loads.length === 0; const cost = costModel.forRegion(costed, { - genericTwins: 1, + genericTwins: fusedExecResult ? 0 : 1, ...(candidate.kind === "stack-object" || candidate.kind === "string-split-projection" || candidate.kind === "string-split-cursor" || - candidate.kind === "regexp-exec-projection" || + (candidate.kind === "regexp-exec-projection" && !fusedExecResult) || candidate.kind === "regexp-iterator-projection" || candidate.kind === "iterator-result-virtualization" || candidate.kind === "iterator-entry-pair-virtualization" @@ -631,7 +633,12 @@ function pendingLocalCandidate( candidate.consumers[0]!.instruction, ]) : candidate.kind === "regexp-exec-projection" - ? Object.freeze([candidate.call, candidate.loads[0]!.instruction]) + ? Object.freeze([ + candidate.call, + ...candidate.loads + .slice(0, 1) + .map((load) => load.instruction), + ]) : candidate.kind === "regexp-iterator-projection" ? Object.freeze([ candidate.step, @@ -1087,7 +1094,9 @@ function pendingLocalCandidate( ), generatedCodeCost: cost.estimatedCStatements, compilerWorkCost: cost.compileScore, + // Empty exec projections keep their fallback inside one runtime call. expansive: + !fusedExecResult && candidate.kind !== "builtin-collection-call-chain" && candidate.kind !== "string-char-code-at-chain" && candidate.kind !== "iterator-entry-pair-virtualization", diff --git a/src/compiler/core/core-ir-region-validity.ts b/src/compiler/core/core-ir-region-validity.ts index dea5a2c97..226ce77ab 100644 --- a/src/compiler/core/core-ir-region-validity.ts +++ b/src/compiler/core/core-ir-region-validity.ts @@ -1357,7 +1357,7 @@ function verifySpecialization( }); if ( candidate?.kind !== "regexp-exec-projection" || - selection.anchors.length !== 2 || + selection.anchors.length !== (candidate.loads.length === 0 ? 1 : 2) || selection.anchors[0] !== candidate.call || selection.anchors[1] !== candidate.loads[0]?.instruction || regexp.property !== candidate.property || diff --git a/src/compiler/core/core-ir-regions.ts b/src/compiler/core/core-ir-regions.ts index aff5f4581..f13da695e 100644 --- a/src/compiler/core/core-ir-regions.ts +++ b/src/compiler/core/core-ir-regions.ts @@ -833,10 +833,11 @@ export interface CoreAllocatedRegExpExecProjectionRegion extends CoreAllocatedRe "regexp-exec-projection", "regexp-capture-spans", "whole-region", - readonly [ - Extract, - Extract, - ] + | readonly [Extract] + | readonly [ + Extract, + Extract, + ] > { readonly property?: Extract; readonly propertyPlacement: CorePropertyPlacement; diff --git a/src/compiler/target/compiler-artifact-codec.ts b/src/compiler/target/compiler-artifact-codec.ts index 921ba1709..898543008 100644 --- a/src/compiler/target/compiler-artifact-codec.ts +++ b/src/compiler/target/compiler-artifact-codec.ts @@ -122,7 +122,7 @@ import type { /** Host-compiler cache format. This metadata never reaches the VM loader. */ export const COMPILER_ARTIFACT_MAGIC = 0x434c414d; // "MALC" little-endian // Internal artifacts are hard cut-overs: stale cache entries rebuild. -export const COMPILER_ARTIFACT_VERSION = 371; +export const COMPILER_ARTIFACT_VERSION = 372; /** Direct entries of C library bindings, as `binding-unit.ts` names them. */ const FOREIGN_CALL_SYMBOL = /^mal_ffi_direct\d+_\w+$/; @@ -4174,7 +4174,7 @@ function validateRegExpExecProjectionRegion( let valid = region.representation === "regexp-capture-spans" && region.lastIndexEffect === "retained-call-twin" && - region.anchors.length === 2 && + region.anchors.length === (region.loads.length === 0 ? 1 : 2) && region.anchors[0] === region.callIp && region.anchors[1] === region.loads[0]?.ip && (call?.opcode === "CALL" || call?.opcode === "CALL_KNOWN") && @@ -4213,7 +4213,6 @@ function validateRegExpExecProjectionRegion( (register) => Number.isInteger(register) && register >= 0 && register < fn.registerCount, ) && - region.loads.length > 0 && region.loads.length <= 8; for (const check of region.nullChecks) { const comparison = fn.instructions[check.comparisonIp]; diff --git a/src/compiler/target/lower-core-target.ts b/src/compiler/target/lower-core-target.ts index 0922f4003..13ddcf793 100644 --- a/src/compiler/target/lower-core-target.ts +++ b/src/compiler/target/lower-core-target.ts @@ -1330,7 +1330,7 @@ function lowerCoreSpecializations( }); regions.push({ ...envelope(row), - anchors: [call, loads[0]!.instruction], + anchors: loads.length === 0 ? [call] : [call, loads[0]!.instruction], kind: "regexp-exec-projection", license: { guard: regexp.guard, diff --git a/src/compiler/target/program-image.ts b/src/compiler/target/program-image.ts index e1bceefec..ffddb2258 100644 --- a/src/compiler/target/program-image.ts +++ b/src/compiler/target/program-image.ts @@ -4698,7 +4698,7 @@ function lowerExecutionFunctionToNativePlan( !guard.obligations.includes("fallback") || !guard.obligations.includes("materialize") || region.lastIndexEffect !== "retained-call-twin" || - resolvedAnchors.length !== 2 || + resolvedAnchors.length !== (loads.length === 0 ? 1 : 2) || region.resultRegisters.length === 0 ) { throw coreRegionError(region.kind, "projection metadata"); @@ -4928,7 +4928,6 @@ function lowerExecutionFunctionToNativePlan( (register) => !Number.isInteger(register) || register < 0 || register >= fn.registerCount, ) || - resolvedLoads.length === 0 || resolvedLoads.length > 8 || new Set(resolvedLoads.map((load) => load.captureIndex)).size !== resolvedLoads.length || diff --git a/src/compiler/target/render-native-c.ts b/src/compiler/target/render-native-c.ts index 6522d1e39..12c11511f 100644 --- a/src/compiler/target/render-native-c.ts +++ b/src/compiler/target/render-native-c.ts @@ -3889,7 +3889,7 @@ function emitCompiledVariant( 0, ), }); - nextStackSlot += loads.length + 1; + if (loads.length > 0) nextStackSlot += loads.length + 1; } const regexpIteratorProjectionSites = new Map< number, @@ -7039,8 +7039,10 @@ function analyzeNativeRegionTopology( (left, right) => left.captureIndex - right.captureIndex, ); if ( - loads.length === 0 || loads.length > 8 || + projection.anchors.length !== (loads.length === 0 ? 1 : 2) || + projection.anchors[0] !== projection.callIp || + projection.anchors[1] !== projection.loads[0]?.ip || seenExecCalls.has(projection.callIp) || loads.some( (load, index) => @@ -7938,9 +7940,12 @@ function emitBody( ); } for (const site of regexpExecProjectionSites.values()) { + if (site.loads.length === 0) continue; lines.push( `bool __regexp_exec_${site.projection.callIp}_fast = false;`, `bool __regexp_exec_${site.projection.callIp}_projected = false;`, + ); + lines.push( `i32 __regexp_exec_${site.projection.callIp}_starts[${site.loads.length}];`, `i32 __regexp_exec_${site.projection.callIp}_ends[${site.loads.length}];`, ); @@ -12672,6 +12677,14 @@ class NativeInstructionEmission { if (this.context.nativeRegExpExecProjectionAction?.role === "call") { const { site } = this.context.nativeRegExpExecProjectionAction; const projection = site.projection; + if (site.loads.length === 0) { + const callee = `mal_vm_load_primordial(vm, ${knownOperations()[knownOperationIndex(instruction.operation)!]!.node})`; + return [ + `r${instruction.dst} = ${this.profileCall("regexp", `mal_regexp_exec_result_projection(vm, ${callee}, ${this.boxedOperand(instruction.thisValue)}, ${this.boxedOperand(instruction.arguments[0]!)})`)};`, + this.throwCheck(), + this.callPoll(), + ]; + } const indices = site.loads.map((load) => load.captureIndex).join(", "); const outputs = site.loads .map((_load, index) => `&__gc_slots[${site.slotsOffset + index}]`) @@ -13335,6 +13348,17 @@ class NativeInstructionEmission { if (this.context.nativeRegExpExecProjectionAction?.role === "call") { const { site, propertyLoad } = this.context.nativeRegExpExecProjectionAction; const projection = site.projection; + if (site.loads.length === 0) { + const callee = + propertyLoad === undefined + ? this.boxedOperand(instruction.callee) + : `mal_vm_load_primordial(vm, ${knownOperations()[knownOperationIndex("RegExp.prototype.exec")!]!.node})`; + return [ + `r${instruction.dst} = ${this.profileCall("regexp", `mal_regexp_exec_result_projection(vm, ${callee}, ${this.boxedOperand(instruction.thisValue)}, ${this.boxedOperand(instruction.arguments[0]!)})`)};`, + this.throwCheck(), + this.callPoll(), + ]; + } const fast = `__regexp_exec_${projection.callIp}_fast`; const indices = site.loads.map((load) => load.captureIndex).join(", "); const outputs = site.loads diff --git a/tests/local/regexp-discarded-result.js b/tests/local/regexp-discarded-result.js new file mode 100644 index 000000000..2322f2577 --- /dev/null +++ b/tests/local/regexp-discarded-result.js @@ -0,0 +1,177 @@ +const observations = []; + +function discard(regexp, source) { + regexp.exec(source); +} + +function discardLiteral(source) { + /(a)(b)?/.exec(source); +} + +function discardEvaluated(regexp, source) { + regexp.exec(source()); +} + +function matched(regexp, source) { + return regexp.exec(source) !== null; +} + +const discardCallback = new Proxy(discard, {}); +const literalCallback = new Proxy(discardLiteral, {}); + +function thrown(callback) { + try { + callback(); + return "none"; + } catch (error) { + return error.name; + } +} + +discardLiteral("prefix-ab-suffix"); +observations.push([RegExp.input, RegExp.lastMatch, RegExp.$1, RegExp.$2]); +discard(/missing/, "no match"); +observations.push([RegExp.lastMatch, RegExp.$1, RegExp.$2]); + +class DerivedRegExp extends RegExp {} +const derived = new DerivedRegExp("a", "g"); +Reflect.apply(discardCallback, undefined, [derived, "a"]); +observations.push([derived.lastIndex, derived instanceof DerivedRegExp]); + +const altered = /a/g; +Object.defineProperty(altered, "exec", { value: RegExp.prototype.exec }); +Object.setPrototypeOf(altered, null); +Reflect.apply(discardCallback, undefined, [altered, "a"]); +observations.push([altered.lastIndex, Object.getPrototypeOf(altered) === null]); + +let invalidCoercions = 0; +const invalid = { exec: RegExp.prototype.exec }; +observations.push([ + thrown(() => + Reflect.apply(discardCallback, undefined, [ + invalid, + { + toString() { + invalidCoercions++; + return "a"; + }, + }, + ]), + ), + invalidCoercions, +]); + +for (const flags of ["g", "y"]) { + const regexp = new RegExp("a", flags); + const indices = []; + for (let index = 0; index < 3; index++) { + Reflect.apply(discardCallback, undefined, [regexp, "aba"]); + indices.push(regexp.lastIndex); + } + observations.push([flags, indices]); +} + +let lastIndexCoercions = 0; +const nonglobal = /a/; +const indexObject = { + valueOf() { + lastIndexCoercions++; + return 2; + }, +}; +nonglobal.lastIndex = indexObject; +discard(nonglobal, "a"); +observations.push([lastIndexCoercions, nonglobal.lastIndex === indexObject]); + +const order = []; +const coerced = /a/g; +coerced.lastIndex = { + valueOf() { + order.push("lastIndex"); + return 0; + }, +}; +discard(coerced, { + toString() { + order.push("input"); + return "a"; + }, +}); +observations.push([order, coerced.lastIndex]); + +let literalCoercions = 0; +discardLiteral({ + toString() { + literalCoercions++; + return "ab"; + }, +}); +observations.push([literalCoercions, RegExp.lastMatch]); +observations.push( + thrown(() => + Reflect.apply(literalCallback, undefined, [ + { + toString() { + throw new RangeError(); + }, + }, + ]), + ), +); +const throwingIndex = /a/; +throwingIndex.lastIndex = { + valueOf() { + throw new SyntaxError(); + }, +}; +observations.push( + thrown(() => Reflect.apply(discardCallback, undefined, [throwingIndex, "a"])), +); + +for (const source of ["a", "missing"]) { + const readonly = /a/g; + Object.defineProperty(readonly, "lastIndex", { writable: false }); + observations.push( + thrown(() => Reflect.apply(discardCallback, undefined, [readonly, source])), + ); +} + +let getterCalls = 0; +let customCalls = 0; +const custom = { + get exec() { + getterCalls++; + return function (source) { + customCalls++; + observations.push([this === custom, source]); + return undefined; + }; + }, +}; +discard(custom, "custom input"); +observations.push([getterCalls, customCalls]); + +const capturedOrder = []; +const captured = /a/; +Object.defineProperty(captured, "exec", { + configurable: true, + get() { + capturedOrder.push("get"); + return RegExp.prototype.exec; + }, +}); +discardEvaluated(captured, () => { + capturedOrder.push("argument"); + Object.defineProperty(captured, "exec", { + value() { + capturedOrder.push("replacement"); + return null; + }, + }); + return "a"; +}); +observations.push([capturedOrder, RegExp.lastMatch]); +observations.push([matched(/a/, "a"), matched(/a/, "b")]); +const escaped = /(a)/.exec("a"); +observations.push([Array.isArray(escaped), escaped[0], escaped[1], escaped.index]); +console.log(JSON.stringify(observations)); diff --git a/tests/native/regexp-discarded-result.test.ts b/tests/native/regexp-discarded-result.test.ts new file mode 100644 index 000000000..2065ac9b0 --- /dev/null +++ b/tests/native/regexp-discarded-result.test.ts @@ -0,0 +1,66 @@ +import { execFileSync, spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterAll, describe, expect, it } from "vitest"; +import { resolveBuildConfig } from "../../src/build-config.ts"; +import { + buildBackendPairFromOneProgramImage, + HOST_MAIN, + runToStdout, + STRESS_ENV, +} from "../../src/test-harness.ts"; + +const fixture = "tests/local/regexp-discarded-result.js"; +const outDir = mkdtempSync(path.join(os.tmpdir(), "mal-regexp-discarded-")); +afterAll(() => rmSync(outDir, { recursive: true, force: true })); + +describe("discarded regexp exec results", () => { + it.each(["mutable", "locked"] as const)( + "preserves exec effects and captured callees under %s primordials", + (primordials) => { + const expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + const pair = buildBackendPairFromOneProgramImage({ + fixture, + name: `regexp-discarded-${primordials}`, + outDir, + mainFile: HOST_MAIN, + config: resolveBuildConfig({ + engine: { primordials, eval: false, realms: false }, + surface: { node: true }, + }), + environment: { ...process.env, MAL_PERF_STATS: "1" }, + }); + for (const name of ["discard", "discardLiteral"]) { + const callback = pair.programImage.native.functions.find( + (fn) => + String.fromCharCode( + ...(pair.programImage.runtime.stringConstants[fn.body.nameStringIndex] ?? + []), + ) === name, + ); + expect( + callback?.specializations.some( + (region) => + region.kind === "regexp-exec-projection" && region.loads.length === 0, + ), + ).toBe(true); + } + for (const binary of [pair.compiled, pair.interpreted]) { + for (const stress of [{}, STRESS_ENV]) { + expect(runToStdout(binary, { env: stress, timeoutMs: 60_000 })).toBe(expected); + } + } + const measured = spawnSync(pair.compiled, [], { + encoding: "utf8", + timeout: 60_000, + env: { ...process.env, MAL_PERF_STATS: "1" }, + }); + expect(measured.status, measured.stderr).toBe(0); + expect(measured.stdout).toBe(expected); + const elisions = measured.stderr.match(/result_elisions=(\d+)/); + expect(elisions).not.toBeNull(); + expect(Number(elisions![1])).toBeGreaterThan(0); + }, + ); +}); diff --git a/tests/program-image-emission.test.ts b/tests/program-image-emission.test.ts index 2bf238cf2..bb71074f7 100644 --- a/tests/program-image-emission.test.ts +++ b/tests/program-image-emission.test.ts @@ -4586,6 +4586,54 @@ describe("native update-expression representation", () => { ); }); + it.each(["mutable", "locked"] as const)( + "retains RegExp.exec calls without observed captures under %s primordials", + (primordials) => { + const source = ` + globalThis.discard = function(regexp, value) { regexp.exec(value); }; + globalThis.literal = function(value) { /(a)/.exec(value); }; + globalThis.matched = function(regexp, value) { return regexp.exec(value) !== null; }; + globalThis.escape = function(regexp, value) { return regexp.exec(value); }; + `; + const semantic = analyzeSourceAndRunSemanticAnalysis( + source, + "regexp-discarded-result.js", + parseScript(source, { strict: false }), + ); + const lowered = compileSemanticProgramToProgramImage(semantic, { + facts: compilerProgramFactsFromConfig( + resolveBuildConfig({ engine: { primordials } }), + ), + }); + const projections = specializations(lowered).filter( + (region) => region.kind === "regexp-exec-projection", + ); + expect(projections).toHaveLength(3); + for (const projection of projections) { + expect(projection.loads).toEqual([]); + expect(projection.anchors).toEqual([projection.callIp]); + } + const cached = deserializeCompilerArtifact(serializeCompilerArtifact(lowered)); + expect( + specializations(cached).filter( + (region) => region.kind === "regexp-exec-projection", + ), + ).toEqual(projections); + for (const [index, fn] of cached.native.functions.entries()) { + if ( + fn.specializations.some((region) => region.kind === "regexp-exec-projection") + ) { + expect(emitCompiledFunction(fn, index, "", false)?.source).toContain( + "mal_regexp_exec_result_projection(vm,", + ); + } + } + const emitted = emitProgramImage(cached, { compiled: true }); + expect(emitted).toContain("mal_regexp_exec_result_projection(vm,"); + expect(emitted).not.toMatch(/__regexp_exec_\d+_(?:starts|ends)\[0\]/); + }, + ); + it("carries Core-selected RegExp.exec projections through lowering and wire", () => { const source = `globalThis.parse = function parse(regexp, value) { const match = regexp.exec(value); diff --git a/tests/test-suite-native-sanitizer.txt b/tests/test-suite-native-sanitizer.txt index dd91de755..533b85c7e 100644 --- a/tests/test-suite-native-sanitizer.txt +++ b/tests/test-suite-native-sanitizer.txt @@ -261,6 +261,7 @@ tests/native/static-property-membership.test.ts tests/native/indexed-operand-facts.test.ts tests/native/native-fast-entry.test.ts tests/native/regexp-span-continuation.test.ts +tests/native/regexp-discarded-result.test.ts tests/native/map-region-entries.test.ts tests/native/array-initialization-region.test.ts tests/native/array-initialization-incremental.test.ts From b18c63d6a70ff5aabf18044d6f5a78763279e406 Mon Sep 17 00:00:00 2001 From: Dirk de Visser Date: Sun, 8 Nov 2026 18:34:28 +0100 Subject: [PATCH 2/2] perf(runtime): reserve ordinary constructor field capacity - Preserve property publication and setters while avoiding extra slot allocations. - Validate constructor eligibility across Core and wire metadata; advance cache identities. - Cover constructor parity and GC boundaries; measure fixed EarleyBoyer and V8 holdouts. --- docs/decisions/runtime-region-contracts.md | 5 + runtime/src/vm_load.c | 5 +- src/compiler/core/core-constructor-layout.ts | 8 +- .../target/compiler-artifact-codec.ts | 2 +- src/compiler/target/program-image-codec.ts | 2 +- src/compiler/target/runtime-image.ts | 5 +- tests/local/ordinary-constructor-capacity.js | 101 ++++++++++++++++++ .../ordinary-constructor-capacity.test.ts | 55 ++++++++++ tests/program-image-codec.test.ts | 18 +++- tests/program-image-emission.test.ts | 30 ++++++ tests/test-suite-native-sanitizer.txt | 1 + 11 files changed, 225 insertions(+), 7 deletions(-) create mode 100644 tests/local/ordinary-constructor-capacity.js create mode 100644 tests/native/ordinary-constructor-capacity.test.ts diff --git a/docs/decisions/runtime-region-contracts.md b/docs/decisions/runtime-region-contracts.md index 1e1f8ed3d..c5577fc06 100644 --- a/docs/decisions/runtime-region-contracts.md +++ b/docs/decisions/runtime-region-contracts.md @@ -18,6 +18,11 @@ and legacy match state. When no capture is observed, the call is the sole region anchor and matching can omit the result Array. A null comparison still observes the same success or failure; escaping match objects retain the original call. +Constructor capacity hints reserve storage while property presence follows the +original stores. Ordinary constructors retain inherited setters, callbacks and +returned-object behavior; publishing a complete initial shape requires a separate +initialization proof. + Exact callback contexts carry the callee, creation environment, canonical compiled entry, and stable function index. Runtime image growth can replace the function table, so contexts resolve rows through that index. Dynamically scoped contexts diff --git a/runtime/src/vm_load.c b/runtime/src/vm_load.c index 58cce6bd3..e18e1048e 100644 --- a/runtime/src/vm_load.c +++ b/runtime/src/vm_load.c @@ -17,7 +17,7 @@ */ #define WIRE_MAGIC 0x574c414du // "MALW" little-endian -#define WIRE_VERSION 85u +#define WIRE_VERSION 86u #define WIRE_FLAG_HAS_DEBUG 1u typedef enum WireOp { @@ -1867,7 +1867,8 @@ static void rd_function(MalLoadedRuntimeImage *L, Rd *r, MalFunction *fn, bool d fn->captured_count < 0 || fn->mapped_argument_count > fn->parameter_count || fn->constructor_slot_reserve > MAL_SHAPE_MAX_INLINE_SLOTS || (fn->constructor_slot_reserve != 0 && - (!fn->is_class_constructor || fn->is_derived_constructor)) || + (fn->is_derived_constructor || fn->kind != MAL_FUNCTION_KIND_NORMAL || + (!fn->is_class_constructor && !fn->has_prototype))) || (!fn->mapped_arguments && fn->mapped_argument_count != 0) || (fn->mapped_arguments && fn->strict)) { r->ok = false; diff --git a/src/compiler/core/core-constructor-layout.ts b/src/compiler/core/core-constructor-layout.ts index c2239b13f..682915b82 100644 --- a/src/compiler/core/core-constructor-layout.ts +++ b/src/compiler/core/core-constructor-layout.ts @@ -23,7 +23,13 @@ function rootDefinition( /** Capacity only: these properties are not considered present until their stores execute. */ export function coreConstructorSlotReserve(fn: CoreFunctionStore): number { - if (!fn.metadata.isClassConstructor || fn.metadata.isDerivedConstructor) return 0; + if ( + fn.metadata.isDerivedConstructor || + fn.isGenerator || + fn.isAsync || + (!fn.metadata.isClassConstructor && !fn.metadata.hasPrototype) + ) + return 0; const keys = new Set(); // Private elements share the instance's shape slots; each install names a distinct element. let privateElements = 0; diff --git a/src/compiler/target/compiler-artifact-codec.ts b/src/compiler/target/compiler-artifact-codec.ts index 898543008..5a8d509cd 100644 --- a/src/compiler/target/compiler-artifact-codec.ts +++ b/src/compiler/target/compiler-artifact-codec.ts @@ -122,7 +122,7 @@ import type { /** Host-compiler cache format. This metadata never reaches the VM loader. */ export const COMPILER_ARTIFACT_MAGIC = 0x434c414d; // "MALC" little-endian // Internal artifacts are hard cut-overs: stale cache entries rebuild. -export const COMPILER_ARTIFACT_VERSION = 372; +export const COMPILER_ARTIFACT_VERSION = 373; /** Direct entries of C library bindings, as `binding-unit.ts` names them. */ const FOREIGN_CALL_SYMBOL = /^mal_ffi_direct\d+_\w+$/; diff --git a/src/compiler/target/program-image-codec.ts b/src/compiler/target/program-image-codec.ts index babe31278..88f2da040 100644 --- a/src/compiler/target/program-image-codec.ts +++ b/src/compiler/target/program-image-codec.ts @@ -44,7 +44,7 @@ import type { export const WIRE_MAGIC = 0x574c414d; // "MALW" little-endian // Runtime wires are hard cut-overs: stale cached buffers must rebuild. -export const WIRE_VERSION = 85; +export const WIRE_VERSION = 86; /** Canonical wire tags generated into the C runtime from the same registry. */ export const WIRE_OPCODES = BYTECODE_OPERATIONS; diff --git a/src/compiler/target/runtime-image.ts b/src/compiler/target/runtime-image.ts index 9a8fd36e9..a88d05bdf 100644 --- a/src/compiler/target/runtime-image.ts +++ b/src/compiler/target/runtime-image.ts @@ -2174,7 +2174,10 @@ export function validateRuntimeImageMetadata(definition: RuntimeImage): void { !isNonnegativeSafeInteger(fn.constructorSlotReserve) || fn.constructorSlotReserve > CORE_MAX_SHAPED_OBJECT_SLOTS || (fn.constructorSlotReserve !== 0 && - (!fn.isClassConstructor || fn.isDerivedConstructor)) + (fn.isDerivedConstructor || + fn.isGenerator || + fn.isAsync || + (!fn.isClassConstructor && !fn.hasPrototype))) ) throw new RangeError("invalid constructor slot reserve"); for (const instruction of fn.instructions) { diff --git a/tests/local/ordinary-constructor-capacity.js b/tests/local/ordinary-constructor-capacity.js new file mode 100644 index 000000000..07c3b6560 --- /dev/null +++ b/tests/local/ordinary-constructor-capacity.js @@ -0,0 +1,101 @@ +const observations = []; +const collect = typeof gc === "function" ? gc : () => {}; + +function Pair(left, right) { + this.left = left; + this.right = right; +} + +const PairConstructor = new Proxy(Pair, {}); +const kept = []; +for (let index = 0; index < 64; index++) { + kept.push(new PairConstructor(index, kept.at(-1))); +} +collect(); +observations.push([kept.at(-1).left, kept.at(-1).right === kept.at(-2)]); + +const grown = kept[0]; +grown.extra = "third"; +grown.tail = "fourth"; +delete grown.left; +Object.defineProperty(grown, "right", { get: () => "accessor" }); +collect(); +observations.push([Object.keys(grown), grown.extra, grown.tail, grown.right]); + +function Conditional(flag) { + if (flag) this.first = "first"; + this.second = "second"; +} +observations.push([ + Object.keys(new Conditional(false)), + Object.keys(new Conditional(true)), +]); + +function SetterPair(left, right) { + this.left = left; + this.right = right; +} +const setterObservations = []; +Object.defineProperty(SetterPair.prototype, "right", { + set(value) { + collect(); + setterObservations.push([value, Object.keys(this), Object.hasOwn(this, "right")]); + }, +}); +const setterPair = new SetterPair(3, 4); +observations.push([setterObservations, setterPair.left, Object.keys(setterPair)]); + +function Observed(observer) { + observer(this); + this.left = "left"; + this.right = "right"; +} +const observed = new Observed((receiver) => { + observations.push(Object.keys(receiver)); + collect(); +}); +observations.push(Object.keys(observed)); + +function Constrained(block) { + "use strict"; + block(this); + this.left = 1; + this.right = 2; +} +let blockedReceiver; +let blocked = false; +try { + new new Proxy(Constrained, {})((receiver) => { + blockedReceiver = receiver; + Object.preventExtensions(receiver); + collect(); + }); +} catch (error) { + blocked = error instanceof TypeError; +} +observations.push([ + blocked, + Object.keys(blockedReceiver), + Object.isExtensible(blockedReceiver), +]); + +function Returning() { + this.temporary = "temporary"; + return { replacement: true }; +} +observations.push(Object.keys(new Returning())); + +const target = { existing: true }; +Pair.call(target, 7, 8); +observations.push([Object.keys(target), target.left, target.right]); + +function Other() {} +Other.prototype.tag = "other"; +const alternate = Reflect.construct(Pair, [9, 10], Other); +observations.push([ + Object.getPrototypeOf(alternate) === Other.prototype, + alternate.tag, + alternate.left, + alternate.right, +]); +console.log(JSON.stringify(observations)); diff --git a/tests/native/ordinary-constructor-capacity.test.ts b/tests/native/ordinary-constructor-capacity.test.ts new file mode 100644 index 000000000..71b3ba7f8 --- /dev/null +++ b/tests/native/ordinary-constructor-capacity.test.ts @@ -0,0 +1,55 @@ +import { execFileSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterAll, describe, expect, it } from "vitest"; +import { resolveBuildConfig } from "../../src/build-config.ts"; +import { + buildBackendPairFromOneProgramImage, + HOST_MAIN, + runToStdout, + STRESS_ENV, +} from "../../src/test-harness.ts"; + +const fixture = "tests/local/ordinary-constructor-capacity.js"; +const outDir = mkdtempSync(path.join(os.tmpdir(), "mal-ordinary-constructor-")); +afterAll(() => rmSync(outDir, { recursive: true, force: true })); + +describe("ordinary constructor storage capacity", () => { + it.each(["mutable", "locked"] as const)( + "preserves property publication and constructor calls under %s primordials", + (primordials) => { + const expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + const pair = buildBackendPairFromOneProgramImage({ + fixture, + name: `ordinary-constructor-${primordials}`, + outDir, + mainFile: HOST_MAIN, + config: resolveBuildConfig({ + engine: { primordials, eval: false, realms: false }, + surface: { node: true }, + }), + }); + for (const name of ["Pair", "Constrained"]) { + const constructor = pair.programImage.runtime.functions.find( + (fn) => + String.fromCharCode( + ...(pair.programImage.runtime.stringConstants[fn.nameStringIndex] ?? []), + ) === name, + ); + expect(constructor?.isClassConstructor).toBe(false); + expect(constructor?.constructorSlotReserve).toBe(2); + } + for (const binary of [pair.compiled, pair.interpreted]) { + for (const stress of [{}, STRESS_ENV]) { + expect( + runToStdout(binary, { + env: { MAL_HOST_GC: "1", ...stress }, + timeoutMs: 60_000, + }), + ).toBe(expected); + } + } + }, + ); +}); diff --git a/tests/program-image-codec.test.ts b/tests/program-image-codec.test.ts index 94820e800..c87a603e0 100644 --- a/tests/program-image-codec.test.ts +++ b/tests/program-image-codec.test.ts @@ -1481,7 +1481,11 @@ describe("program-image-codec", () => { }); it("round-trips and validates constructor slot reserves", () => { - const withReserve = (constructorSlotReserve: number, isDerivedConstructor = false) => + const withReserve = ( + constructorSlotReserve: number, + isDerivedConstructor = false, + flags: Partial = {}, + ) => withRuntime(definition, { functions: definition.runtime.functions.map((fn, index) => index === 0 @@ -1490,6 +1494,7 @@ describe("program-image-codec", () => { isClassConstructor: true, isDerivedConstructor, constructorSlotReserve, + ...flags, } : fn, ), @@ -1499,9 +1504,20 @@ describe("program-image-codec", () => { deserializeCompilerArtifact(serializeCompilerArtifact(valid)).runtime.functions[0]! .constructorSlotReserve, ).toBe(CORE_MAX_SHAPED_OBJECT_SLOTS); + const ordinary = withReserve(2, false, { + isClassConstructor: false, + hasPrototype: true, + }); + expect( + deserializeCompilerArtifact(serializeCompilerArtifact(ordinary)).runtime + .functions[0]!.constructorSlotReserve, + ).toBe(2); for (const invalid of [ withReserve(CORE_MAX_SHAPED_OBJECT_SLOTS + 1), withReserve(1, true), + withReserve(1, false, { isClassConstructor: false, hasPrototype: false }), + withReserve(1, false, { isGenerator: true }), + withReserve(1, false, { isAsync: true }), ]) { expect(() => serializeCompilerArtifact(invalid)).toThrow( /invalid constructor slot reserve/, diff --git a/tests/program-image-emission.test.ts b/tests/program-image-emission.test.ts index bb71074f7..5a219a0f8 100644 --- a/tests/program-image-emission.test.ts +++ b/tests/program-image-emission.test.ts @@ -485,6 +485,36 @@ describe("emit-program-image instruction packing", () => { ).toBe(0); }); + it.each([ + ["function", 2], + ["function*", 0], + ["async function", 0], + ] as const)("reserves ordinary constructor capacity for %s", (kind, reserve) => { + const source = `${kind} Pair(left, right) { + this.left = left; + this.right = right; + this.left = left; + } + globalThis.Pair = Pair;`; + const semantic = analyzeSourceAndRunSemanticAnalysis( + source, + "ordinary-constructor-capacity.js", + parseScript(source, { strict: false }), + ); + const image = compileSemanticProgramToProgramImage(semantic); + const restored = deserializeCompilerArtifact(serializeCompilerArtifact(image)); + for (const current of [image, restored]) { + const constructor = current.runtime.functions.find( + (fn) => + String.fromCharCode( + ...(current.runtime.stringConstants[fn.nameStringIndex] ?? []), + ) === "Pair", + ); + expect(constructor).toBeDefined(); + expect(constructor!.constructorSlotReserve).toBe(reserve); + } + }); + it("combines semantic dependencies with one retained region twin", () => { const license = vmRegionLicense( [ diff --git a/tests/test-suite-native-sanitizer.txt b/tests/test-suite-native-sanitizer.txt index 533b85c7e..3c894cb6e 100644 --- a/tests/test-suite-native-sanitizer.txt +++ b/tests/test-suite-native-sanitizer.txt @@ -147,6 +147,7 @@ tests/native/json-parse-shapes.test.ts tests/native/json-stringify-scratch.test.ts tests/native/literal-template.test.ts tests/native/literal-materialization.test.ts +tests/native/ordinary-constructor-capacity.test.ts tests/native/static-array-copies.test.ts tests/native/symbol-registry-reuse.test.ts tests/native/node-buffer.test.ts