optimize arithmetics
This commit is contained in:
1 parent
161f3f74e2
commit
d8454a11fc
21 files changed
+913
-208
No files matched your search
@@ -16,21 +16,11 @@
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.Compiler
|
||||
import net.sergeych.lyng.ExecutionError
|
||||
import net.sergeych.lyng.Pos
|
||||
import net.sergeych.lyng.Script
|
||||
import net.sergeych.lyng.ScriptError
|
||||
import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.eval
|
||||
import net.sergeych.lyng.toSource
|
||||
import net.sergeych.lyng.bytecode.CmdDisassembler
|
||||
import net.sergeych.lyng.bytecode.CmdFunction
|
||||
import net.sergeych.lyng.*
|
||||
import net.sergeych.lyng.obj.toInt
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class BytecodeRecentOpsTest {
|
||||
@@ -129,6 +119,79 @@ class BytecodeRecentOpsTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun intListIndexOpsUsePrimitiveBytecode() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval(
|
||||
"""
|
||||
fun calc() {
|
||||
var a: List<Int> = [1, 2, 3]
|
||||
val s = a[1]
|
||||
a[1] += 3
|
||||
a[1]++
|
||||
a[1] = s
|
||||
a[1]
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
val disasm = scope.disassembleSymbol("calc")
|
||||
assertTrue(disasm.contains("GET_INDEX_INT"), disasm)
|
||||
assertTrue(disasm.contains("SET_INDEX_INT"), disasm)
|
||||
assertEquals(2, scope.eval("calc()").toInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun listFillIntIndexOpsUsePrimitiveBytecode() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval(
|
||||
"""
|
||||
fun calc() {
|
||||
var a = List.fill(4) { 2 }
|
||||
val s = a[1]
|
||||
a[1] += 3
|
||||
a[1] = s
|
||||
a[1]
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
val disasm = scope.disassembleSymbol("calc")
|
||||
assertTrue(disasm.contains("GET_INDEX_INT"), disasm)
|
||||
assertTrue(disasm.contains("SET_INDEX_INT"), disasm)
|
||||
assertEquals(2, scope.eval("calc()").toInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun listFillIntUsesPrimitiveFillBytecode() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval(
|
||||
"""
|
||||
fun calc() {
|
||||
val xs = List.fill(5) { 2 }
|
||||
xs[0] + xs[4]
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
val disasm = scope.disassembleSymbol("calc")
|
||||
assertTrue(disasm.contains("LIST_FILL_INT"), disasm)
|
||||
assertEquals(4, scope.eval("calc()").toInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun listFillIntWithIndexLambdaKeepsSemantics() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval(
|
||||
"""
|
||||
fun calc() {
|
||||
val xs = List.fill(5) { it * 3 }
|
||||
xs[0] + xs[4]
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
val disasm = scope.disassembleSymbol("calc")
|
||||
assertTrue(disasm.contains("LIST_FILL_INT"), disasm)
|
||||
assertEquals(12, scope.eval("calc()").toInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun optionalIndexPreIncSkipsOnNullReceiver() = runTest {
|
||||
eval(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -97,7 +97,7 @@ class TestCoroutines {
|
||||
counter = c + 1
|
||||
// }
|
||||
}
|
||||
}.forEach { (it as Deferred).await() }
|
||||
}.forEach { it.await() }
|
||||
println(counter)
|
||||
assert( counter < 10 )
|
||||
|
||||
@@ -105,6 +105,21 @@ class TestCoroutines {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMapForEachDeferredInference() = runTest {
|
||||
eval(
|
||||
"""
|
||||
var sum = 0
|
||||
|
||||
(1..3).map { n ->
|
||||
launch { n }
|
||||
}.forEach { sum += it.await() }
|
||||
|
||||
assertEquals(6, sum)
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFlows() = runTest {
|
||||
eval("""
|
||||
|
||||
Reference in new issue
Block a user