# Recomposition, Layout, and DrawScope: One Idea, Three Phases

> Compose runs composition, layout, and draw as separate phases. Reading state in the right one is most of what "performance" means in practice.

**Date:** 2026-10-04T00:00:00.000Z
**Tags:** Recomposition, Layout, Canvas

## The intuition

A composable that recomposes on every scroll pixel and one that only
re-measures can look identical in the preview. On screen, only one of
them keeps up with the display, and the difference is never the state
itself, it's which phase reads it.

Every frame goes through three phases, each with its own snapshot
observer that only reruns for state reads that happened *inside that
phase*:

* **Composition**: what exists (most expensive)
* **Layout**: where/how big (medium)
* **Draw**: what pixels (cheapest)

The cost isn't flat across them: composition is the most expensive
phase to rerun, layout is medium, draw is cheapest.

So the question that matters isn't `where is this value used`, it's
`which phase am I reading it in`, because that answers `what's the
cheapest phase this read can land in`.

That cost ranking only matters because all three phases share one
deadline: the display's refresh clock. Composition, layout, and draw
all have to finish before the next vsync (Vertical sync: the display's fixed refresh clock. Every phase (composition, layout, draw, GPU rasterization) must finish before each tick, or the previous frame is shown again.) tick,
or the previous frame gets shown again (a dropped frame, visible as
jank).

| Refresh rate | Budget per frame |
|---|---|
| 60 Hz | 16.6 ms |
| 90 Hz | 11.1 ms |
| 120 Hz | 8.3 ms |

That shared budget is also shared *unevenly*: an unnecessary
recomposition costs the most, an unnecessary re-layout costs less,
and an unnecessary redraw costs the least.

At 120 Hz you only get 8.3 ms total for all three phases plus the GPU,
so a composable that recomposes on every scroll pixel is the first
thing to blow the budget and cause a stutter.

## Tip 1: Recomposition, read late not early

```kotlin
// ❌ offsetPx baked into the Modifier chain at composition time →
// every change rebuilds the chain → recomposes every scroll pixel
Modifier.offset(x = offsetPx.dp)

// ✅ offsetPx read inside the placement lambda, not passed as a value →
// Modifier's identity stays stable, only placement reruns
Modifier.offset { IntOffset(0, offsetPx.roundToInt()) }
```

`Modifier.offset(x: Dp, y: Dp)` takes plain values, so `offsetPx.dp`
has to be resolved *before* the `Modifier` chain exists, during
composition. Every change produces a new `Modifier` and forces a
recompose.

`Modifier.offset { IntOffset(...) }` instead stores the lambda itself
and only calls it during placement, part of the layout phase, so the
`Modifier`'s identity never changes and composition never reruns.
Same `State`, same effect on screen, different phase footing the bill.

> **The one exception**
>
> Whether a node *exists at all* (`AnimatedVisibility`'s `visible`) has
> to be decided in composition; layout and draw can only move or
> repaint what composition already emitted.

## Tip 2: Layout, constraints down, sizes up, parent decides position

```kotlin
// ❌ read in the composable body → heightPx change recomposes
val heightPx = lerp(maxHeightPx, minHeightPx, collapseFraction()).roundToInt()
Modifier.layout { measurable, constraints ->
    val placeable = measurable.measure(constraints.copy(maxHeight = heightPx))
    layout(placeable.width, heightPx) {
        placeable.placeRelative(0, 0)
    }
}

// ✅ read inside the modifier's own lambda → only re-measures, no recomposition
Modifier.layout { measurable, constraints ->  // constraints: the box I must satisfy
    val heightPx = lerp(maxHeightPx, minHeightPx, collapseFraction()).roundToInt()
    val placeable = measurable.measure(        // measurable: pending, not sized until I call this
        constraints.copy(maxHeight = heightPx)   // I choose what to hand down (height pinned)
    )
    layout(placeable.width, heightPx) {        // I report MY size upward to my parent
        placeable.placeRelative(0, 0)            // I alone decide where the child sits
    }
}
```

A child only gets to pick a size inside the `[min, max]` box its
parent hands it; it never picks its own position.

`Modifier.layout` reads state at the layout phase, medium cost,
cheaper than composition but pricier than draw. So `heightPx` has to
be read *inside* the lambda, same lesson as Tip 1, or every
collapse-drag pixel recomposes instead of just re-measuring.

## Tip 3: DrawScope, pixels, top-left origin, and centered strokes

```kotlin
// ❌ read in the composable body → progress change recomposes
val progress = scrollProgress()
Modifier.drawBehind {
    val stroke = 8.dp.toPx()
    // inset by half the stroke width, or the outer half clips off
    drawCircle(Color.Blue.copy(alpha = progress),
        radius = size.minDimension / 2 - stroke / 2, style = Stroke(stroke))
}

// ✅ read inside drawBehind's own lambda → only redraws, no recomposition
Modifier.drawBehind {
    val stroke = 8.dp.toPx()
    val progress = scrollProgress()
    drawCircle(Color.Blue.copy(alpha = progress),
        radius = size.minDimension / 2 - stroke / 2, style = Stroke(stroke))
}
```

Inside `DrawScope`, everything is already in **pixels**, origin is
**top-left**, +y goes **down**. Strokes are centered on the path, not
inside it, hence the half-stroke inset above, or the outer half clips
off.

* **`drawBehind`**: paints only *before* the node's own content, every draw pass, no caching. The simplest option when you just need something underneath.
* **`drawWithContent`**: you call `drawContent()` yourself, so you control the order: before, after, or both, by placing that call wherever you want relative to your own drawing.
* **`drawWithCache`**: the outer lambda only reruns when size/layout changes, not every frame; build expensive objects (`Brush`, `Path`, `Shader`) there once, then return an `onDrawBehind { }` or `onDrawWithContent { }` block that reuses them on every actual draw.

Draw is the cheapest of the three phases to rerun, so `progress` has
to be read *inside* whichever of these you use, same lesson as Tips 1
and 2: reading it here never forces a re-measure, let alone a
recomposition.

## Elsewhere: same fundamental, different framework

* **Browser rendering pipeline (CSS)**: style recalc, layout (reflow),
  paint, composite, the same cost ordering under a different name.
  Animating `transform`/`opacity` skips straight to composite, no
  reflow or repaint; animating `width`/`top` forces the full pipeline.
  The "stay on the compositor thread" advice is this post's Tip 1 and
  3, just framed as a browser DevTools performance tip instead of a
  Compose one.
* **Flutter**: the same three-phase split, spelled as build, layout,
  paint, with `markNeedsBuild()`, `markNeedsLayout()`, and
  `markNeedsPaint()` as the literal three invalidation knobs on a
  `RenderObject`. Calling the cheapest one that's correct is the exact
  same discipline as reading state in the right Compose phase.
* **Classic Android `View` system**: `invalidate()` (draw only) versus
  `requestLayout()` (measure and layout, then draw) is the direct
  ancestor of Tip 2 and 3's distinction, minus a composition phase,
  since the `View` system was never declarative to begin with.
* **UIKit**: `setNeedsDisplay()` versus `setNeedsLayout()` draws the
  same line, minus a composition phase, same as the classic `View`
  system above.
* **SwiftUI**: the same three-phase split, spelled as body
  re-evaluation (composition), the `Layout` protocol's
  `sizeThatFits`/`placeSubviews` (layout), and `Canvas`'s
  `GraphicsContext` (draw), same pixel space, top-left origin, same
  centered-stroke gotcha as `DrawScope`. Tip 1's "read late, not
  early" has a direct counterpart in `Animatable`: a `Shape` or
  `ViewModifier` declares `var animatableData`, and SwiftUI
  interpolates and re-renders it through layout/draw every frame
  without ever re-invoking `body`.


---

Canonical page: https://recomposed.blog/phase-aware-compose-tips