Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa8785aa authored by Steve Elliott's avatar Steve Elliott
Browse files

[kairos] re-use defer scope to avoid reallocs

Flag: com.android.systemui.status_bar_mobile_icon_kairos
Bug: 383172066
Test: atest
Change-Id: Ie4f935884ecb6179a6c5cd6abf64890b9517afae
parent a547cf76
Loading
Loading
Loading
Loading
+14 −18
Original line number Diff line number Diff line
@@ -22,10 +22,8 @@ internal interface DeferScope {
    fun <R> deferAsync(block: () -> R): Lazy<R>
}

internal inline fun <A> deferScope(block: DeferScope.() -> A): A {
    val scope =
        object : DeferScope {
            val deferrals = ArrayDeque<() -> Unit>() // TODO: store lazies instead?
internal class DeferScopeImpl : DeferScope {
    val deferrals = ArrayDeque<() -> Unit>()

    fun drainDeferrals() {
        while (deferrals.isNotEmpty()) {
@@ -40,10 +38,6 @@ internal inline fun <A> deferScope(block: DeferScope.() -> A): A {
    override fun <R> deferAsync(block: () -> R): Lazy<R> =
        lazy(block).also { deferrals.add { it.value } }
}
    return scope.block().also { scope.drainDeferrals() }
}

internal object NoValue

internal class CompletableLazy<T>(
    private var _value: Any? = NoValue,
@@ -63,4 +57,6 @@ internal class CompletableLazy<T>(
        }

    override fun isInitialized(): Boolean = _value !== NoValue

    private object NoValue
}
+5 −3
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ internal class Network(val coroutineScope: CoroutineScope, val coalescingPolicy:
    }
    override val transactionStore = TransactionStore()

    private val deferScopeImpl = DeferScopeImpl()
    private val stateWrites = ArrayDeque<StateSource<*>>()
    private val fastOutputs = ArrayDeque<Output<*>>()
    private val outputsByDispatcher =
@@ -182,8 +183,9 @@ internal class Network(val coroutineScope: CoroutineScope, val coalescingPolicy:
            onResult.invokeOnCompletion { job.cancel() }
        }

    inline fun <R> evalScope(block: EvalScope.() -> R): R = deferScope {
        block(EvalScopeImpl(this@Network, this))
    inline fun <R> evalScope(block: EvalScope.() -> R): R =
        block(EvalScopeImpl(networkScope = this, deferScope = deferScopeImpl)).also {
            deferScopeImpl.drainDeferrals()
        }

    /** Performs a transactional update of the Kairos network. */