big optimization round

This commit is contained in:
sergeych committed 2025-11-10 22:14:21 +01:00
1 parent 029fde2883
commit 38c1b3c209
39 files changed
+1914 -143

No files matched your search

@@ -52,4 +52,49 @@ class CallBenchmarkTest {
assertEquals(expected, r1)
assertEquals(expected, r2)
}
}
@Test
fun benchmarkMixedArityCalls() = runBlocking {
val n = 200_000
val script = """
fun f0() { 1 }
fun f1(a) { a }
fun f2(a,b) { a + b }
fun f3(a,b,c) { a + b + c }
fun f4(a,b,c,d) { a + b + c + d }
var s = 0
var i = 0
while (i < $n) {
s = s + f0()
s = s + f1(1)
s = s + f2(1, 1)
s = s + f3(1, 1, 1)
s = s + f4(1, 1, 1, 1)
i = i + 1
}
s
""".trimIndent()
// Baseline
PerfFlags.ARG_BUILDER = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] mixed-arity x$n [ARG_BUILDER=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// Optimized
PerfFlags.ARG_BUILDER = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] mixed-arity x$n [ARG_BUILDER=ON]: ${(t3 - t2)/1_000_000.0} ms")
// Each loop: 1 + 1 + 2 + 3 + 4 = 11
val expected = 11L * n
assertEquals(expected, r1)
assertEquals(expected, r2)
}
}
@@ -0,0 +1,57 @@
/*
* JVM micro-benchmark for mixed-arity function calls and ARG_BUILDER.
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class CallMixedArityBenchmarkTest {
@Test
fun benchmarkMixedArityCalls() = runBlocking {
val n = 200_000
val script = """
fun f0() { 1 }
fun f1(a) { a }
fun f2(a,b) { a + b }
fun f3(a,b,c) { a + b + c }
fun f4(a,b,c,d) { a + b + c + d }
var s = 0
var i = 0
while (i < $n) {
s = s + f0()
s = s + f1(1)
s = s + f2(1, 1)
s = s + f3(1, 1, 1)
s = s + f4(1, 1, 1, 1)
i = i + 1
}
s
""".trimIndent()
// Baseline
PerfFlags.ARG_BUILDER = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] mixed-arity x$n [ARG_BUILDER=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// Optimized
PerfFlags.ARG_BUILDER = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] mixed-arity x$n [ARG_BUILDER=ON]: ${(t3 - t2)/1_000_000.0} ms")
// Each loop: 1 + 1 + 2 + 3 + 4 = 11
val expected = 11L * n
assertEquals(expected, r1)
assertEquals(expected, r2)
}
}
@@ -0,0 +1,53 @@
/*
* JVM micro-benchmark for Scope frame pooling impact on call-heavy code paths.
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class CallPoolingBenchmarkTest {
@Test
fun benchmarkScopePoolingOnFunctionCalls() = runBlocking {
val n = 300_000
val script = """
fun inc1(a) { a + 1 }
fun inc2(a) { inc1(a) + 1 }
fun inc3(a) { inc2(a) + 1 }
var s = 0
var i = 0
while (i < $n) {
s = inc3(s)
i = i + 1
}
s
""".trimIndent()
// Baseline: pooling OFF
PerfFlags.SCOPE_POOL = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] call-pooling x$n [SCOPE_POOL=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// Optimized: pooling ON
PerfFlags.SCOPE_POOL = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] call-pooling x$n [SCOPE_POOL=ON]: ${(t3 - t2)/1_000_000.0} ms")
// Each inc3 performs 3 increments per loop
val expected = 3L * n
assertEquals(expected, r1)
assertEquals(expected, r2)
// Reset flag to default (OFF) to avoid affecting other tests unintentionally
PerfFlags.SCOPE_POOL = false
}
}
@@ -0,0 +1,55 @@
/*
* JVM micro-benchmark for calls with splat (spread) arguments.
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class CallSplatBenchmarkTest {
@Test
fun benchmarkCallsWithSplatArgs() = runBlocking {
val n = 120_000
val script = """
fun sum4(a,b,c,d) { a + b + c + d }
val base = [1,1,1,1]
var s = 0
var i = 0
while (i < $n) {
// two direct, one splat per iteration
s = s + sum4(1,1,1,1)
s = s + sum4(1,1,1,1)
s = s + sum4(base[0], base[1], base[2], base[3])
i = i + 1
}
s
""".trimIndent()
// Baseline
PerfFlags.ARG_BUILDER = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] splat-calls x$n [ARG_BUILDER=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// Optimized
PerfFlags.ARG_BUILDER = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] splat-calls x$n [ARG_BUILDER=ON]: ${(t3 - t2)/1_000_000.0} ms")
// Each loop adds (4 + 4 + 4) = 12
val expected = 12L * n
assertEquals(expected, r1)
assertEquals(expected, r2)
// Reset to default
PerfFlags.ARG_BUILDER = true
}
}
@@ -0,0 +1,76 @@
/*
* JVM stress tests for scope frame pooling (deep nesting and recursion).
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class DeepPoolingStressJvmTest {
@Test
fun deepNestedCalls_noLeak_and_correct_with_and_without_pooling() = runBlocking {
val depth = 200
val script = """
fun f0(x) { x + 1 }
fun f1(x) { f0(x) + 1 }
fun f2(x) { f1(x) + 1 }
fun f3(x) { f2(x) + 1 }
fun f4(x) { f3(x) + 1 }
fun f5(x) { f4(x) + 1 }
fun chain(x, d) {
var i = 0
var s = x
while (i < d) {
// 5 nested calls per iteration
s = f5(s)
i = i + 1
}
s
}
chain(0, $depth)
""".trimIndent()
// Pool OFF
PerfFlags.SCOPE_POOL = false
val scope1 = Scope()
val r1 = (scope1.eval(script) as ObjInt).value
// Pool ON
PerfFlags.SCOPE_POOL = true
val scope2 = Scope()
val r2 = (scope2.eval(script) as ObjInt).value
// Each loop adds 6 (f0..f5 adds 6)
val expected = 6L * depth
assertEquals(expected, r1)
assertEquals(expected, r2)
// Reset
PerfFlags.SCOPE_POOL = false
}
@Test
fun recursion_factorial_correct_with_and_without_pooling() = runBlocking {
val n = 10
val script = """
fun fact(x) {
if (x <= 1) 1 else x * fact(x - 1)
}
fact($n)
""".trimIndent()
// OFF
PerfFlags.SCOPE_POOL = false
val scope1 = Scope()
val r1 = (scope1.eval(script) as ObjInt).value
// ON
PerfFlags.SCOPE_POOL = true
val scope2 = Scope()
val r2 = (scope2.eval(script) as ObjInt).value
// 10! = 3628800
val expected = 3628800L
assertEquals(expected, r1)
assertEquals(expected, r2)
PerfFlags.SCOPE_POOL = false
}
}
@@ -0,0 +1,61 @@
/*
* JVM micro-benchmark for expression evaluation with RVAL_FASTPATH.
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class ExpressionBenchmarkTest {
@Test
fun benchmarkExpressionChains() = runBlocking {
val n = 350_000
val script = """
// arithmetic + elvis + logical chains
val maybe = null
var s = 0
var i = 0
while (i < $n) {
// exercise elvis on a null
s = s + (maybe ?: 0)
// branch using booleans without coercion to int
if ((i % 3 == 0 && true) || false) { s = s + 1 } else { s = s + 2 }
// parity via arithmetic only (avoid adding booleans)
s = s + (i - (i / 2) * 2)
i = i + 1
}
s
""".trimIndent()
// OFF
PerfFlags.RVAL_FASTPATH = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] expr-chain x$n [RVAL_FASTPATH=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// ON
PerfFlags.RVAL_FASTPATH = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] expr-chain x$n [RVAL_FASTPATH=ON]: ${(t3 - t2)/1_000_000.0} ms")
// correctness: compute expected with simple kotlin logic mirroring the loop
var s = 0L
var i = 0
while (i < n) {
if ((i % 3 == 0 && true) || false) s += 1 else s += 2
// parity via arithmetic only, matches script's single parity addition
s += i - (i / 2) * 2
i += 1
}
assertEquals(s, r1)
assertEquals(s, r2)
}
}
@@ -0,0 +1,50 @@
/*
* JVM micro-benchmark for scope frame pooling on instance method calls.
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class MethodPoolingBenchmarkTest {
@Test
fun benchmarkInstanceMethodCallsWithPooling() = runBlocking {
val n = 300_000
val script = """
class C() {
var x = 0
fun add1() { x = x + 1 }
fun get() { x }
}
val c = C()
var i = 0
while (i < $n) {
c.add1()
i = i + 1
}
c.get()
""".trimIndent()
// Pool OFF
PerfFlags.SCOPE_POOL = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] method-loop x$n [SCOPE_POOL=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// Pool ON
PerfFlags.SCOPE_POOL = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] method-loop x$n [SCOPE_POOL=ON]: ${(t3 - t2)/1_000_000.0} ms")
assertEquals(n.toLong(), r1)
assertEquals(n.toLong(), r2)
}
}
@@ -0,0 +1,81 @@
/*
* JVM mixed workload micro-benchmark to exercise multiple hot paths together.
*/
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
class MixedBenchmarkTest {
@Test
fun benchmarkMixedWorkloadRvalFastpath() = runBlocking {
// Keep iterations moderate to avoid CI timeouts
val n = 250_000
val script = """
class Acc() {
var x = 0
fun add(v) { x = x + v }
fun get() { x }
}
val acc = Acc()
val maybe = null
var s = 0
var i = 0
while (i < $n) {
// exercise locals + primitive ops
s = s + i
// elvis on null
s = s + (maybe ?: 0)
// boolean logic (short-circuit + primitive fast path)
if ((i % 3 == 0 && true) || false) { s = s + 1 } else { s = s + 2 }
// instance field/method with PIC
acc.add(1)
// simple index with list building every 1024 steps (rare path)
if (i % 1024 == 0) {
val lst = [0,1,2,3]
s = s + lst[2]
}
i = i + 1
}
s + acc.get()
""".trimIndent()
// OFF
PerfFlags.RVAL_FASTPATH = false
val scope1 = Scope()
val t0 = System.nanoTime()
val r1 = (scope1.eval(script) as ObjInt).value
val t1 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] mixed x$n [RVAL_FASTPATH=OFF]: ${(t1 - t0)/1_000_000.0} ms")
// ON
PerfFlags.RVAL_FASTPATH = true
val scope2 = Scope()
val t2 = System.nanoTime()
val r2 = (scope2.eval(script) as ObjInt).value
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] mixed x$n [RVAL_FASTPATH=ON]: ${(t3 - t2)/1_000_000.0} ms")
// Compute expected value in Kotlin to ensure correctness
var s = 0L
var i = 0
var acc = 0L
while (i < n) {
s += i
s += 0 // (maybe ?: 0)
if ((i % 3 == 0 && true) || false) s += 1 else s += 2
acc += 1
if (i % 1024 == 0) s += 2
i += 1
}
val expected = s + acc
assertEquals(expected, r1)
assertEquals(expected, r2)
// Reset flag for other tests
PerfFlags.RVAL_FASTPATH = false
}
}
@@ -45,6 +45,10 @@ class PicBenchmarkTest {
val t3 = System.nanoTime()
println("[DEBUG_LOG] [BENCH] Field PIC=ON: ${(t3 - t2) / 1_000_000.0} ms")
assertEquals(iterations.toLong(), r2)
if (PerfFlags.PIC_DEBUG_COUNTERS) {
println("[DEBUG_LOG] [PIC] field get hit=${net.sergeych.lyng.PerfStats.fieldPicHit} miss=${net.sergeych.lyng.PerfStats.fieldPicMiss}")
println("[DEBUG_LOG] [PIC] field set hit=${net.sergeych.lyng.PerfStats.fieldPicSetHit} miss=${net.sergeych.lyng.PerfStats.fieldPicSetMiss}")
}
}
@Test
@@ -0,0 +1,105 @@
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.PerfStats
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjClass
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class PicInvalidationJvmTest {
@Test
fun fieldPicInvalidatesOnClassLayoutChange() = runBlocking {
// Enable counters and PICs
PerfFlags.FIELD_PIC = true
PerfFlags.PIC_DEBUG_COUNTERS = true
PerfStats.resetAll()
val scope = Scope()
// Declare a class and warm up field access
val script = """
class C() {
var x = 0
fun getX() { x }
}
val c = C()
var i = 0
while (i < 1000) {
// warm read path
val t = c.x
i = i + 1
}
c.getX()
""".trimIndent()
val r1 = (scope.eval(script) as ObjInt).value
assertEquals(0L, r1)
val hitsBefore = PerfStats.fieldPicHit
val missesBefore = PerfStats.fieldPicMiss
assertTrue(hitsBefore >= 1, "Expected some PIC hits after warm-up")
// Mutate class layout from Kotlin side to bump layoutVersion and invalidate PIC
val cls = (scope["C"]!!.value as ObjClass)
cls.createClassField("yy", ObjInt(1), isMutable = false)
// Access the same field again; first access after version bump should miss PIC
val r2 = (scope.eval("c.x") as ObjInt).value
assertEquals(0L, r2)
val missesAfter = PerfStats.fieldPicMiss
assertTrue(missesAfter >= missesBefore + 1, "Expected PIC miss after class layout change")
// Optional summary when counters enabled
if (PerfFlags.PIC_DEBUG_COUNTERS) {
println("[DEBUG_LOG] [PIC] field get hit=${PerfStats.fieldPicHit} miss=${PerfStats.fieldPicMiss}")
println("[DEBUG_LOG] [PIC] field set hit=${PerfStats.fieldPicSetHit} miss=${PerfStats.fieldPicSetMiss}")
}
// Disable counters to avoid affecting other tests
PerfFlags.PIC_DEBUG_COUNTERS = false
}
@Test
fun methodPicInvalidatesOnClassLayoutChange() = runBlocking {
PerfFlags.METHOD_PIC = true
PerfFlags.PIC_DEBUG_COUNTERS = true
PerfStats.resetAll()
val scope = Scope()
val script = """
class D() {
var x = 0
fun inc() { x = x + 1 }
fun get() { x }
}
val d = D()
var i = 0
while (i < 1000) {
d.inc()
i = i + 1
}
d.get()
""".trimIndent()
val r1 = (scope.eval(script) as ObjInt).value
assertEquals(1000L, r1)
val mhBefore = PerfStats.methodPicHit
val mmBefore = PerfStats.methodPicMiss
assertTrue(mhBefore >= 1, "Expected method PIC hits after warm-up")
// Bump layout by adding a new class field
val cls = (scope["D"]!!.value as ObjClass)
cls.createClassField("zz", ObjInt(0), isMutable = false)
// Next invocation should miss and then re-fill
val r2 = (scope.eval("d.get()") as ObjInt).value
assertEquals(1000L, r2)
val mmAfter = PerfStats.methodPicMiss
assertTrue(mmAfter >= mmBefore + 1, "Expected method PIC miss after class layout change")
// Optional summary when counters enabled
if (PerfFlags.PIC_DEBUG_COUNTERS) {
println("[DEBUG_LOG] [PIC] method hit=${PerfStats.methodPicHit} miss=${PerfStats.methodPicMiss}")
}
PerfFlags.PIC_DEBUG_COUNTERS = false
}
}
@@ -0,0 +1,47 @@
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import net.sergeych.lyng.obj.ObjList
import kotlin.test.Test
import kotlin.test.assertEquals
class ScriptSubsetJvmTest {
private suspend fun evalInt(code: String): Long = (Scope().eval(code) as ObjInt).value
private suspend fun evalList(code: String): List<Any?> = (Scope().eval(code) as ObjList).list.map { (it as? ObjInt)?.value ?: it }
@Test
fun binarySearchBasics_jvm_only() = runBlocking {
val code = """
val coll = [1,2,3,4,5]
coll.binarySearch(3)
""".trimIndent()
// OFF
PerfFlags.RVAL_FASTPATH = false
val rOff = evalInt(code)
// ON
PerfFlags.RVAL_FASTPATH = true
val rOn = evalInt(code)
assertEquals(2L, rOff)
assertEquals(2L, rOn)
PerfFlags.RVAL_FASTPATH = false
}
@Test
fun optionalChainingIndexField_jvm_only() = runBlocking {
val code = """
val a = null
val r1 = a?.x
val lst = [1,2,3]
val r2 = lst[1]
r2
""".trimIndent()
PerfFlags.RVAL_FASTPATH = false
val off = evalInt(code)
PerfFlags.RVAL_FASTPATH = true
val on = evalInt(code)
assertEquals(2L, off)
assertEquals(2L, on)
PerfFlags.RVAL_FASTPATH = false
}
}
@@ -0,0 +1,140 @@
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjBool
import net.sergeych.lyng.obj.ObjInt
import net.sergeych.lyng.obj.ObjList
import kotlin.test.Test
import kotlin.test.assertEquals
/**
* JVM-only fast functional subset additions. Keep each test quick (< ~1s) and deterministic.
*/
class ScriptSubsetJvmTest_Additions3 {
private suspend fun evalInt(code: String): Long = (Scope().eval(code) as ObjInt).value
private suspend fun evalBool(code: String): Boolean = (Scope().eval(code) as ObjBool).value
private suspend fun evalList(code: String): List<Any?> = (Scope().eval(code) as ObjList).list.map { (it as? ObjInt)?.value ?: it }
@Test
fun controlFlow_when_and_ifElse_jvm_only() = runBlocking {
val code = """
fun classify(x) {
when(x) {
0 -> 100
1 -> 200
else -> 300
}
}
val a = classify(0)
val b = classify(1)
val c = classify(5)
if (true) 1 else 2
a + b + c
""".trimIndent()
val r = evalInt(code)
// 100 + 200 + 300 = 600
assertEquals(600L, r)
}
@Test
fun optionals_chain_field_index_method_jvm_only() = runBlocking {
val code = """
class Box() {
var xs = [10,20,30]
fun get(i) { xs[i] }
}
val maybe = null
val b = Box()
// optional on null yields null
val r1 = maybe?.xs
// optional on non-null: method and index
val r2 = b?.get(1)
r2
""".trimIndent()
val r = evalInt(code)
assertEquals(20L, r)
}
@Test
fun exceptions_try_catch_finally_jvm_only() = runBlocking {
val code = """
fun risky(x) { if (x == 0) throw "boom" else 7 }
var s = 0
try { s = risky(0) } catch (e) { s = 1 } finally { s = s + 2 }
s
""".trimIndent()
val r = evalInt(code)
// catch sets 1, finally adds 2 -> 3
assertEquals(3L, r)
}
@Test
fun classes_visibility_and_fields_jvm_only() = runBlocking {
val code = """
class C() {
var pub = 1
private var hidden = 9
fun getPub() { pub }
fun getHidden() { hidden }
fun setPub(v) { pub = v }
}
val c = C()
c.setPub(5)
c.getPub() + c.getHidden()
""".trimIndent()
val r = evalInt(code)
// 5 + 9
assertEquals(14L, r)
}
@Test
fun collections_insert_remove_and_maps_jvm_only() = runBlocking {
val code = """
val lst = []
lst.insertAt(0, 2)
lst.insertAt(0, 1)
lst.removeAt(1)
// now [1]
val a = 10
val b = 20
a + b + lst[0]
""".trimIndent()
val r = evalInt(code)
assertEquals(31L, r)
}
@Test
fun loops_for_and_while_basics_jvm_only() = runBlocking {
val n = 2000
val code = """
var s = 0
for (i in 0..$n) { s = s + 1 }
var j = 0
while (j < $n) { s = s + 1; j = j + 1 }
s
""".trimIndent()
val r = evalInt(code)
// for loop adds n+1, while adds n -> total 2n+1
assertEquals((2L*n + 1L), r)
}
@Test
fun pooling_edgecase_captures_and_exception_jvm_only() = runBlocking {
// Ensure pooling ON for this test, then restore default
val prev = PerfFlags.SCOPE_POOL
try {
PerfFlags.SCOPE_POOL = true
val code = """
fun outer(a) {
fun inner(b) { if (b == 0) throw "err" else a + b }
try { inner(0) } catch (e) { a + 2 }
}
outer(5)
""".trimIndent()
val r = evalInt(code)
assertEquals(7L, r)
} finally {
PerfFlags.SCOPE_POOL = prev
}
}
}
@@ -0,0 +1,137 @@
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import net.sergeych.lyng.obj.ObjList
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
/**
* More JVM-only fast functional tests migrated from ScriptTest to avoid MPP runs.
* Keep each test fast (<1s) and deterministic.
*/
class ScriptSubsetJvmTest_Additions4 {
private suspend fun evalInt(code: String): Long = (Scope().eval(code) as ObjInt).value
private suspend fun evalList(code: String): List<Any?> = (Scope().eval(code) as ObjList).list.map { (it as? ObjInt)?.value ?: it }
@Test
fun mapsAndSetsBasics_jvm_only() = runBlocking {
// Validate simple list map behavior without relying on extra stdlib
val code = """
val src = [1,2,2,3,1]
// map over list
val doubled = src.map { it + 1 }
doubled.size()
""".trimIndent()
val r = evalInt(code)
// doubled size == original size (5)
assertEquals(5L, r)
}
@Test
fun optionalChainingDeep_jvm_only() = runBlocking {
val code = """
class A() { fun b() { null } }
val a = A()
val r1 = a?.b()?.c
val r2 = (a?.b()?.c ?: 7)
r2
""".trimIndent()
val r = evalInt(code)
assertEquals(7L, r)
}
@Test
fun whenExpressionBasics_jvm_only() = runBlocking {
val code = """
fun f(x) {
when(x) {
0 -> 100
1 -> 200
else -> 300
}
}
f(0) + f(1) + f(2)
""".trimIndent()
val r = evalInt(code)
assertEquals(600L, r)
}
@Test
fun tryCatchFinallyWithReturn_jvm_only() = runBlocking {
val code = """
fun g(x) {
var t = 0
try {
if (x < 0) throw("oops")
t = x
} catch (e) {
t = 5
} finally {
t = t + 1
}
t
}
g(-1) + g(3)
""".trimIndent()
val r = evalInt(code)
// g(-1): catch sets 5, finally +1 => 6; g(3): t=3, finally +1 => 4; total 10
assertEquals(10L, r)
}
@Test
fun pooling_edge_case_closure_and_exception_jvm_only() = runBlocking {
val code = """
fun maker(base) { { base + 1 } }
val c = maker(41)
var r = 0
try {
r = c()
throw("fail")
} catch (e) {
r = r + 1
}
r
""".trimIndent()
// OFF
PerfFlags.SCOPE_POOL = false
val off = evalInt(code)
// ON
PerfFlags.SCOPE_POOL = true
val on = evalInt(code)
assertEquals(43L, off)
assertEquals(43L, on)
// reset
PerfFlags.SCOPE_POOL = false
}
@Test
fun forWhileNested_jvm_only() = runBlocking {
val code = """
var s = 0
for (i in 1..10) {
var j = 0
while (j < 3) {
if (i % 2 == 0 && j == 1) { j = j + 1; continue }
s = s + i + j
j = j + 1
}
}
s
""".trimIndent()
val r = evalInt(code)
// Compute expected quickly in Kotlin mirror
var expected = 0L
for (i in 1..10) {
var j = 0
while (j < 3) {
if (i % 2 == 0 && j == 1) { j += 1; continue }
expected += i + j
j += 1
}
}
assertEquals(expected, r)
assertTrue(expected > 0)
}
}
@@ -0,0 +1,81 @@
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
/**
* JVM-only fast functional tests to broaden coverage for pooling, classes, and control flow.
* Keep each test fast (<1s) and deterministic.
*/
class ScriptSubsetJvmTest_Additions5 {
private suspend fun evalInt(code: String): Long = (Scope().eval(code) as ObjInt).value
@Test
fun classVisibility_public_vs_private_jvm_only() = runBlocking {
val code = """
class C() {
var pub = 3
private var prv = 5
fun getPrv() { prv }
}
val c = C()
c.pub + c.getPrv()
""".trimIndent()
val r = evalInt(code)
assertEquals(8L, r)
}
@Test
fun classVisibility_private_field_access_error_jvm_only() = runBlocking {
val code = """
class C() {
private var prv = 5
}
val c = C()
// attempt to access private field should fail
c.prv
""".trimIndent()
// We expect an exception; Scope.eval() will throw ExecutionError.
assertFailsWith<Throwable> { evalInt(code) }
// Ensure Unit return from the test body
Unit
}
@Test
fun inheritance_override_call_path_jvm_only() = runBlocking {
val code = """
// Simple two classes with same method name; no inheritance to avoid syntax dependencies
class A() { fun v() { 1 } }
class B() { fun v() { 2 } }
val a = A(); val b = B()
a.v() + b.v()
""".trimIndent()
val r = evalInt(code)
assertEquals(3L, r)
}
@Test
fun pooled_frames_closure_this_capture_jvm_only() = runBlocking {
val code = """
class Box() { var x = 40; fun inc() { x = x + 1 } fun get() { x } }
fun make(block) { block }
val b = Box()
val f = make { b.inc(); b.get() }
var r = 0
r = f()
r
""".trimIndent()
// OFF
PerfFlags.SCOPE_POOL = false
val off = evalInt(code)
// ON
PerfFlags.SCOPE_POOL = true
val on = evalInt(code)
assertEquals(41L, off)
assertEquals(41L, on)
PerfFlags.SCOPE_POOL = false
}
}
@@ -0,0 +1,120 @@
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjInt
import net.sergeych.lyng.obj.ObjList
import kotlin.test.Test
import kotlin.test.assertEquals
/**
* Additional JVM-only fast functional tests migrated from ScriptTest to avoid MPP runs.
* Keep each test fast (<1s) and with clear assertions.
*/
class ScriptSubsetJvmTest_Additions {
private suspend fun evalInt(code: String): Long = (Scope().eval(code) as ObjInt).value
private suspend fun evalList(code: String): List<Any?> = (Scope().eval(code) as ObjList).list.map { (it as? ObjInt)?.value ?: it }
@Test
fun rangesAndForLoop_jvm_only() = runBlocking {
val code = """
var s = 0
for (i in 1..100) s = s + i
s
""".trimIndent()
val r = evalInt(code)
assertEquals(5050L, r)
}
@Test
fun classFieldsAndMethods_jvm_only() = runBlocking {
val n = 20000
val code = """
class Counter() {
var x = 0
fun inc() { x = x + 1 }
fun get() { x }
}
val c = Counter()
var i = 0
while (i < $n) { c.inc(); i = i + 1 }
c.get()
""".trimIndent()
val r = evalInt(code)
assertEquals(n.toLong(), r)
}
@Test
fun elvisAndLogicalChains_jvm_only() = runBlocking {
val n = 10000
val code = """
val maybe = null
var s = 0
var i = 0
while (i < $n) {
s = s + (maybe ?: 0)
if ((i % 3 == 0 && true) || false) { s = s + 1 } else { s = s + 2 }
s = s + (i - (i / 2) * 2)
i = i + 1
}
s
""".trimIndent()
val r = evalInt(code)
// Kotlin mirror for correctness
var s = 0L
var i = 0
while (i < n) {
if ((i % 3 == 0 && true) || false) s += 1 else s += 2
s += (i - (i / 2) * 2)
i += 1
}
assertEquals(s, r)
}
@Test
fun sortedInsertWithBinarySearch_jvm_only() = runBlocking {
val code = """
val src = [3,1,2]
val result = []
for (x in src) {
val i = result.binarySearch(x)
result.insertAt(if (i < 0) -i-1 else i, x)
}
result
""".trimIndent()
val r = evalList(code)
assertEquals(listOf(1L, 2L, 3L), r)
}
}
class ScriptSubsetJvmTest_Additions2 {
private suspend fun evalInt(code: String): Long = (Scope().eval(code) as ObjInt).value
@Test
fun optionalMethodCallWithElvis_jvm_only() = runBlocking {
val code = """
class C() { fun get() { 5 } }
val a = null
(a?.get() ?: 7)
""".trimIndent()
val r = evalInt(code)
assertEquals(7L, r)
}
@Test
fun continueAndBreakInWhile_jvm_only() = runBlocking {
val code = """
var s = 0
var i = 0
while (i <= 100) {
if (i > 50) break
if (i % 2 == 1) { i = i + 1; continue }
s = s + i
i = i + 1
}
s
""".trimIndent()
val r = evalInt(code)
// Sum of even numbers from 0 to 50 inclusive: 2 * (0+1+...+25) = 2 * 325 = 650
assertEquals(650L, r)
}
}