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 loopStack = ArrayDeque<LoopContext>()
private var currentPos: Pos? = null
private var cachedVoidSlot: Int? = null
private data class LoopContext(
val label: String?,
@ -5623,6 +5624,17 @@ class BytecodeCompiler(
private fun emitInlineBlock(stmt: BlockStatement, needResult: Boolean): CompiledValue? =
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 {
return allowLocalSlots
}
@ -6449,10 +6461,7 @@ class BytecodeCompiler(
restoreFlowTypeOverride(elseRestore)
}
builder.mark(endLabel)
val slot = allocSlot()
val voidId = builder.addConst(BytecodeConst.ObjRef(ObjVoid))
builder.emit(Opcode.CONST_OBJ, voidId, slot)
return CompiledValue(slot, SlotType.OBJ)
return CompiledValue(ensureVoidSlot(), SlotType.OBJ)
}
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)
- Skip redundant OBJ_TO_BOOL in logical AND/OR when compiler already emits BOOL.
- MixedCompareBenchmarkTest: 275 ms -> 249 ms.
4) Range/loop hot path
- Keep range iteration in INT ops, avoid accidental boxing.
- Confirm loop-var slots avoid re-boxing in loop bodies.
4) Range/loop hot path (done)
- Reuse a cached ObjVoid slot for if-statements in statement context (avoids per-iteration CONST_OBJ).
- MixedCompareBenchmarkTest: 249 ms -> 247 ms.
5) String ops
- Extend fast path for string comparisons in hot loops.
6) Box/unbox audit