perf: simplify _validateMapping — hot path first, drop redundant 'in' checks#66
Merged
Conversation
… checks
The previous form had:
aGenerated && 'line' in aGenerated && 'column' in aGenerated
&& aGenerated.line > 0 && aGenerated.column >= 0
The numeric `> 0` / `>= 0` checks already subsume the `'line' in X`
membership tests — `undefined > 0` and `'abc' > 0` are both false, so
missing or non-numeric coords still fall to the error branches.
The `in` operator does a prototype-chain walk per check; dropping the
four redundant ones removes a measurable per-addMapping cost.
Also reordered: case 2/3 (full mapping) is the hot path on every
bundler emit; check it first and return. The friendly-error path
(original.line/column not numbers) and the generic invalid-mapping
path stay reachable for the cold cases.
Bench (SOLO=1 PHASES=adding generate vs main, two-process):
preact +10.5%, react +10.2%, amp +7.3%, vscode +6.4%,
issue-41 +5.2%, babel.min +3.9% — mean ~+7.3%
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_validateMappingruns once peraddMappingcall (whenskipValidationis false, the default). Two cleanups, both behavior-preserving:Drop redundant
'X' in Ymembership tests. The previous form had:The numeric
> 0/>= 0checks already subsume the'in'tests —undefined > 0and'abc' > 0are bothfalse, so missing or non-numeric coords still fall to the error branches. Theinoperator walks the prototype chain on every check; the four redundant ones cost real time per addMapping.Reorder: hot path first. Case 2/3 (full mapping with original + source) is what every bundler emit hits. Check it first and return. The friendly-error branch (original.line/column not numbers) and the generic invalid-mapping branch stay reachable for the cold cases — error messages and codepaths are unchanged.
Bench
SOLO=1 PHASES=adding scripts/bench-diff.sh main generate(two-process, ops/sec via benchmark.js):Mean ~+7.3%. babel.min sees +3.9% here vs +0.2% on #65 — the prototype-walk savings hit every mapping unconditionally, so the two PRs stack rather than overlap.
Test plan
/Invalid mapping/,/original\.line and original\.column are not numbers/)source-map-generator.jsstays 100/100/100