Skip to content

Commit 8a865e1

Browse files
fglockclaude
andcommitted
Optimize temporary local variable allocation
Reduce over-allocation of temp locals that was causing bytecode bloat. Before: Math.max(128, tempCount + 64) - minimum 128 slots, 64-slot buffer After: tempCount + 32 - modest 32-slot buffer The original allocation was excessive because TempLocalCountVisitor only counts 3 specific cases (logical operators, for loops, local()), but there are ~90 places in codegen that allocate temp variables dynamically. A 32-slot buffer provides safety margin without the extreme waste of the previous min-128 + 64-buffer approach. Testing: - ✅ All 152 unit tests pass (100% pass rate) - ✅ Reduces bytecode bloat while maintaining safety - Note: Originally tried no buffer (broke perl5 tests), 32 is the balance Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0aaf9a4 commit 8a865e1

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/main/java/org/perlonjava/codegen/EmitterMethodCreator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,14 @@ private static byte[] getBytecodeInternal(EmitterContext ctx, Node ast, boolean
575575
// Temporaries are allocated dynamically during bytecode emission via
576576
// ctx.symbolTable.allocateLocalVariable(). We pre-initialize slots to ensure
577577
// they're not in TOP state when accessed. Use a visitor to estimate the
578-
// actual number needed based on AST structure rather than a fixed count.
578+
// actual number needed based on AST structure.
579+
// Add a modest buffer since visitor doesn't count all allocation sites.
579580
int preInitTempLocalsStart = ctx.symbolTable.getCurrentLocalVariableIndex();
580-
org.perlonjava.astvisitor.TempLocalCountVisitor tempCountVisitor =
581+
org.perlonjava.astvisitor.TempLocalCountVisitor tempCountVisitor =
581582
new org.perlonjava.astvisitor.TempLocalCountVisitor();
582583
ast.accept(tempCountVisitor);
583-
int preInitTempLocalsCount = Math.max(128, tempCountVisitor.getMaxTempCount() + 64); // Add buffer
584+
int preInitTempLocalsCount = tempCountVisitor.getMaxTempCount() + 32; // Add 32-slot buffer
585+
ctx.logDebug("Pre-initializing " + preInitTempLocalsCount + " temp locals");
584586
for (int i = preInitTempLocalsStart; i < preInitTempLocalsStart + preInitTempLocalsCount; i++) {
585587
mv.visitInsn(Opcodes.ACONST_NULL);
586588
mv.visitVarInsn(Opcodes.ASTORE, i);

0 commit comments

Comments
 (0)