Nucleus
Performance & native

Rendering performanceNew

Repaint only what changed with partial redraw, see each frame's damage with the debug overlay, count recompositions, and let idle windows give their GPU memory back.

View as Markdown

This page covers how a Nucleus window renders its frames: partial redraw, the debug switches that show what each frame repaints, and what an idle window does. Everything here comes with nucleus-application and works on macOS, Windows and Linux, on the JVM and in a native image.

Turn on partial redraw

With partial redraw on, a frame repaints and presents only the part of the window that changed since the previous one, instead of the whole window. A blinking cursor or a spinner then repaints only its own area, and a window where nothing moves presents no frame at all.

Partial redraw is off by default. Turn it on with the partialRedraw knob of the startup pack, or with the whole pack:

build.gradle.kts
nucleus.application {
    nucleusOptimization { partialRedraw = true }
    // or: nucleusOptimization = true
}

The knob patches Compose's render layers at build time, so the runtime can tell which layers changed, and turns the runtime on. Your source code stays the same. It applies to the run task, to packaged apps and to GraalVM native images.

Pass -Dnucleus.tao.partialRedraw=false to repaint every frame in full for one run, or true to force it on.

See what each frame repaints

The debug { } block turns on overlays and logs for the run task. Packages, runDistributable and native images never get them.

build.gradle.kts
nucleus.application {
    nucleusOptimization { partialRedraw = true }

    debug {
        partialRedraw {
            tint = true   // flash what each frame changed in red
            stats = true  // log how much of the window frames repaint
        }
    }
}
SwitchEffect
tintFlashes what each frame repainted in translucent red, every other frame, like Android's "Show GPU view updates". A frame repainted in full flashes the whole window.
statsLogs every few seconds how much of the window frames repaint and which layers damaged them, and why frames repaint in full when that reason changes.
verifyRenders every partial frame again in full off screen and logs any pixel that differs. Slow, and turns tint off.

To turn a switch on for one run without editing the build, pass the comma-separated nucleus.debug Gradle property:

./gradlew run -Pnucleus.debug=tint,stats

A flash larger than what moved means the frame repainted more than it needed. The usual cause is a layer that does not clip its content or casts a shadow: its damage is then its nearest clipping ancestor's bounds. The partial redraw switches need partial redraw on; otherwise the build warns and every frame repaints in full.

With tint on, only the moving band flashes red; the static panel is never repainted

Count recompositions

recomposition logs the composables that ran the most, every few seconds, first composition included. It works with or without partial redraw.

build.gradle.kts
nucleus.application {
    debug {
        recomposition {
            enabled = true
            top = 20 // composables per report, default 10
        }
    }
}

-Pnucleus.debug=recomposition turns it on for one run.

Idle windows

A window renders a frame only when its content changes. A coroutine on Dispatchers.Main, such as a delay loop, a clock or a withContext(Dispatchers.Main) hop, runs between frames and does not render one by itself.

Once a window has drawn nothing for 3 seconds, or as soon as it is minimized, Nucleus releases the GPU resources Skia cached for it: the textures an animation or an image-heavy screen uploaded. The next frame recreates what it needs. A window with a live GPU render context consumer keeps its cache. There is nothing to configure.

On macOS, a window out of sight renders nothing: a hidden app, a covered window, a window on another Space or behind a locked screen skips its frames, animations included, and resumes when it shows again. The display link stops 500 ms after the last frame, the accessibility sync waits for an assistive client to connect, and the single-instance watcher uses FSEvents instead of polling. A hidden app falls from about 14 % CPU to under 1 %, so the Mac can sleep.

Notes

  • Some frames always repaint in full: the first one, a resize, a change of window background or corner, a frame while a popup is open, and the frame after a layer that draws content recorded elsewhere, such as a backdrop blur.
  • Under macOS and Windows, a frame whose changes cover 80 % of the window or more repaints in full, which costs less there than a partial frame.
  • The build-time patch only exists in builds of the Nucleus plugin. An app started from the IDE without the plugin's run task, or under hot reload, repaints every frame in full.
  • Compose deactivates a lazy-list item scrolled out of view after the frame instead of inside it. Pass -Dnucleus.tao.outOfFrameExecutor=false to go back to deactivating it inside the frame.
  • On Windows, a window that overhangs the desktop, such as a maximized or fullscreen one, presents through a DirectComposition mirror. GPUs below Direct3D feature level 11_0 (WDDM 1.1 drivers, where the mirror turns black) repaint on move instead. Pass -Dnucleus.tao.windowsFrameMirror=false to do the same on a driver that misbehaves above that level. SystemInfo.renderTier() reports such GPUs as LEGACY.

What's next

Was this page helpful?
Edit on GitHub

Last updated on