Skip to content

Commit 845ba18

Browse files
shai-almogclaude
andcommitted
js-port: peephole-collapse the call-result temp and identity copy-pairs
{ let __result = <call>; X = __result; } -> X = <call>; (RHS fully evaluates before the store, so the rewrite is sound even when X appears in the call expression), plus the adjacent X = Y; Y = X; identity pair. Modest raw win (-13KB; esbuild already collapses most of these downstream) but it also shrinks the pre-esbuild chunks the whitespace budget and chunk splitter operate on. Suite green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 407555f commit 845ba18

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

vm/ByteCodeTranslator/src/com/codename1/tools/translator/JavascriptMethodGenerator.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,25 @@ private static String applyMethodPeephole(CharSequence body) {
11071107
s = applyVirtualRule(s,
11081108
"stack\\.p\\(([a-zA-Z_\\$][\\w\\$]*(?:\\[\\d+\\])*(?:\\[\"[\\w\\$]+\"\\])*)\\);?\\s*stack\\.p\\(([^;(){},]+)\\);?\\s*stack\\.p\\(([^;(){},]+)\\);?\\s*\\{ let __arg1 = stack\\.q\\(\\); let __arg0 = stack\\.q\\(\\); yield\\* cn1_iv2\\(stack\\.q\\(\\), \"([^\"]+)\", __arg0, __arg1\\); (pc = \\d+; break;) \\}",
11091109
"yield* cn1_iv2($1, \"$4\", $2, $3); $5");
1110+
// Rule 18: collapse the straight-line call-result temp.
1111+
// { let __result = <call-expr>; X = __result; }
1112+
// → X = <call-expr>;
1113+
// The temp is created and consumed in the same block with
1114+
// exactly one use, and a JS assignment fully evaluates its
1115+
// RHS before the store, so the rewrite cannot change
1116+
// semantics even when X also appears inside the call
1117+
// expression (``b = yield* cn1_iv0(b, ...)``). This is the
1118+
// single largest scaffolding pattern in the straight-line
1119+
// emitter's output (one per non-void invocation).
1120+
s = s.replaceAll(
1121+
"\\{\\s*let __result = ((?:yield\\* )?[^;]+);\\s*([\\w\\$]+(?:\\[\\d+\\])?) = __result;\\s*\\}",
1122+
"$2 = $1;");
1123+
// Rule 18b: identity copy-pair.
1124+
// X = Y; Y = X; → X = Y;
1125+
// The second statement re-stores Y's own value.
1126+
s = s.replaceAll(
1127+
"(\\s)([\\w\\$]+) = ([\\w\\$]+);\\s+\\3 = \\2;",
1128+
"$1$2 = $3;");
11101129
// Rule 17: array load (AALOAD/IALOAD/BALOAD/CALOAD/SALOAD)
11111130
// with inlined array + index pushes.
11121131
// stack.p(A); stack.p(I);

0 commit comments

Comments
 (0)