Skip to content

iOS: a repeating-linear-gradient background on a sheet stalls its open for over a second, and repaints on every frame of a detent drag #661

Closed

Description

@janicduplessis

What happened

A sheet whose page has a fine diagonal texture made of repeating-linear-gradient layers is slow on iOS in two ways:

  1. Every open stalls. In a Release build on an iPhone 18 Pro simulator (iOS 27.0), opening such a full-height sheet held the main thread for 1.5 to 1.9 s, followed by a commit of about the same length. xctrace counted 22.5 B instructions, nearly all of them ripc_DrawShading under NodeLayer.display().
  2. Every frame of a detent drag repaints the texture. Dragging the same sheet between medium and large ran at 7 to 13 UI updates per second, with 58 to 127 ms per update spent in NodeLayer.display. An axis-aligned 14 px grid texture ran at 46 to 53.

All numbers are from the simulator. Neither was measured on a device.

Cause

The stall. Gradient.paintAxialRuns (host/apple/Sources/ExactKit/Gradient.swift) draws a gradient with hard stops as one drawLinearGradient per continuous run. Core Graphics shades each call over the whole clip, whatever the run's extent. So a repeating gradient costs the box's area times its stripe count, and a diagonal hatch across a phone screen has hundreds of runs.

The drag. A repeating-linear-gradient background makes the node draw its whole box in draw(_:) (gradientDraws → boxDrawn). The view's contentMode is .redraw, and UIKit resizes a sheet with detents on every frame of an interactive drag, so the full-size texture is rasterized again on every frame. Making each repaint cheaper does not change that.

How a fork fixed it

Two changes, both on Apple hosts only:

  • Shade each run only where it falls (appandflow/exact@b7c16ebe1, about 30 lines in paintAxialRuns). With more than eight runs, the clip is cut into 32-pixel tiles on the device grid, and each tile draws only the runs whose span along the gradient line crosses it. These are the same drawLinearGradient calls with the same options, so each pixel is shaded by the same run as before. Runs whose colours are all transparent are skipped. Eight runs or fewer, or a device transform with rotation, keep the whole-clip loop. The open went from 22.5 B instructions to 1.0 B, about 85 ms including the commit. The Chrome parity hatch case in BorderParityMacTests stays at 0.00/255.
  • Paint a repeating background as tiles on iOS, so a resize draws nothing (appandflow/exact#9). When every background layer is a repeating linear gradient whose period is the same at every box size, Gradient.period(dark:) reads one period from the resolved stops, and the gradient paints as cached tile pictures on sublayers placed from the device pixel of the corner where the gradient line starts. Cells with the same phase share one CGImage, so at 45° there is no seam or drift. The node keeps no bitmap, and a whole-pixel resize reuses existing pictures. A gradient with a percentage stop, to <corner>, mixed layers, linear-light interpolation, or a box that something else makes draw keeps the drawn path. The drag went to 54 to 60 updates per second, with NodeLayer.display at 0.05 to 0.09 ms per update. The PR description has the measurements and the tile tests (GradientTileTests, GradientTilesIOSTests). macOS is unchanged.

Host

iOS (the run-shading cost applies to macOS too, since it is the shared Gradient.swift)

Smallest app that shows it

Open the sheet, then drag it between its detents.

routes nav
  home "/"
    menu "/menu"

component App
  action follow(url: string)
    nav = go(nav, url)
  action back
    nav = back(nav)
  view
    main navigationKey=`${top(nav).id}` navigationBack="back" navigate=follow width="100%" height="100%"
      for e in stack(nav) key=e.id
        if e.name == "home"
          column navigationKey=`${e.id}` position="absolute" inset=0 padding=16
            link href="/menu" testId="open-menu"
              text "Open"
        if e.name == "menu"
          column navigationKey=`${e.id}` navigationPresentation="modal" navigationDetent="medium large" position="absolute" inset=0 padding=16 background-color="#fcfcfc" background-image="repeating-linear-gradient(-45deg, transparent 0 4.243px, #e6e6e6 4.243px 5.657px), repeating-linear-gradient(45deg, transparent 0 4.243px, #e6e6e6 4.243px 5.657px)"
            text "Sheet"

This snippet compiles. The measurements above came from an app with the same structure, not from this exact snippet.

Exact commit

Measured on a fork based on a107143. The code described above is unchanged on upstream main at f3a4ace.

Activity

  1. ccheever commented on Nov 15, 2026

    @ccheever
    Contributor

    Part 1, shading each stripe only in the tiles it crosses, is being built now. Part 2 is decided, after a three-model panel: a repeating linear gradient is drawn as one period that the platform tiles. The period is rasterized axis-aligned and rotated with the pattern matrix, so every angle tiles, and its phase follows the view rather than the window, so a sheet drag or a Mac live resize draws nothing. Other paints keep drawing. Queued to build after part 1.

    Posted by Claude Code on the maintainers' behalf.

  2. ccheever commented on Nov 15, 2026

    @ccheever
    Contributor

    Part 1 is in b121e3d, using the tiling approach from appandflow/exact@b7c16ebe1, credited in the code and the commit. paintAxialRuns now shades stripes only where they fall. On an iPhone simulator (iOS 27, debug build), the issue's two hatch layers at 402×874 pt and 3× went from 2.2–4.3 s to 0.19–0.5 s. An iPad-sized box went from about 5 s to about 0.5 s. The hatch parity case is unchanged, and at phone size the opaque hatch is byte-identical. On large boxes some pixels did change, and checked against where CSS puts each stripe, the new output is the correct one: the old code drifted along the gradient. This stays open for part 2, a tiled period with a phase tied to the view.

    Posted by Claude Code on the maintainers' behalf.

  3. ccheever commented on Nov 15, 2026

    @ccheever
    Contributor

    Part 2 is in 3c5a260, which closes this. On Apple, each repeating linear gradient in a background is now its own sublayer. The kernel works out one period, that period is drawn once as a Core Animation pattern cell, and Core Animation tiles it. The sublayer is turned to the gradient's angle and its origin sits where a period starts, so the stripes stay with the view, and a resize only moves that origin. The code is shared by iOS, tvOS and macOS, so macOS live resize gets this too.

    On an iPhone simulator (iOS 27), dragging the issue's sheet through its detents:

    Display calls Time per call Redraws
    Before 30 24–39 ms the whole texture each time
    After 54 0.13–0.32 ms none

    The main thread now keeps up, which is why there are more calls.

    Compared with Chrome on screen at 3x, the mean difference out of 255 is 0.00 at 0°, 45° and 90°, 1.32 at 30°, and at most 2.92 elsewhere. The hatch parity tests pass on iOS and macOS. One limit remains: a gradient whose period or angle depends on the box's size (percentage stops, to <corner>) draws a new cell on each resize. That is still far cheaper than redrawing the box. LLP 1066 is amended to match.

    Posted by Claude Code on the maintainers' behalf.

  4. added a commit that references this issue on Nov 16, 2026
    3c5a260
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions