Rendering performance
What a change costs in style, layout and paint, how layers and draw calls work, and how to keep text, images and effects cheap in Aperture UI.
Profile layout separately from paint and GPU execution. This page covers the work between a DOM change and the command list: style, layout and paint, then how that list turns into draw calls. It describes the mechanisms in this version of the engine and publishes no timings. Performance measurement lists the tools that measure them.
Layout-affecting changes lay out the elements they touch and their ancestors, not the whole page,
because incremental layout is on by default. Use transforms for movement when appropriate and
measure the difference. Fractional opacity, filters, backdrop filters and blend modes can require
intermediate targets, while the stacking contexts that transforms and will-change create do not
imply cached rendering. See Rendering architecture.
What a change costs
The cost of a change depends on how much of the pipeline it forces.
| You change | The engine does |
|---|---|
| An inline style from script, only on the paint-only list below | Restyles the element and repaints. Skips layout. |
| An inline style, any other property | Restyles, lays out and repaints |
| A class or attribute | Restyles that element's subtree, or the whole document if a stylesheet uses +, ~ or :has(). Then lays out every restyled element and its ancestors, and repaints. |
A pseudo-class state such as :hover or :focus | Restyles the element, then lays out and repaints as above. Its descendants restyle too if a rule such as .card:hover .title ties them to the state. For :hover, a stylesheet that uses +, ~ or :has() widens this: .a:hover + .b restyles a sibling and :has(:hover) an ancestor, so the engine restyles the subtree of the nearest common ancestor of the old and new hovered elements and marks the ancestors above it for restyle. |
| Inserting or removing DOM nodes | Reconciles the changed parts into the engine's element tree, then lays out. When it cannot reconcile, it rebuilds the tree. |
| An animation or transition frame | Repaints for opacity, colors, background position and transforms. Lays out for sizes, margins, padding and offsets. |
The paint-only list is exact. When every inline-style change since the previous frame is made only to
these properties, the frame skips layout: transform, transform-origin, rotate, scale,
translate, opacity, color, background, background-color, background-image,
background-position, background-position-x, background-position-y, box-shadow, text-shadow,
filter, backdrop-filter, visibility, z-index, cursor, outline-color, border-color, the
four border-*-color properties, caret-color, text-decoration-color and will-change. One
exception: a change to or from visibility: collapse counts as a layout change, because a collapsed
table row or column changes its size. Any property that is not on the list counts as a layout
change, and so does a change to any other attribute. One such change anywhere in that frame's batch
forces layout for the frame.
That makes element.style.transform = ... the cheap way to drive a per-frame animation from script.
width, height, top, left, margin, padding, font-size and display are not on the list.
Other restyles do not get that shortcut. The engine does not compare the old and new style to see whether layout could have changed: every element that is restyled is laid out again, together with its ancestors. Layout is incremental, so this is cheap for a small subtree, and it is the reason to use the inline paint-only path, or a CSS animation, for anything that changes every frame.
Style
The engine indexes every rule by the rightmost compound of its selector. A rule that ends in an id, a
class or a tag is looked up directly. A rule that ends in anything else, such as an attribute
selector, :hover on its own, a pseudo-element on its own or *, is tested against every element.
For a rule with ancestors, the engine first checks a compact filter of the ancestors' tags, ids and
classes, so a descendant selector that cannot match is usually rejected without a walk up the tree.
- Style with classes, and end your selectors in a class or an id.
@mediaconditions are evaluated once when the media environment changes, not once per element.@containerrules are evaluated for each element.- Sibling combinators (
+,~) and:has()have a global cost. They are the only selectors that let a change on one element alter the styling of elements outside its subtree. If any rule in any stylesheet uses one, including inside@mediaor@layer, a class or attribute change restyles the whole document. Keep these out of a HUD that changes every frame, or use a class that your script toggles.
Style also runs across threads. When enough elements are dirty to pay for it (a fixed minimum count),
the engine computes style one tree level at a time on worker threads, and applies the effects that
mutate the tree afterwards, on the calling thread. A few dirty subtrees take the serial path.
ApertureSDK::SetParallelStyleEnabled(false) turns the parallel pass off, to compare. See
SDK settings.
Layout
Layout is incremental. When part of the page is dirty, the engine marks the dirty elements and all of their ancestors as affected and lays out only those. For any other element it checks whether the space its parent would give it is the same as last time, and if it is, reuses the previous box and skips its whole subtree. So the cost of a change is its own subtree plus the formatting contexts of its ancestors, not the page.
- A change that stays inside a small subtree is cheap. A change near the root, or one that changes the space handed to many siblings, is not.
- Scrolling alone does not re-run layout, except that
position: stickyboxes follow the scroll and are laid out again. - Reading a geometry value from script (
getBoundingClientRect,offsetWidth) lays out first if a change is pending, andgetComputedStyleruns pending style. Do all your reads, then all your writes. A loop that alternates them pays for a flush on every pass. - Give images a size, or the page shifts when they decode. See Images.
ApertureSDK::SetIncrementalLayoutEnabled(false) makes every pass lay out the whole tree, which is
useful to rule incremental layout out when a page looks stale.
Paint
Painting turns the laid-out tree into a command list.
- Scroll frames. When nothing but scroll offsets changed since the last paint, the engine reuses the previous paint tree and applies the new offsets instead of building it again.
- Geometry cache. Tessellated shapes such as rounded rectangles and border rings are cached by their inputs, so the same shape is tessellated once.
- Text. Shaped text is cached by its text and font parameters, so unchanged text is not shaped again. Changing a string, a font size or a family shapes it again.
- Shared SVG icons. Identical small SVG icons share one raster in an atlas texture, which lets repeated icons draw together.
ApertureSDK::SetGeometryUploadCacheEnabled(true) is off by default. It skips re-uploading vertex
data that is already resident on the GPU. Verify static content renders identically before you
rely on it.
Layers
Most of a page paints straight into the target. A few properties need their element drawn into an off-screen target first, and composited after, because the effect applies to the group:
opacitystrictly between 0 and 1,filterandbackdrop-filter,- a
mix-blend-modeother thannormal, mask-image,- a
clip-pathofpolygon()orellipse().inset()andcircle()clip with the scissor and the border radius, and need no layer.
Transforms, will-change and position: fixed or sticky form a stacking context, which is
paint-order bookkeeping. They do not allocate a target. will-change therefore does not buy a cached
layer.
What the engine does to keep layers cheap:
- Content-sized targets. The target is as big as the layer's drawn content plus the reach of its filters, clamped to the viewport and rounded up to a coarse grid, so an animating layer keeps reusing the same pooled target. A layer that is empty or off-screen draws nothing. A layer that contains an instanced or unbounded command falls back to a viewport-sized target.
- Retained layers. A layer whose recorded commands are identical to an earlier frame keeps its rendered and filtered target and only draws one quad. A layer holding images, canvas, video, nested targets or custom effects is not retained, because those can change without the commands changing. Targets that go unused for a few frames return to the pool.
- No target available. If the pool cannot supply one, the engine logs a warning once and falls back to per-vertex alpha. Overlapping children then blend twice, and filters are skipped.
So the cost is the number of layers and the pixels in them, plus filter work. A blur has to filter
every pixel of its target, and backdrop-filter also copies the backdrop before it filters it. Keep
these off elements that are large, numerous or changing every frame. Filters run as compute passes in
the backend (see Rendering backends).
UIView::setBackdropFilterSettings() is declared but has no implementation in this version, so do
not call it: it fails at link time.
To see the targets a page acquires, set APUI_LAYER_DEBUG (see Performance). A
list of layers is also part of the DevTools Layers area.
Draw calls
The render context turns the command list into draws. In SimpleAPHIRenderContext, the base class
for backends:
- Batching. Commands with identical state merge into one batch. The state includes the shape kind, texture, clip, blend state, gradient, effects and transform. A command can join an earlier batch, within a bounded look-back, as long as no later batch overlaps it on screen, so a run of "background, text, background, text" collapses instead of alternating.
- One upload per submit. All batches are concatenated into one geometry upload, and each batch draws from its offset.
- Draw records. A backend can opt in, with
SupportsDrawRecords(), to move per-draw state into a structured buffer and bind a small table of textures. The batch key then shrinks to blend state, gradient and filters, and UI that needed one draw per element or color collapses into a few. It falls back to the per-batch path for a command list that has custom effects, backdrop blend modes, video frames or clip-path masks. See Rendering backends.
What helps batching: share one texture between many images by using a sprite sheet, reuse the same
few gradients and clip shapes, and group elements that share state. What hurts it: every distinct
texture, gradient, clip or blend state is a new batch. To count them, set APUI_BATCH_STATS.
Text
On the default vector route glyphs are drawn from shared curve data, so a new font size needs no new bitmap. On the atlas route every font, size and weight combination fills atlas space, so keep that set small. On both routes, bundle only the fonts you need. See Fonts.
Images
- Images from files decode on a worker thread. Until the decode finishes the image has no size, so
set
widthandheighton every<img>or style a size, and the page will not shift when the pixels arrive. Adata:URI decodes at once. - The engine caches decoded images. It keeps them while memory is not under pressure and evicts the coldest unreferenced ones when a budget is exceeded, so a screen you return to is often still there. An image that was evicted decodes again asynchronously when it is next used.
- Each distinct
background-imageURL is its own decoded image. Reuse images, and use one sprite sheet for many small ones. - SVG, canvas and Lottie are CPU rasters uploaded as textures when they change. See Canvas and SVG and Lottie.
Before You Call It an Optimization
- Inspect texture memory, draw state changes and, on the atlas route, glyph atlas growth. Bundle fonts covering your locales and test long sessions that repeatedly open screens.
- Keep frequently updated DOM regions small. Reuse elements and change their state instead of replacing the document.
- Compare CPU and GPU captures before treating a change as an optimization. A change that saves style work can cost a layer, and the reverse.
Performance measurement
Measure CPU time, GPU cost and memory in the embedding title, and see how Aperture UI saves work on each frame and which tools show where the time goes.
Scripting performance
What script costs in an Aperture UI frame. The bridge between C++ and V8, compiled-script caching, the tier-up budget, data binding and page habits.