Pantrix UI

A small semantics tree for iOS

Why, in Compose Multiplatform, a screen of many rows stalls on iOS while its accessibility tree is read, as it scrolls and as a row opens and closes, how the tree is kept small, and how it is measured. The problem is not Pantrix's own: it shows the same way on iOS in every Compose app that composes hundreds of semantics nodes on one screen at once.

Which of what is said here was seen on the simulator, which was read in the source of Compose Multiplatform 1.12.2, and which was not verified is written apart in section 6.

1. The problem

The symptom

The cause

On iOS Compose turns the semantics tree into UIKit's accessibility elements and keeps them up to date (AccessibilityMediator):

In Pantrix's demo the Data Table page, sixteen tables in one column, held 1585 nodes; the other pages between 107 and 205. On an iPhone simulator, in a Debug build, while its tree was read, that page stalled for about 400 ms every half second.

2. The solution

The tree is kept small. There are three ways, from the largest effect down:

WayWhat is doneWhat it gains
Composing only what is seenLong content is a LazyColumn or a LazyVerticalGrid, not a ColumnOnly the rows on the screen are in the tree; whatever the screen's length, the count of nodes is what the screen holds
Merging what is one thingA row that says one thing, a name and its value, a cell, takes Modifier.semantics(mergeDescendants = true) {}iOS makes no element of its own for the texts inside a merging node; a screen reader reads it as one piece too
Leaving out what is not to be readA shape that is only decoration, an icon beside a text that means the same, takes Modifier.clearAndSetSemantics {}That node and what is under it leave the tree

Four details:

DetailWhat is done
1Merging does not stop the walk: iOS goes through the nodes under a merging node too, it only makes no elements of themThe largest gain is from the lazy list; merging comes on top of it
2iOS builds a merged node's label from five texts at the most; a sixth is not in the labelWhat is short is merged: a line, a cell. Not the whole of a card of many texts
3A test tag inside a merging node is not in the merged treeA test finds it with useUnmergedTree = true
4In a lazy list a row that leaves the screen leaves the composition tooWhat the row keeps with rememberSaveable comes back; what it keeps with plain remember starts again. So that a row that takes the focus does not stay behind a bar, the rows say the list's room (the scroll-padding guide)

3. With Pantrix

3.1. A list's page

A long page is a PantrixLazyPageScaffold: its heading and its header are the list's first rows, and its rows are composed as they come into view. The content takes the list's room as contentPadding (LazyPageScope); each row says it, so that a row that takes the focus by the keyboard does not stay behind a bar at the bottom:

PantrixLazyPageScaffold(title = "Orders") {
    val shown = Modifier.scrollPadding(contentPadding)
    items(orders, key = { it.id }) { order -> OrderRow(order, shown) }
}

Where a page builds its own list, PantrixPageScaffold(scroll = PageScroll.None) gives the same room as contentPadding; the rows say it the same way.

3.2. Data table

PantrixDataTable has a cell read as one node; in the list a table becomes on a narrow screen, each line of a record too, the column's name with its value. What a cell holds to press, a button, is read on its own. A table whose row is pressed (onRowClick) already reads the row as one button. The merged tree of the sixteen tables in Pantrix's demo:

WindowBeforeAfter
Phone (430 dp)1427 nodes905 nodes
Wide (1200 dp)1676 nodes1136 nodes

On iOS, while the tree is read, a pass of the sync on the demo's lazy page went from 159 ms down to 118 ms with it (section 6). The test tag of something put inside a cell is found with useUnmergedTree = true.

4. Without Pantrix

The same two steps in any Compose project:

@Composable
fun OrderLine(name: String, value: String) {
    // A name and its value: one thing for a screen reader, one element for iOS.
    Row(Modifier.semantics(mergeDescendants = true) {}) {
        Text(name)
        Text(value)
    }
}

@Composable
fun OrdersScreen(orders: List<Order>) {
    // Only the orders on the screen are composed.
    LazyColumn {
        items(orders, key = { it.id }) { order -> OrderCard(order) }
    }
}

5. Measuring

5.1. Counting the nodes in a test

A screen's count of nodes is counted in a test and held to a limit; it then does not grow by itself as the screen does:

@OptIn(ExperimentalTestApi::class)
fun ComposeUiTest.semanticsNodeCount(useUnmergedTree: Boolean = false): Int {
    fun count(node: SemanticsNode): Int = 1 + node.children.sumOf { count(it) }
    return count(onRoot(useUnmergedTree).fetchSemanticsNode())
}

@Test
fun the_orders_screen_keeps_its_tree_small() = runComposeUiTest {
    setContent { AppTheme { OrdersScreen(orders = sampleOrders(500)) } }
    val count = semanticsNodeCount()
    assertTrue(count < 300, "the orders screen holds $count semantics nodes")
}

The merged tree's count is close to the elements iOS makes; the unmerged tree's (useUnmergedTree = true) is the nodes iOS walks.

5.2. With time profiler on iOS

The screen is opened and, while something reads the tree (VoiceOver on a device, a test driver or Accessibility Inspector on the simulator), a recording is taken as it scrolls:

xcrun xctrace record --template 'Time Profiler' --device <udid> --attach <pid> --time-limit 20s --output screen.trace
xcrun xctrace export --input screen.trace --xpath '/trace-toc/run[@number="1"]/data/table[@schema="potential-hangs"]' > hangs.xml

potential-hangs lists where the main thread was held long, with their starts and durations; the time-profile table tells which function held it how long (in Compose, AccessibilityMediator.sync). Kotlin/Native's Debug build is slower than its Release: a duration is told with the build it was measured in.

6. Verification

Seen

On the iPhone 18 Pro Max simulator (iOS 27.0), in the demo's Debug build, on the Data Table page, while Maestro's driver read the tree, with Time Profiler (a pass of AccessibilityMediator.sync, on the main thread):

PageA pass, medianLongestHangs
Before: sixteen examples in one column403 ms417 ms21 in 60 s
A lazy page159 ms216 ms1 in 100 s
A lazy page and a cell of one node118 ms172 ms0 in 100 s

Tests

Read in the source

Compose Multiplatform 1.12.2, the source of ui-iosarm64, androidx/compose/ui/platform/Accessibility.ios.kt:

Not verified

7. Where in this repository

WhatWhere
The demo's lazy pageapp/shared/src/commonMain/kotlin/com/pantrixui/cmp/demo/page/ComponentPage.kt (ComponentPage)
Data Table's cell and a record's lineui/components/src/commonMain/kotlin/com/pantrixui/components/data/datatable/DataTableBody.kt (BodyCell), DataTableList.kt (RecordLine)
A list's page and its roomui/components/src/commonMain/kotlin/com/pantrixui/components/container/page/PageScaffold.kt (ListBody), LazyPageScope (PageAction.kt)
TestsDataTableTest, PageScaffoldTest (ui/components/src/commonTest), DemoPagesTest (app/shared/src/jvmTest)
The open item about the cost in Composedocs/OPEN_ITEMS.md, section 3