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

Commit 64fde703 authored by Steve Elliott's avatar Steve Elliott
Browse files

[kairos] Remove unnecessary concurrency overhead

removes some volatiles, atomics, and concurrent data structures.

Flag: com.android.systemui.status_bar_mobile_icon_kairos
Bug: 383172066
Test: atest
Change-Id: Ic6bbc97a964bb8a7ff330f2e4f489bc1d4fe4d05
parent 36ed8521
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import com.android.systemui.kairos.internal.constInit
import com.android.systemui.kairos.internal.mapImpl
import com.android.systemui.kairos.internal.mergeNodes
import com.android.systemui.kairos.internal.mergeNodesLeft
import com.android.systemui.kairos.internal.store.ConcurrentHashMapK
import com.android.systemui.kairos.internal.store.HashMapK
import com.android.systemui.kairos.internal.switchDeferredImpl
import com.android.systemui.kairos.internal.switchPromptImpl
import com.android.systemui.kairos.util.map
@@ -205,7 +205,7 @@ fun <K, V> Incremental<K, Events<V>>.mergeEventsIncrementally(): Events<Map<K, V
                            .asIterable()
                    },
                    getPatches = { patches },
                    storeFactory = ConcurrentHashMapK.Factory(),
                    storeFactory = HashMapK.Factory(),
                )
                .awaitValues(),
        )
@@ -250,7 +250,7 @@ fun <K, V> Incremental<K, Events<V>>.mergeEventsIncrementallyPromptly(): Events<
                            .asIterable()
                    },
                    getPatches = { patches },
                    storeFactory = ConcurrentHashMapK.Factory(),
                    storeFactory = HashMapK.Factory(),
                )
                .awaitValues(),
        )
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ internal class DeferScopeImpl : DeferScope {
    }

    override fun <R> deferAsync(block: () -> R): Lazy<R> =
        lazy(block).also { deferrals.add { it.value } }
        lazy(LazyThreadSafetyMode.NONE, block).also { deferrals.add { it.value } }
}

internal class CompletableLazy<T>(
+14 −13
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package com.android.systemui.kairos.internal

import com.android.systemui.kairos.internal.store.ConcurrentHashMapK
import com.android.systemui.kairos.internal.store.HashMapK
import com.android.systemui.kairos.internal.store.MapHolder
import com.android.systemui.kairos.internal.store.MapK
import com.android.systemui.kairos.internal.store.MutableMapK
@@ -33,7 +33,7 @@ internal class DemuxNode<W, K, A>(

    lateinit var upstreamConnection: NodeConnection<MapK<W, K, A>>

    @Volatile private var epoch: Long = Long.MIN_VALUE
    private var epoch: Long = Long.MIN_VALUE

    fun hasCurrentValueLocked(logIndent: Int, evalScope: EvalScope, key: K): Boolean =
        evalScope.epoch == epoch &&
@@ -52,15 +52,16 @@ internal class DemuxNode<W, K, A>(
                    upstreamConnection.getPushEvent(currentLogIndent, evalScope)
                }
            updateEpoch(evalScope)
            for ((key, _) in upstreamResult) {
                if (!branchNodeByKey.contains(key)) continue
            upstreamResult.forEach { key, _ ->
                if (branchNodeByKey.contains(key)) {
                    val branch = branchNodeByKey.getValue(key)
                    branch.schedule(currentLogIndent, evalScope)
                }
            }
        }

    override fun adjustDirectUpstream(scheduler: Scheduler, oldDepth: Int, newDepth: Int) {
        for ((_, branchNode) in branchNodeByKey) {
        branchNodeByKey.forEach { _, branchNode ->
            branchNode.downstreamSet.adjustDirectUpstream(scheduler, oldDepth, newDepth)
        }
    }
@@ -71,7 +72,7 @@ internal class DemuxNode<W, K, A>(
        oldIndirectSet: Set<MuxDeferredNode<*, *, *>>,
        newDirectDepth: Int,
    ) {
        for ((_, branchNode) in branchNodeByKey) {
        branchNodeByKey.forEach { _, branchNode ->
            branchNode.downstreamSet.moveIndirectUpstreamToDirect(
                scheduler,
                oldIndirectDepth,
@@ -88,7 +89,7 @@ internal class DemuxNode<W, K, A>(
        removals: Set<MuxDeferredNode<*, *, *>>,
        additions: Set<MuxDeferredNode<*, *, *>>,
    ) {
        for ((_, branchNode) in branchNodeByKey) {
        branchNodeByKey.forEach { _, branchNode ->
            branchNode.downstreamSet.adjustIndirectUpstream(
                scheduler,
                oldDepth,
@@ -105,7 +106,7 @@ internal class DemuxNode<W, K, A>(
        newIndirectDepth: Int,
        newIndirectSet: Set<MuxDeferredNode<*, *, *>>,
    ) {
        for ((_, branchNode) in branchNodeByKey) {
        branchNodeByKey.forEach { _, branchNode ->
            branchNode.downstreamSet.moveDirectUpstreamToIndirect(
                scheduler,
                oldDirectDepth,
@@ -121,14 +122,14 @@ internal class DemuxNode<W, K, A>(
        indirectSet: Set<MuxDeferredNode<*, *, *>>,
    ) {
        lifecycle.lifecycleState = DemuxLifecycleState.Dead
        for ((_, branchNode) in branchNodeByKey) {
        branchNodeByKey.forEach { _, branchNode ->
            branchNode.downstreamSet.removeIndirectUpstream(scheduler, depth, indirectSet)
        }
    }

    override fun removeDirectUpstream(scheduler: Scheduler, depth: Int) {
        lifecycle.lifecycleState = DemuxLifecycleState.Dead
        for ((_, branchNode) in branchNodeByKey) {
        branchNodeByKey.forEach { _, branchNode ->
            branchNode.downstreamSet.removeDirectUpstream(scheduler, depth)
        }
    }
@@ -217,7 +218,7 @@ internal fun <K, A> demuxMap(
    upstream: EvalScope.() -> EventsImpl<Map<K, A>>,
    numKeys: Int?,
): DemuxImpl<K, A> =
    DemuxImpl(mapImpl(upstream) { it, _ -> MapHolder(it) }, numKeys, ConcurrentHashMapK.Factory())
    DemuxImpl(mapImpl(upstream) { it, _ -> MapHolder(it) }, numKeys, HashMapK.Factory())

internal class DemuxActivator<W, K, A>(
    private val numKeys: Int?,
+21 −13
Original line number Diff line number Diff line
@@ -61,14 +61,14 @@ import java.util.TreeMap
 */
internal class DepthTracker {

    @Volatile var snapshotIsDirect = true
    @Volatile private var snapshotIsIndirectRoot = false
    var snapshotIsDirect = true
    private var snapshotIsIndirectRoot = false

    private inline val snapshotIsIndirect: Boolean
        get() = !snapshotIsDirect

    @Volatile var snapshotIndirectDepth: Int = 0
    @Volatile var snapshotDirectDepth: Int = 0
    var snapshotIndirectDepth: Int = 0
    var snapshotDirectDepth: Int = 0

    private val _snapshotIndirectRoots = HashSet<MuxDeferredNode<*, *, *>>()
    val snapshotIndirectRoots
@@ -79,10 +79,10 @@ internal class DepthTracker {
    private val dirty_directUpstreamDepths = TreeMap<Int, Int>()
    private val dirty_indirectUpstreamDepths = TreeMap<Int, Int>()
    private val dirty_indirectUpstreamRoots = Bag<MuxDeferredNode<*, *, *>>()
    @Volatile var dirty_directDepth = 0
    @Volatile private var dirty_indirectDepth = 0
    @Volatile private var dirty_depthIsDirect = true
    @Volatile private var dirty_isIndirectRoot = false
    var dirty_directDepth = 0
    private var dirty_indirectDepth = 0
    private var dirty_depthIsDirect = true
    private var dirty_isIndirectRoot = false

    fun schedule(scheduler: Scheduler, node: MuxNode<*, *, *>) {
        if (dirty_depthIsDirect) {
@@ -182,7 +182,7 @@ internal class DepthTracker {
        return (!dirty_depthIsDirect && (addsChanged || removalsChanged))
    }

    private fun <T> HashSet<T>.applyRemovalDiff(changeSet: Set<T>): Set<T> {
    private fun <T> HashSet<T>.applyRemovalDiff(changeSet: HashSet<T>): Set<T> {
        val remainder = HashSet<T>()
        for (element in changeSet) {
            if (!remove(element)) {
@@ -479,9 +479,17 @@ internal fun scheduleAll(
    downstreamSet: DownstreamSet,
    evalScope: EvalScope,
): Boolean {
    downstreamSet.nodes.forEach { it.schedule(logIndent, evalScope) }
    downstreamSet.muxMovers.forEach { it.scheduleMover(logIndent, evalScope) }
    downstreamSet.outputs.forEach { it.schedule(logIndent, evalScope) }
    downstreamSet.stateWriters.forEach { evalScope.schedule(it) }
    for (node in downstreamSet.nodes) {
        node.schedule(logIndent, evalScope)
    }
    for (mover in downstreamSet.muxMovers) {
        mover.scheduleMover(logIndent, evalScope)
    }
    for (output in downstreamSet.outputs) {
        output.schedule(logIndent, evalScope)
    }
    for (idx in downstreamSet.stateWriters.indices) {
        evalScope.schedule(downstreamSet.stateWriters[idx])
    }
    return downstreamSet.isNotEmpty()
}
+2 −2
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ private fun <K, V> Map<K, V>.applyPatchCalm(
    val current = this
    val filteredPatch = mutableMapOf<K, Maybe<V>>()
    val new = current.toMutableMap()
    for ((key, change) in patch) {
    patch.forEach { key, change ->
        when (change) {
            is Maybe.Present -> {
                if (key !in current || current.getValue(key) != change.value) {
@@ -93,7 +93,7 @@ private fun <K, V> Map<K, V>.applyPatchCalm(
    return if (filteredPatch.isNotEmpty()) filteredPatch to new else null
}

internal inline fun <K, V> EventsImpl<Map<K, Maybe<V>>>.calmUpdates(
internal fun <K, V> EventsImpl<Map<K, Maybe<V>>>.calmUpdates(
    state: StateDerived<Map<K, V>>
): Pair<EventsImpl<Map<K, Maybe<V>>>, EventsImpl<Map<K, V>>> {
    val maybeUpdate =
Loading