Optimize primitive list fill capacity and append

This commit is contained in:
sergeych committed 2026-04-21 21:37:04 +03:00
1 parent fbb5688696
commit 953f237ca3
10 files changed
+155 -17

No files matched your search

@@ -213,6 +213,24 @@ class BytecodeRecentOpsTest {
assertEquals(4, scope.eval("calc()").toInt())
}
@Test
fun listFillWithCapacityUsesPrimitiveCapacityBytecode() = runTest {
val scope = Script.newScope()
scope.eval(
"""
fun calc() {
val xs = List.fill(5, 12) { it * 2 }
xs.add(99)
xs[0] + xs[4] + xs[5]
}
""".trimIndent()
)
val disasm = scope.disassembleSymbol("calc")
assertTrue(disasm.contains("LIST_NEW_INT_CAP"), disasm)
assertFalse(disasm.contains("LIST_FILL_INT_CAP"), disasm)
assertEquals(107, scope.eval("calc()").toInt())
}
@Test
fun directLambdaLiteralCallUsesInlineBytecode() = runTest {
val scope = Script.newScope()
@@ -22,6 +22,7 @@ import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.obj.toInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeSource
class OptTest {
@@ -59,4 +60,25 @@ class OptTest {
}
println("add-to-array best=${bestMs}ms avg=${totalMs / passes}ms after warmup")
}
@Test
fun testAddToArray2() = runTest {
eval(
$$"""
import lyng.time
val n = 700_000
fun tm<T>(block: ()->T): T {
val t = Instant()
block().also {
println("tm: ${Instant() - t}")
}
}
val x = tm { List.fill(n) { it * 10 + 1 } }
val y = tm { List.fill(n, n + 10) { it * 10 + 1 } }
tm { x.add(-1) }
tm { y.add(-2) }
""".trimIndent()
)
}
}