v0.10.1-SNAPSHOT optimization: Accessor refactored now use more effective ObjRef and slots for locals

This commit is contained in:
sergeych committed 2025-11-09 23:19:02 +01:00
1 parent 6ca3e4589e
commit 0b9e94c6e9
17 files changed
+806 -389

No files matched your search

+47 -24
View File
@@ -22,6 +22,7 @@ import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.InlineSourcesImportProvider
import net.sergeych.tools.bm
import kotlin.test.*
class ScriptTest {
@@ -812,7 +813,8 @@ class ScriptTest {
assertEquals(6, c.eval("x").toInt())
assertEquals(6, c.eval("x++").toInt())
assertEquals(7, c.eval("x++").toInt())
assertEquals(8, c.eval("x")
assertEquals(
8, c.eval("x")
.also {
println("${it.toDouble()} ${it.toInt()} ${it.toLong()} ${it.toInt()}")
}
@@ -2252,19 +2254,35 @@ class ScriptTest {
@Test
fun testMatchOperator() = runTest {
eval("""
eval(
"""
assert( "abc123".matches(".*\d{3}") )
assert( ".*\d{3}".re =~ "abc123" )
assert( "abc123" =~ ".*\d{3}".re )
assert( "abc123" !~ ".*\d{4}".re )
println($~)
"abc123" =~ ".*(\d)(\d)(\d)$".re
println($~)
assertEquals("1", $~[1])
"""
)
}
@Test
fun testMatchingOperator2() = runTest {
eval(
"""
"abc123" =~ ".*(\d)(\d)(\d)$".re
println($~)
assertEquals("1", $~[1])
assertEquals("2", $~[2])
assertEquals("3", $~[3])
assertEquals("abc123", $~[0])
""".trimIndent())
""".trimIndent()
)
}
// @Test
@@ -3314,26 +3332,31 @@ class ScriptTest {
}
// @Test
// fun testMinimumOptimization() = runTest {
// val x = Scope().eval(
// """
// fun naiveCountHappyNumbers() {
// var count = 0
// for( n1 in 0..9 )
// for( n2 in 0..9 )
// for( n3 in 0..9 )
// for( n4 in 0..9 )
// for( n5 in 0..9 )
// for( n6 in 0..9 )
// if( n1 + n2 + n3 == n4 + n5 + n6 ) count++
// count
// }
// naiveCountHappyNumbers()
// """.trimIndent()
// ).toInt()
// assertEquals(55252, x)
// }
// @Test
fun testMinimumOptimization() = runTest {
for (i in 1..200) {
bm {
val x = Scope().eval(
"""
fun naiveCountHappyNumbers() {
var count = 0
for( n1 in 0..9 )
for( n2 in 0..9 )
for( n3 in 0..9 )
for( n4 in 0..9 )
for( n5 in 0..9 )
for( n6 in 0..9 )
if( n1 + n2 + n3 == n4 + n5 + n6 ) count++
count
}
naiveCountHappyNumbers()
""".trimIndent()
).toInt()
assertEquals(55252, x)
}
delay(10)
}
}
@Test
fun testRegex1() = runTest {
@@ -0,0 +1,43 @@
/*
* Tests for optional chaining assignment semantics (no-op on null receiver)
*/
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
class ScriptTest_OptionalAssign {
@Test
fun optionalFieldAssignIsNoOp() = runTest {
eval(
"""
class C { var x = 1 }
var c = null
// should be no-op and not throw
c?.x = 5
assertEquals(null, c?.x)
// non-null receiver should work as usual
c = C()
c?.x = 7
assertEquals(7, c.x)
""".trimIndent()
)
}
@Test
fun optionalIndexAssignIsNoOp() = runTest {
eval(
"""
var a = null
// should be no-op and not throw
a?[0] = 42
assertEquals(null, a?[0])
// non-null receiver should work as usual
a = [1,2,3]
a?[1] = 99
assertEquals(99, a[1])
""".trimIndent()
)
}
}