Semantic Diff

#9469 Emit source map mappings for pattern-match arms, branch tests, and block boundaries

4 commits

6b9bc077dc2592dd6b21bc9239bdd70465244595..pr-9469

bc67a4567 · mununki · Nov 28, 2026, 19:50 UTC

Emit source map mappings for pattern-match arms, branch tests, and block boundaries

Why

Pattern-match arms and if branches returning a constant or variable (| Empty => "empty", | None => -1, | _ => x) emitted no source map mapping, because Lambda.t carried Location.t only on Lprim, Lapply, and Lfunction. Branch dispatch tests (typeof s !== "object", o === undefined, switch (s.TAG)) and shared destructuring (Primitive_option.valFromOption(o)) either had no mapping or borrowed a sibling arm's location, and block/function closing braces had no end mappings, causing V8 coverage tools (c8 / v8-to-istanbul) to drop unexecuted ranges and report unexecuted arms as covered (#9387).

modifiedSourceMap pattern-match arms and dispatch tests

let opt = o =>  switch o {  | Some(Some(x)) => x + 1  | Some(None) => 0  | None => -1  }
text
Before
After
function opt(o) { @0 -> 1:10 o =>function opt(o) { @0 -> 1:10 o => if (o === undefined) { @6 -> 3:4 Some(Some(x)) => x + 1 return -1; (none) } (none) let x = Primitive_option.valFromOption(o); @10 -> 4:4 Some(None) => 0 if (x !== undefined) { @6 -> 3:9 Some(x)) => x + 1 return x + 1 | 0; @11 -> 3:21 x + 1 } else { (none) return 0; (none) } (none)} (none) if (o === undefined) { @2 -> 2:2 switch o { return -1; @4 -> 5:13 -1 } @2 -> 5:14 (end) let x = Primitive_option.valFromOption(o); @2 -> 2:2 switch o { if (x !== undefined) { @2 -> 2:2 switch o { return x + 1 | 0; @4 -> 3:21 x + 1 } else { @2 -> 3:26 (end) @3 -> 4:18 0 return 0; @4 -> 4:18 0 } @2 -> 4:19 (end)} @0 -> 6:3 (end)

Fixes #9387

Review

  • Does attaching locations to Lambda branch nodes change optimizations or generated JS? No. Leaf nodes (Lconst, Lvar) are untouched, and Lambda_traverse.make_key and Lambda.eq_approx ignore all branch and node locations. Generated JS is byte-identical with source maps enabled or disabled (apart from the sourceMappingURL comment).
  • How are non-1:1 arm optimizations (merged, split, or reordered branches) handled? Shared destructuring (relocate_cell_args) and dispatch tests map to the enclosing switch/if rather than a sibling arm. Fallthrough case labels keep each arm's pat_loc, inlined static exits (Lam_pass_exits) propagate branch_loc to every inlined site, and same-switch nested dispatch blocks (if (typeof s !== "object")) map { and } to switch start so interleaved executed arms are never marked uncovered.
2abd577e8 · mununki · Nov 29, 2026, 05:24 UTC

Avoid branch-location overhead in .cmj exports and non-sourcemap builds

Why

1. Lambda.no_if_locs was exported in lambda.mli without external callers, triggering reanalyze's dead-code check; keep it internal and share both no_if_locs and no_branch_loc when locations are absent. 2. Marshaling branch, switch, and handler locations inside persistent_closed_lambda inflated runtime .cmj files (+1.42%); strip branch locations before exporting Lambda terms to .cmj. 3. Printing chained else-if trees scanned remaining else branches quadratically when nested If statements carried no source_loc, and divide_variant / Lam_compile repeated location work when source maps were disabled (+1.34% instructions on PolyVariantSwitch.res); guard source-map boundary lookups behind active source-map state and share unchanged pattern-match cells.

74f49b01e · mununki · Nov 29, 2026, 06:00 UTC

Handle dispatch prologues, preserve Seq source_loc, and cut pass allocations

Why

1. Non-tail pattern matches with shared destructuring prologues (such as let match = l.tl; let x = l.hd; if (match !== 0) ...) were rejected by the singleton-only is_same_dispatch_block check, mapping the dispatch block's opening brace to the enclosing switch and its closing brace to a descendant arm end so executing only the empty-list arm marked that executed arm uncovered in V8 coverage. Step through same-switch variable prologues and nested blocks in is_same_dispatch_block. 2. Js_exp_make.econd, remove_pure_sub_exp, Js_stmt_make.exp, and Js_analyzer.rev_flatten_seq dropped source_loc when unwrapping Seq (_, Undefined) wrappers around unit/ref-assignment arms. Preserve the outer source_loc when the unwrapped child has none. 3. Lam_pass_exits.simplif_with_loc allocated a (lam, loc) pair on every visited node, and Lswitch/Lstringswitch traversals across backend/typing passes allocated closures per switch, causing >1% allocation regressions on Result, Primitive_object, and no-op/leaf-edit benchmarks. Track inlined handler locations via a single ref and eliminate per-node/per-switch closure allocations.

c7e1138f9 · mununki · Nov 29, 2026, 07:14 UTC

Hoist source-map helpers out of Lam_compile.compile

Why

Lam_compile_main.compile calls Lam_compile.compile_lambda ~output_prefix once per top-level Lam_group.t, which re-enters Lam_compile.compile. Keeping the nine source-map helper functions nested inside compile capturing has_source_map allocated fresh closures for every top-level group, regressing allocations by +1.32% on build/Namespace/edit (1,000 top-level namespace alias groups) and +0.71% on stress/InlineChain.res.

Hoist the source-map helpers to module top-level so Lam_compile.compile allocates no per-group closures, checking Js_source_map.enabled () inside each helper and caching it locally in compile_ifthenelse.