A Study of the Jetpack Compose SlotTable Internals
A Study of the Jetpack Compose SlotTable Internals
The SlotTable is the in-memory data structure that represents the UI tree of a Jetpack Compose application. Instead of a traditional tree of objects, it's a highly optimized, flat structure designed for extremely fast UI updates. Let's explore its internals by examining the code you provided.
1. The Core Data Model: groups and slots
At the heart of the SlotTable are two parallel, flat arrays. This is the first and most critical concept to grasp.
internal class SlotTable : CompositionData, Iterable<CompositionGroup> {
/**
* An array to store group information... an array of an inline struct.
*/
var groups = IntArray(0)
private set
/**
* An array that stores the slots for a group.
*/
var slots = Array<Any?>(0) { null }
private set
//...
}
-
groups: IntArray: This is the blueprint of your UI. It stores the structure and metadata of your composables in a compact, primitive array. Think of it as a highly efficient, inlined list of instructions that describes the hierarchy, keys, and properties of each composable call. Because it's a flatIntArray, the CPU can scan it very rapidly without expensive memory jumps (pointer chasing). -
slots: Array<Any?>: This is the data warehouse. It stores the actual data and objects produced by your composables, such as remembered values, state objects, lambdas, and UI nodes (LayoutNode).
The groups array tells the Compose runtime how to interpret the slots array. This is a classic data-oriented design, prioritizing cache-friendly traversal over object-oriented convenience.
2. The Nomenclature and the "Inline Struct"
The comments at the top of the file are our Rosetta Stone. They define the terminology used throughout. The most important is the concept of a Group.
A Group is not a class. It's a logical concept representing a contiguous block of 5 integers within the groups array. This is what the comment means by an "inline struct."
// Group layout
// 0 | 1 | 2 | 3 | 4 |
// Key | Group info | Parent anchor | Size | Data anchor |
private const val Group_Fields_Size = 5
This structure is accessed via extension functions that perform bitwise operations, which is extremely fast. For example, to check if a group represents a UI node:
This article continues for subscribers
Subscribe to Dove Letter for full access to 40+ deep-dive articles about Android and Kotlin development.
Become a Sponsor