perf(package): strip comments from the Apple runner source the npm package ships - #2467
perf(package): strip comments from the Apple runner source the npm package ships#2467thymikee wants to merge 2 commits into
Conversation
…ckage ships The packager copies apple/runner/** into dist/ as Swift source, removing only its AGENT_DEVICE_RUNNER_UNIT_TESTS blocks, so doc comments and design notes were downloaded on every install: 71.9 kB of 441.2 kB of packaged runner Swift. Add a lexical scanner for the removal. A regex cannot do this: `//` and `/*` open a comment only in code position, raw literals move their own delimiter and escape with the `#` count, interpolation segments hold code and further literals, and Swift block comments nest. A construct the scanner cannot account for throws at packaging time instead of shipping Swift that does not compile.
Size Report
Startup median (7 runs, lower is better):
|
|
One packaging safety gap at d95e6ab: valid Swift extended regex literals are silently rewritten. |
`#/foo//bar/#` is a valid extended regex literal with no comment in it, but the scanner only knew the `#"` raw-string family, so it read the literal's `//` as a line comment and shipped `let pattern = #/foo` — Swift that does not compile. Add `#/…/#` and `##/…/##` as a literal context: matching `#` counts, the single- and multi-line forms, Swift's own-line rule for a multi-line closing delimiter, and the `\/` escape that keeps one from closing early. Bare `/…/` literals stay unresolvable, because the same `/` opens a comment, divides, and starts a regex literal, and only the parse separates them. Where one could begin — an expression position whose `/` is not followed by a space, a tab or `)` — packaging throws by file and line instead of rewriting bytes it cannot prove are code. Divisions (`width/2`, `Double(3)/Double(4)`), the recording scripts' shebang and `(/)` keep flowing through.
|
You were right, and the characterisation was exact: at d95e6ab the scanner only knew the What changed in
What the new regressions prove
Every Swift snippet in those tests was checked against Re-validated on 9770a12
|
|
The regex corruption is fixed at 9770a12: the original failing literal now survives unchanged, and the new tests cover delimiters, escapes and unsupported bare-regex syntax. No remaining code findings. The reported packaged iOS/macOS builds pass; marking ready for human review while the remaining smoke check finishes. |
|
Before this merges — I want to challenge the size of it, with measurements, because I think ~95% of the machinery is buying ~0.6% of the bytes. I measured the shipped corpus (
So 99.4% of the comment bytes are whole-line A much smaller rule gets essentially the same saving: delete lines whose trimmed text starts with What that trades away is real but small: the ~652 B of trailing comments stay, and doc comments on the same line as code stay. Call it 108.7 kB recovered instead of 109.4 kB. The reason I think this matters more than the byte count: the current design can fail a publish on valid Swift. The bare-slash heuristic decides expression position from a 128-char lookbehind, and if it guesses wrong on some future file it throws rather than shipping. A rule that only ever deletes a line that is entirely a comment cannot corrupt code and cannot block a release — worst case it leaves a comment in. Two smaller points, whichever way you go:
Happy to cut the smaller version if you agree; I did not want to rewrite an approved PR on my own initiative. |
|
The current code verdict is unchanged, and checks are green. The smaller approach is worth considering, but tracking only triple-quoted strings is not enough to guarantee safety: a line starting with // can also be content inside a multiline regex literal. Before replacing the scanner, define the supported input and prove that other files are preserved unchanged. This is a design choice before merging, not a new failure in the reviewed head. |
Summary
apple/runner/**ships to npm as Swift source, and the packager rewrote it only to remove#if AGENT_DEVICE_RUNNER_UNIT_TESTSblocks — so every doc comment and design note was downloadedon every install.
scripts/package-apple-runner-source.mjsnow also strips comments, through a newscripts/strip-swift-comments.mjs.A regex cannot do this safely:
//and/*open a comment only in code position, a raw literal(
#"…"#) moves its own closing delimiter and interpolation opener with the#count, interpolationsegments hold code including further literals, extended regex literals (
#/…/#) are a seconddelimiter family that also opens on a
#run, and Swift block comments nest. The module is alexical scanner over those states; a construct it cannot account for throws at packaging time
rather than shipping Swift that does not compile. A line whose only content was a comment
disappears; blank lines and every byte inside a literal survive. The unit-test block strip is
unchanged and still runs first, so which blocks it removes does not depend on comment removal.
5 files touched.
Bare
/…/regex literals are the one construct no scanner can resolve — the same/opens acomment, divides, and starts a regex literal, and only the parse separates them. Where one could
begin (an expression position whose
/is not followed by a space, a tab or)), packaging throwsby file and line instead of rewriting bytes it cannot prove are code. Divisions (
width/2,Double(3)/Double(4)), the recording scripts'#!shebang and(/)flow through untouched.dist/apple/runner/555,907 B -> 484,009 B (-71,898 B, -12.9%); its Swift alone 441,196 B ->369,298 B (-16.3%). Closes #2461.
Validation
Tested commit 9770a12.
pnpm format,pnpm lint,pnpm typecheck,pnpm check:affected --run(which runscheck:fallow --base origin/main),pnpm check:xctest-selection: all pass.xcodebuild build-for-testingondist/apple/runner/.../AgentDeviceRunner.xcodeprojreports
** TEST BUILD SUCCEEDED **for bothgeneric/platform=iOS Simulatorandplatform=macOS,arch=arm64. The two runtime-compiled recording scripts compile withxcrun swiftc(0 errors), and all 44 packaged Swift files passxcrun swiftc -parse.xcrun swiftc -parse(Swift 6.2.3) both as written and as the scanner leaves it, so the fixtures are valid Swift
rather than a guess at the grammar.
No
xcodebuild testwas run. Confirm the Bundle Size job for the one-time drop.