Skip to content

Commit 69e5970

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "Use forAllData instead of forEachData for deactivating" into androidx-main
2 parents c192132 + 8291149 commit 69e5970

File tree

2 files changed

+35
-42
lines changed

2 files changed

+35
-42
lines changed

compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Composer.kt

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,45 +4202,40 @@ internal fun SlotWriter.deactivateCurrentGroup(rememberManager: RememberManager)
42024202

42034203
// To ensure this order, we call `enters` as a pre-order traversal
42044204
// of the group tree, and then call `leaves` in the inverse order.
4205-
val start = currentGroup
4206-
val end = currentGroupEnd
4207-
for (group in start until end) {
4208-
val node = node(group)
4209-
if (node is ComposeNodeLifecycleCallback) {
4210-
val endRelativeOrder = slotsSize - slotsStartIndex(group)
4211-
rememberManager.deactivating(node, endRelativeOrder, -1, -1)
4212-
}
4213-
4214-
forEachData(group) { slotIndex, data ->
4215-
when (data) {
4216-
is RememberObserverHolder -> {
4217-
val wrapped = data.wrapped
4218-
if (wrapped is ReusableRememberObserver) {
4219-
// do nothing, the value should be preserved on reuse
4220-
} else {
4221-
removeData(group, slotIndex, data)
4222-
val endRelativeOrder = slotsSize - slotIndex
4223-
withAfterAnchorInfo(data.after) { priority, endRelativeAfter ->
4224-
rememberManager.forgetting(
4225-
wrapped,
4226-
endRelativeOrder,
4227-
priority,
4228-
endRelativeAfter
4229-
)
4230-
}
4205+
forAllData(currentGroup) { slotIndex, data ->
4206+
when (data) {
4207+
is ComposeNodeLifecycleCallback -> {
4208+
val endRelativeOrder = slotsSize - slotIndex
4209+
rememberManager.deactivating(data, endRelativeOrder, -1, -1)
4210+
}
4211+
is RememberObserverHolder -> {
4212+
val wrapped = data.wrapped
4213+
if (wrapped is ReusableRememberObserver) {
4214+
// do nothing, the value should be preserved on reuse
4215+
} else {
4216+
removeData(slotIndex, data)
4217+
val endRelativeOrder = slotsSize - slotIndex
4218+
withAfterAnchorInfo(data.after) { priority, endRelativeAfter ->
4219+
rememberManager.forgetting(
4220+
wrapped,
4221+
endRelativeOrder,
4222+
priority,
4223+
endRelativeAfter
4224+
)
42314225
}
42324226
}
4233-
is RecomposeScopeImpl -> {
4234-
removeData(group, slotIndex, data)
4235-
data.release()
4236-
}
4227+
}
4228+
is RecomposeScopeImpl -> {
4229+
removeData(slotIndex, data)
4230+
data.release()
42374231
}
42384232
}
42394233
}
42404234
}
42414235

4242-
private fun SlotWriter.removeData(group: Int, index: Int, data: Any?) {
4243-
runtimeCheck(data === set(group, index, Composer.Empty)) { "Slot table is out of sync" }
4236+
private fun SlotWriter.removeData(index: Int, data: Any?) {
4237+
val result = clear(index)
4238+
runtimeCheck(data === result) { "Slot table is out of sync (expected $data, got $result)" }
42444239
}
42454240

42464241
@JvmInline

compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/SlotTable.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,14 @@ internal class SlotWriter(
16241624
return result
16251625
}
16261626

1627+
/** Set the slot by index to Composer.Empty, returning previous value */
1628+
fun clear(slotIndex: Int): Any? {
1629+
val address = dataIndexToDataAddress(slotIndex)
1630+
val result = slots[address]
1631+
slots[address] = Composer.Empty
1632+
return result
1633+
}
1634+
16271635
/**
16281636
* Skip the current slot without updating. If the slot table is inserting then and
16291637
* [Composer.Empty] slot is added and [skip] return [Composer.Empty].
@@ -2023,16 +2031,6 @@ internal class SlotWriter(
20232031
}
20242032
}
20252033

2026-
inline fun forEachData(group: Int, block: (index: Int, data: Any?) -> Unit) {
2027-
val address = groupIndexToAddress(group)
2028-
val slotsStart = groups.slotIndex(address)
2029-
val slotsEnd = groups.dataIndex(groupIndexToAddress(group + 1))
2030-
2031-
for (slot in slotsStart until slotsEnd) {
2032-
block(slot - slotsStart, slots[dataIndexToDataAddress(slot)])
2033-
}
2034-
}
2035-
20362034
inline fun forAllData(group: Int, block: (index: Int, data: Any?) -> Unit) {
20372035
val address = groupIndexToAddress(group)
20382036
val start = groups.dataIndex(address)

0 commit comments

Comments
 (0)