big optimization

This commit is contained in:
sergeych committed 2025-11-11 21:22:10 +01:00
1 parent dc3000e9f7
commit fdb056e78e
21 files changed
+876 -100

No files matched your search

@@ -98,6 +98,26 @@ import net.sergeych.lyng.obj.ObjList
if (quick != null) return quick
}
}
// Single-splat fast path: if there is exactly one splat argument that evaluates to ObjList,
// avoid builder and copies by returning its list directly.
if (PerfFlags.ARG_BUILDER) {
if (this.size == 1) {
val only = this.first()
if (only.isSplat) {
val v = only.value.execute(scope)
if (v is ObjList) {
return Arguments(v.list, tailBlockMode)
} else if (v.isInstanceOf(ObjIterable)) {
// Convert iterable to list once and return directly
val i = (v.invokeInstanceMethod(scope, "toList") as ObjList).list
return Arguments(i, tailBlockMode)
} else {
scope.raiseClassCastError("expected list of objects for splat argument")
}
}
}
}
// General path with builder or simple list fallback
if (PerfFlags.ARG_BUILDER) {
val b = ArgBuilderProvider.acquire()
@@ -143,7 +163,7 @@ import net.sergeych.lyng.obj.ObjList
}
return Arguments(list, tailBlockMode)
}
}
}
data class Arguments(val list: List<Obj>, val tailBlockMode: Boolean = false) : List<Obj> by list {
@@ -41,13 +41,22 @@ class Compiler(
private val currentLocalNames: MutableSet<String>?
get() = localNamesStack.lastOrNull()
// Track declared local variables count per function for precise capacity hints
private val localDeclCountStack = mutableListOf<Int>()
private val currentLocalDeclCount: Int
get() = localDeclCountStack.lastOrNull() ?: 0
private inline fun <T> withLocalNames(names: Set<String>, block: () -> T): T {
localNamesStack.add(names.toMutableSet())
return try { block() } finally { localNamesStack.removeLast() }
}
private fun declareLocalName(name: String) {
currentLocalNames?.add(name)
// Add to current function's local set; only count if it was newly added (avoid duplicates)
val added = currentLocalNames?.add(name) == true
if (added && localDeclCountStack.isNotEmpty()) {
localDeclCountStack[localDeclCountStack.lastIndex] = currentLocalDeclCount + 1
}
}
var packageName: String? = null
@@ -1236,18 +1245,23 @@ class Compiler(
val source = parseStatement() ?: throw ScriptError(start, "Bad for statement: expected expression")
ensureRparen()
val (canBreak, body) = cc.parseLoop {
parseStatement() ?: throw ScriptError(start, "Bad for statement: expected loop body")
// Expose the loop variable name to the parser so identifiers inside the loop body
// can be emitted as FastLocalVarRef when enabled.
val namesForLoop = (currentLocalNames?.toSet() ?: emptySet()) + tVar.value
val (canBreak, body, elseStatement) = withLocalNames(namesForLoop) {
val loopParsed = cc.parseLoop {
parseStatement() ?: throw ScriptError(start, "Bad for statement: expected loop body")
}
// possible else clause
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
val elseStmt = if (cc.next().let { it.type == Token.Type.ID && it.value == "else" }) {
parseStatement()
} else {
cc.previous()
null
}
Triple(loopParsed.first, loopParsed.second, elseStmt)
}
// possible else clause
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
val elseStatement = if (cc.next().let { it.type == Token.Type.ID && it.value == "else" }) {
parseStatement()
} else {
cc.previous()
null
}
return statement(body.pos) { cxt ->
val forContext = cxt.createChildScope(start)
@@ -1258,7 +1272,7 @@ class Compiler(
// insofar we suggest source object is enumerable. Later we might need to add checks
val sourceObj = source.execute(forContext)
if (sourceObj is ObjRange && sourceObj.isIntRange) {
if (sourceObj is ObjRange && sourceObj.isIntRange && PerfFlags.PRIMITIVE_FASTOPS) {
loopIntRange(
forContext,
sourceObj.start!!.toLong(),
@@ -1631,11 +1645,15 @@ class Compiler(
val paramNames: Set<String> = argsDeclaration.params.map { it.name }.toSet()
// Here we should be at open body
// Parse function body while tracking declared locals to compute precise capacity hints
val fnLocalDeclStart = currentLocalDeclCount
localDeclCountStack.add(0)
val fnStatements = if (isExtern)
statement { raiseError("extern function not provided: $name") }
else
withLocalNames(paramNames) { parseBlock() }
// Capture and pop the local declarations count for this function
val fnLocalDecls = localDeclCountStack.removeLastOrNull() ?: 0
var closure: Scope? = null
@@ -1648,6 +1666,10 @@ class Compiler(
val context = closure?.let { ClosureScope(callerContext, it) }
?: callerContext
// Capacity hint: parameters + declared locals + small overhead
val capacityHint = paramNames.size + fnLocalDecls + 4
context.hintLocalCapacity(capacityHint)
// load params from caller context
argsDeclaration.assignToContext(context, callerContext.args, defaultAccessType = AccessType.Val)
if (extTypeName != null) {
@@ -20,4 +20,7 @@ expect object PerfDefaults {
val PRIMITIVE_FASTOPS: Boolean
val RVAL_FASTPATH: Boolean
// Regex caching (JVM-first): small LRU for compiled patterns
val REGEX_CACHE: Boolean
}
@@ -30,4 +30,7 @@ object PerfFlags {
// Step 4: R-value fast path to bypass ObjRecord in pure expression evaluation
var RVAL_FASTPATH: Boolean = PerfDefaults.RVAL_FASTPATH
// Regex: enable small LRU cache for compiled patterns (JVM-first usage)
var REGEX_CACHE: Boolean = PerfDefaults.REGEX_CACHE
}
@@ -0,0 +1,31 @@
package net.sergeych.lyng
/**
* Tiny, size-bounded cache for compiled Regex patterns. Activated only when [PerfFlags.REGEX_CACHE] is true.
* This is a very simple FIFO-ish cache sufficient for micro-benchmarks and common repeated patterns.
* Not thread-safe by design; the interpreter typically runs scripts on confined executors.
*/
object RegexCache {
private const val MAX = 64
private val map: MutableMap<String, Regex> = LinkedHashMap()
fun get(pattern: String): Regex {
// Fast path: return cached instance if present
map[pattern]?.let { return it }
// Compile new pattern
val re = pattern.toRegex()
// Keep the cache size bounded
if (map.size >= MAX) {
// Remove the oldest inserted entry (first key in iteration order)
val it = map.keys.iterator()
if (it.hasNext()) {
val k = it.next()
it.remove()
}
}
map[pattern] = re
return re
}
fun clear() = map.clear()
}
@@ -63,6 +63,14 @@ open class Scope(
(slots as? ArrayList<ObjRecord>)?.ensureCapacity(expected)
// nameToSlot has no portable ensureCapacity across KMP; leave it to grow as needed.
}
/**
* Hint expected number of local variables/arguments to reduce internal reallocations.
* Safe no-op for small or unknown values.
*/
fun hintLocalCapacity(expected: Int) {
reserveLocalCapacity(expected)
}
open val packageName: String = "<anonymous package>"
fun slotCount(): Int = slots.size
@@ -38,8 +38,8 @@ open class ObjDeferred(val deferred: Deferred<Obj>): Obj() {
}
addFn("isActive") {
val d = thisAs<ObjDeferred>().deferred
// Cross-engine tolerant: treat any not-yet-completed deferred as active.
(!d.isCompleted).toObj()
// Cross-engine tolerant: prefer Deferred.isActive; otherwise treat any not-yet-completed and not-cancelled as active
(d.isActive || (!d.isCompleted && !d.isCancelled)).toObj()
}
addFn("isCancelled") {
thisAs<ObjDeferred>().deferred.isCancelled.toObj()
@@ -118,6 +118,19 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
}
override suspend fun contains(scope: Scope, other: Obj): Boolean {
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS) {
// Fast path: int membership in a list of ints (common case in benches)
if (other is ObjInt) {
var i = 0
val sz = list.size
while (i < sz) {
val v = list[i]
if (v is ObjInt && v.value == other.value) return true
i++
}
return false
}
}
return list.contains(other)
}
@@ -273,6 +286,115 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
thisAs<ObjList>().list.shuffle()
ObjVoid
}
addFn("sum") {
val self = thisAs<ObjList>()
val l = self.list
if (l.isEmpty()) return@addFn ObjNull
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS) {
// Fast path: all ints → accumulate as long
var i = 0
var acc: Long = 0
while (i < l.size) {
val v = l[i]
if (v is ObjInt) {
acc += v.value
i++
} else {
// Fallback to generic dynamic '+' accumulation starting from current acc
var res: Obj = ObjInt(acc)
while (i < l.size) {
res = res.plus(this, l[i])
i++
}
return@addFn res
}
}
return@addFn ObjInt(acc)
}
// Generic path: dynamic '+' starting from first element
var res: Obj = l[0]
var k = 1
while (k < l.size) {
res = res.plus(this, l[k])
k++
}
res
}
addFn("min") {
val l = thisAs<ObjList>().list
if (l.isEmpty()) return@addFn ObjNull
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS) {
var i = 0
var hasOnlyInts = true
var minVal: Long = Long.MAX_VALUE
while (i < l.size) {
val v = l[i]
if (v is ObjInt) {
if (v.value < minVal) minVal = v.value
} else {
hasOnlyInts = false
break
}
i++
}
if (hasOnlyInts) return@addFn ObjInt(minVal)
}
var res: Obj = l[0]
var i = 1
while (i < l.size) {
val v = l[i]
if (v.compareTo(this, res) < 0) res = v
i++
}
res
}
addFn("max") {
val l = thisAs<ObjList>().list
if (l.isEmpty()) return@addFn ObjNull
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS) {
var i = 0
var hasOnlyInts = true
var maxVal: Long = Long.MIN_VALUE
while (i < l.size) {
val v = l[i]
if (v is ObjInt) {
if (v.value > maxVal) maxVal = v.value
} else {
hasOnlyInts = false
break
}
i++
}
if (hasOnlyInts) return@addFn ObjInt(maxVal)
}
var res: Obj = l[0]
var i = 1
while (i < l.size) {
val v = l[i]
if (v.compareTo(this, res) > 0) res = v
i++
}
res
}
addFn("indexOf") {
val l = thisAs<ObjList>().list
val needle = args.firstAndOnly()
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS && needle is ObjInt) {
var i = 0
while (i < l.size) {
val v = l[i]
if (v is ObjInt && v.value == needle.value) return@addFn ObjInt(i.toLong())
i++
}
return@addFn ObjInt((-1).toLong())
}
var i = 0
while (i < l.size) {
if (l[i].compareTo(this, needle) == 0) return@addFn ObjInt(i.toLong())
i++
}
ObjInt((-1).toLong())
}
}
}
}
@@ -231,10 +231,13 @@ class LogicalOrRef(private val left: ObjRef, private val right: ObjRef) : ObjRef
/** Logical AND with short-circuit: a && b */
class LogicalAndRef(private val left: ObjRef, private val right: ObjRef) : ObjRef {
override suspend fun get(scope: Scope): ObjRecord {
val a = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) left.evalValue(scope) else left.get(scope).value
// Hoist flags to locals for JIT friendliness
val fastRval = net.sergeych.lyng.PerfFlags.RVAL_FASTPATH
val fastPrim = net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS
val a = if (fastRval) left.evalValue(scope) else left.get(scope).value
if ((a as? ObjBool)?.value == false) return ObjFalse.asReadonly
val b = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) right.evalValue(scope) else right.get(scope).value
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS) {
val b = if (fastRval) right.evalValue(scope) else right.get(scope).value
if (fastPrim) {
if (a is ObjBool && b is ObjBool) {
return if (a.value && b.value) ObjTrue.asReadonly else ObjFalse.asReadonly
}
@@ -269,12 +272,15 @@ class FieldRef(
private var tKey: Long = 0L; private var tVer: Int = -1; private var tFrameId: Long = -1L; private var tRecord: ObjRecord? = null
override suspend fun get(scope: Scope): ObjRecord {
val base = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) target.evalValue(scope) else target.get(scope).value
val fastRval = net.sergeych.lyng.PerfFlags.RVAL_FASTPATH
val fieldPic = net.sergeych.lyng.PerfFlags.FIELD_PIC
val picCounters = net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS
val base = if (fastRval) target.evalValue(scope) else target.get(scope).value
if (base == ObjNull && isOptional) return ObjNull.asMutable
if (net.sergeych.lyng.PerfFlags.FIELD_PIC) {
if (fieldPic) {
val (key, ver) = receiverKeyAndVersion(base)
rGetter1?.let { g -> if (key == rKey1 && ver == rVer1) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.fieldPicHit++
if (picCounters) net.sergeych.lyng.PerfStats.fieldPicHit++
val rec0 = g(base, scope)
if (base is ObjClass) {
val idx0 = base.classScope?.getSlotIndexOf(name)
@@ -283,7 +289,7 @@ class FieldRef(
return rec0
} }
rGetter2?.let { g -> if (key == rKey2 && ver == rVer2) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.fieldPicHit++
if (picCounters) net.sergeych.lyng.PerfStats.fieldPicHit++
val rec0 = g(base, scope)
if (base is ObjClass) {
val idx0 = base.classScope?.getSlotIndexOf(name)
@@ -292,7 +298,7 @@ class FieldRef(
return rec0
} }
// Slow path
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.fieldPicMiss++
if (picCounters) net.sergeych.lyng.PerfStats.fieldPicMiss++
val rec = base.readField(scope, name)
// Install move-to-front with a handle-aware getter. Where safe, capture resolved handles.
rKey2 = rKey1; rVer2 = rVer1; rGetter2 = rGetter1
@@ -323,23 +329,25 @@ class FieldRef(
}
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
val fieldPic = net.sergeych.lyng.PerfFlags.FIELD_PIC
val picCounters = net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS
val base = target.get(scope).value
if (base == ObjNull && isOptional) {
// no-op on null receiver for optional chaining assignment
return
}
if (net.sergeych.lyng.PerfFlags.FIELD_PIC) {
if (fieldPic) {
val (key, ver) = receiverKeyAndVersion(base)
wSetter1?.let { s -> if (key == wKey1 && ver == wVer1) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.fieldPicSetHit++
if (picCounters) net.sergeych.lyng.PerfStats.fieldPicSetHit++
return s(base, scope, newValue)
} }
wSetter2?.let { s -> if (key == wKey2 && ver == wVer2) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.fieldPicSetHit++
if (picCounters) net.sergeych.lyng.PerfStats.fieldPicSetHit++
return s(base, scope, newValue)
} }
// Slow path
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.fieldPicSetMiss++
if (picCounters) net.sergeych.lyng.PerfStats.fieldPicSetMiss++
base.writeField(scope, name, newValue)
// Install move-to-front with a handle-aware setter
wKey2 = wKey1; wVer2 = wVer1; wSetter2 = wSetter1
@@ -385,9 +393,18 @@ class IndexRef(
private val isOptional: Boolean,
) : ObjRef {
override suspend fun get(scope: Scope): ObjRecord {
val base = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) target.evalValue(scope) else target.get(scope).value
val fastRval = net.sergeych.lyng.PerfFlags.RVAL_FASTPATH
val base = if (fastRval) target.evalValue(scope) else target.get(scope).value
if (base == ObjNull && isOptional) return ObjNull.asMutable
val idx = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) index.evalValue(scope) else index.get(scope).value
val idx = if (fastRval) index.evalValue(scope) else index.get(scope).value
if (fastRval) {
// Primitive list index fast path: avoid virtual dispatch to getAt when shapes match
if (base is ObjList && idx is ObjInt) {
val i = idx.toInt()
// Bounds checks are enforced by the underlying list access; exceptions propagate as before
return base.list[i].asMutable
}
}
return base.getAt(scope, idx).asMutable
}
@@ -419,10 +436,12 @@ class CallRef(
private val isOptionalInvoke: Boolean,
) : ObjRef {
override suspend fun get(scope: Scope): ObjRecord {
val callee = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) target.evalValue(scope) else target.get(scope).value
val fastRval = net.sergeych.lyng.PerfFlags.RVAL_FASTPATH
val usePool = net.sergeych.lyng.PerfFlags.SCOPE_POOL
val callee = if (fastRval) target.evalValue(scope) else target.get(scope).value
if (callee == ObjNull && isOptionalInvoke) return ObjNull.asReadonly
val callArgs = args.toArguments(scope, tailBlock)
val result: Obj = if (net.sergeych.lyng.PerfFlags.SCOPE_POOL) {
val result: Obj = if (usePool) {
scope.withChildFrame(callArgs) { child ->
callee.callOn(child)
}
@@ -450,21 +469,24 @@ class MethodCallRef(
private var mKey4: Long = 0L; private var mVer4: Int = -1; private var mInvoker4: (suspend (Obj, Scope, Arguments) -> Obj)? = null
override suspend fun get(scope: Scope): ObjRecord {
val base = if (net.sergeych.lyng.PerfFlags.RVAL_FASTPATH) receiver.evalValue(scope) else receiver.get(scope).value
val fastRval = net.sergeych.lyng.PerfFlags.RVAL_FASTPATH
val methodPic = net.sergeych.lyng.PerfFlags.METHOD_PIC
val picCounters = net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS
val base = if (fastRval) receiver.evalValue(scope) else receiver.get(scope).value
if (base == ObjNull && isOptional) return ObjNull.asReadonly
val callArgs = args.toArguments(scope, tailBlock)
if (net.sergeych.lyng.PerfFlags.METHOD_PIC) {
if (methodPic) {
val (key, ver) = receiverKeyAndVersion(base)
mInvoker1?.let { inv -> if (key == mKey1 && ver == mVer1) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.methodPicHit++
if (picCounters) net.sergeych.lyng.PerfStats.methodPicHit++
return inv(base, scope, callArgs).asReadonly
} }
mInvoker2?.let { inv -> if (key == mKey2 && ver == mVer2) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.methodPicHit++
if (picCounters) net.sergeych.lyng.PerfStats.methodPicHit++
return inv(base, scope, callArgs).asReadonly
} }
mInvoker3?.let { inv -> if (key == mKey3 && ver == mVer3) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.methodPicHit++
if (picCounters) net.sergeych.lyng.PerfStats.methodPicHit++
// move-to-front: promote 3→1
val tK = mKey3; val tV = mVer3; val tI = mInvoker3
mKey3 = mKey2; mVer3 = mVer2; mInvoker3 = mInvoker2
@@ -473,7 +495,7 @@ class MethodCallRef(
return inv(base, scope, callArgs).asReadonly
} }
mInvoker4?.let { inv -> if (key == mKey4 && ver == mVer4) {
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.methodPicHit++
if (picCounters) net.sergeych.lyng.PerfStats.methodPicHit++
// move-to-front: promote 4→1
val tK = mKey4; val tV = mVer4; val tI = mInvoker4
mKey4 = mKey3; mVer4 = mVer3; mInvoker4 = mInvoker3
@@ -483,7 +505,7 @@ class MethodCallRef(
return inv(base, scope, callArgs).asReadonly
} }
// Slow path
if (net.sergeych.lyng.PerfFlags.PIC_DEBUG_COUNTERS) net.sergeych.lyng.PerfStats.methodPicMiss++
if (picCounters) net.sergeych.lyng.PerfStats.methodPicMiss++
val result = base.invokeInstanceMethod(scope, name, callArgs)
// Install move-to-front with a handle-aware invoker: shift 1→2→3→4, put new at 1
mKey4 = mKey3; mVer4 = mVer3; mInvoker4 = mInvoker3
@@ -17,6 +17,8 @@
package net.sergeych.lyng.obj
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.RegexCache
import net.sergeych.lyng.Scope
class ObjRegex(val regex: Regex) : Obj() {
@@ -36,9 +38,9 @@ class ObjRegex(val regex: Regex) : Obj() {
val type by lazy {
object : ObjClass("Regex") {
override suspend fun callOn(scope: Scope): Obj {
return ObjRegex(
scope.requireOnlyArg<ObjString>().value.toRegex()
)
val pattern = scope.requireOnlyArg<ObjString>().value
val re = if (PerfFlags.REGEX_CACHE) RegexCache.get(pattern) else pattern.toRegex()
return ObjRegex(re)
}
}.apply {
addFn("matches") {
@@ -19,6 +19,8 @@ package net.sergeych.lyng.obj
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.sergeych.lyng.PerfFlags
import net.sergeych.lyng.RegexCache
import net.sergeych.lyng.Scope
import net.sergeych.lyng.statement
import net.sergeych.lynon.LynonDecoder
@@ -182,7 +184,7 @@ data class ObjString(val value: String) : Obj() {
is ObjString -> {
if (s.value == ".*") true
else {
val re = s.value.toRegex()
val re = if (PerfFlags.REGEX_CACHE) RegexCache.get(s.value) else s.value.toRegex()
self.matches(re)
}
}