Remove use of ArrayIterator in runtime hot paths
Fixes: b/181883246
Test: ./gradlew :compose:r:r:tDUT :compose:r:r:i-t:cC
Change-Id: I7d593f92b0a9b63379b9e1cf014066a573e35bd8
diff --git a/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Composer.kt b/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Composer.kt
index f886ebb..9a91f829 100644
--- a/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Composer.kt
+++ b/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Composer.kt
@@ -21,6 +21,8 @@
package androidx.compose.runtime
import androidx.compose.runtime.collection.IdentityScopeMap
+import androidx.compose.runtime.snapshots.fastForEach
+import androidx.compose.runtime.snapshots.fastToSet
import androidx.compose.runtime.tooling.LocalInspectionTables
import androidx.compose.runtime.tooling.CompositionData
import kotlinx.collections.immutable.PersistentMap
@@ -1331,7 +1333,8 @@
fun dispatchRememberObservers() {
// Send forgets
if (forgetting.isNotEmpty()) {
- for (instance in forgetting.reversed()) {
+ for (index in forgetting.indices.reversed()) {
+ val instance = forgetting[index]
if (instance !in abandoning)
instance.onForgotten()
}
@@ -1339,7 +1342,7 @@
// Send remembers
if (remembering.isNotEmpty()) {
- for (instance in remembering) {
+ remembering.fastForEach { instance ->
abandoning.remove(instance)
instance.onRemembered()
}
@@ -1383,7 +1386,7 @@
// Apply all changes
slotTable.write { slots ->
val applier = applier
- changes.forEach { change ->
+ changes.fastForEach { change ->
change(applier, slots, manager)
}
changes.clear()
@@ -2075,7 +2078,7 @@
// usedKeys contains the keys that were used in the new composition, therefore if a key
// doesn't exist in this set, it needs to be removed.
- val usedKeys = current.toSet()
+ val usedKeys = current.fastToSet()
val placedKeys = mutableSetOf<KeyInfo>()
var currentIndex = 0
@@ -2803,7 +2806,7 @@
val insertTable = insertTable
recordSlotEditingOperation { applier, slots, rememberManager ->
insertTable.write { writer ->
- for (fixup in fixups) {
+ fixups.fastForEach { fixup ->
fixup(applier, writer, rememberManager)
}
}
diff --git a/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/ListUtils.kt b/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/ListUtils.kt
index 89aa1ecc..ddd647c 100644
--- a/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/ListUtils.kt
+++ b/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/ListUtils.kt
@@ -21,3 +21,7 @@
block(this[index])
}
}
+
+internal fun <T> List<T>.fastToSet(): Set<T> = HashSet<T>(size).also { set ->
+ fastForEach { item -> set.add(item) }
+}
\ No newline at end of file
diff --git a/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/Snapshot.kt b/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/Snapshot.kt
index 4ae8e9c..2f73c00 100644
--- a/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/Snapshot.kt
+++ b/compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/Snapshot.kt
@@ -1427,7 +1427,7 @@
val modified = previousGlobalSnapshot.modified
if (modified != null) {
val observers = sync { applyObservers.toList() }
- for (observer in observers) {
+ observers.fastForEach { observer ->
observer(modified, previousGlobalSnapshot)
}
}