big optimization round
This commit is contained in:
1 parent
029fde2883
commit
38c1b3c209
39 files changed
+1914
-143
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user