Reuse void slot for if statements

This commit is contained in:
Sergey Chernov 2026-02-16 17:50:39 +03:00
parent 7c28296f92
commit fdb23b3a76
2 changed files with 16 additions and 7 deletions

View File

@ -86,6 +86,7 @@ class BytecodeCompiler(
private val loopVarSlots = HashSet<Int>() private val loopVarSlots = HashSet<Int>()
private val loopStack = ArrayDeque<LoopContext>() private val loopStack = ArrayDeque<LoopContext>()
private var currentPos: Pos? = null private var currentPos: Pos? = null
private var cachedVoidSlot: Int? = null
private data class LoopContext( private data class LoopContext(
val label: String?, val label: String?,
@ -5623,6 +5624,17 @@ class BytecodeCompiler(
private fun emitInlineBlock(stmt: BlockStatement, needResult: Boolean): CompiledValue? = private fun emitInlineBlock(stmt: BlockStatement, needResult: Boolean): CompiledValue? =
emitInlineStatements(stmt.statements(), needResult) emitInlineStatements(stmt.statements(), needResult)
private fun ensureVoidSlot(): Int {
val existing = cachedVoidSlot
if (existing != null) return existing
val slot = allocSlot()
val voidId = builder.addConst(BytecodeConst.ObjRef(ObjVoid))
builder.emit(Opcode.CONST_OBJ, voidId, slot)
updateSlotType(slot, SlotType.OBJ)
cachedVoidSlot = slot
return slot
}
private fun shouldInlineBlock(stmt: BlockStatement): Boolean { private fun shouldInlineBlock(stmt: BlockStatement): Boolean {
return allowLocalSlots return allowLocalSlots
} }
@ -6449,10 +6461,7 @@ class BytecodeCompiler(
restoreFlowTypeOverride(elseRestore) restoreFlowTypeOverride(elseRestore)
} }
builder.mark(endLabel) builder.mark(endLabel)
val slot = allocSlot() return CompiledValue(ensureVoidSlot(), SlotType.OBJ)
val voidId = builder.addConst(BytecodeConst.ObjRef(ObjVoid))
builder.emit(Opcode.CONST_OBJ, voidId, slot)
return CompiledValue(slot, SlotType.OBJ)
} }
private fun updateSlotTypeByName(name: String, type: SlotType) { private fun updateSlotTypeByName(name: String, type: SlotType) {

View File

@ -13,9 +13,9 @@ Candidates (not started)
3) Boolean conversion (done; do not revert without review) 3) Boolean conversion (done; do not revert without review)
- Skip redundant OBJ_TO_BOOL in logical AND/OR when compiler already emits BOOL. - Skip redundant OBJ_TO_BOOL in logical AND/OR when compiler already emits BOOL.
- MixedCompareBenchmarkTest: 275 ms -> 249 ms. - MixedCompareBenchmarkTest: 275 ms -> 249 ms.
4) Range/loop hot path 4) Range/loop hot path (done)
- Keep range iteration in INT ops, avoid accidental boxing. - Reuse a cached ObjVoid slot for if-statements in statement context (avoids per-iteration CONST_OBJ).
- Confirm loop-var slots avoid re-boxing in loop bodies. - MixedCompareBenchmarkTest: 249 ms -> 247 ms.
5) String ops 5) String ops
- Extend fast path for string comparisons in hot loops. - Extend fast path for string comparisons in hot loops.
6) Box/unbox audit 6) Box/unbox audit