Enable SCOPE_POOL globally across all platforms and refactor pooling logic to enhance robustness, efficiency, and cleanup mechanisms. Update documentation to reflect changes.

This commit is contained in:
sergeych committed 2026-01-12 06:16:22 +01:00
1 parent 8f04b25fcb
commit f6deabaa38
13 files changed
+101 -74

No files matched your search

@@ -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.
@@ -23,7 +23,7 @@ actual object PerfDefaults {
actual val ARG_BUILDER: Boolean = true
actual val SKIP_ARGS_ON_NULL_RECEIVER: Boolean = true
actual val SCOPE_POOL: Boolean = false
actual val SCOPE_POOL: Boolean = true
actual val FIELD_PIC: Boolean = true
actual val METHOD_PIC: Boolean = true
@@ -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.
@@ -18,7 +18,6 @@
package net.sergeych.lyng
import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjVoid
/**
* Android actual: per-thread scope frame pool backed by ThreadLocal.
@@ -38,24 +37,31 @@ actual object ScopePool {
actual fun borrow(parent: Scope, args: Arguments, pos: Pos, thisObj: Obj): Scope {
val pool = pool()
val s = if (pool.isNotEmpty()) pool.removeLast() else Scope(parent, args, pos, thisObj)
return try {
if (s.parent !== parent || s.args !== args || s.pos !== pos || s.thisObj !== thisObj) {
if (pool.isNotEmpty()) {
val s = pool.removeLast()
try {
// Re-initialize pooled instance
s.resetForReuse(parent, args, pos, thisObj)
} else {
s.frameId = nextFrameId()
return s
} catch (e: IllegalStateException) {
// Defensive fallback: if a cycle in scope parent chain is detected during reuse,
// discard pooled instance for this call frame and allocate a fresh scope instead.
if (e.message?.contains("cycle") == true && e.message?.contains("scope parent chain") == true) {
return Scope(parent, args, pos, thisObj)
} else {
throw e
}
}
s
} catch (e: IllegalStateException) {
if (e.message?.contains("cycle") == true && e.message?.contains("scope parent chain") == true) {
Scope(parent, args, pos, thisObj)
} else throw e
}
return Scope(parent, args, pos, thisObj)
}
actual fun release(scope: Scope) {
val pool = pool()
scope.resetForReuse(parent = null, args = Arguments.EMPTY, pos = Pos.builtIn, thisObj = ObjVoid)
if (pool.size < MAX_POOL_SIZE) pool.addLast(scope)
if (pool.size < MAX_POOL_SIZE) {
// Scrub sensitive references to avoid accidental retention before returning to pool
scope.scrub()
pool.addLast(scope)
}
}
}