Skip to content

[Flutter GPU] Make recording and replay allocation-free - #196621

Open
bdero wants to merge 10 commits into
flutter:masterfrom
bdero:bdero/gpu-raster-timeline-followups
Open

bdero wants to merge 10 commits into
flutter:masterfrom
bdero:bdero/gpu-raster-timeline-followups

Conversation

@bdero

@bdero bdero commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Related to #193745. Follows #194882.

The big juice.

This is basically the grabbag of perf improvements to make #194882 well worth the squeeze. Haven't tested for display list canvas ops yet, but on my high draw count tests for flutter_gpu, this PR increases throughput by ~40%!

Gets us to the point where a steady frame makes no heap allocations per draw on either thread:

  • Recording pools storage and keeps one reference per object per pass
  • Pipeline state is plain data resolved through a per-pipeline cache
  • Replay binds through non-owning references.
  • Tests hold recording and replay to zero with a counting operator new.

In Impeller, Metal's binding cache now uses flat arrays and skips reapplying an unchanged pipeline, Vulkan binds a pipeline only when it changes and skips tracking non-owning references, OpenGL ES presizes its command storage and reuses a scratch buffer for padded uniform arrays, and render passes report pipeline use counts once per pipeline instead of once per draw.

Smaller changes:

  • Submitting waits while 32 MiB of recordings are in flight
  • Completions runs once when the backend also reports a failure
  • Texture.fromImage no longer builds a renderer on the calling thread
  • Encoding is traced
  • gpu_unittests now runs with the host engine tests.

Pre-launch Checklist

@bdero bdero added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests flutter-gpu team-fluttergpu Owned by Flutter GPU team CICD Run CI/CD labels Nov 15, 2026
@github-project-automation github-project-automation Bot moved this to 🤔 Needs Triage in Flutter GPU Nov 15, 2026
@bdero
bdero force-pushed the bdero/gpu-raster-timeline-followups branch from 78176a0 to 35fb63a Compare November 19, 2026 23:50
Profile and debug builds counted each draw's pipeline by hashing its
descriptor under a library-wide writer lock, which cost about a fifth of
Flutter GPU's per-draw encode time on Metal and serialized threads encoding
at the same time. Passes now count uses by pointer and report each distinct
pipeline once when encoded.
Metal's binding cache walked nested std::maps for every bind and allocated
nodes in each pass, so it now uses flat arrays. Binding no longer touches
reference counts, and an unchanged pipeline isn't reapplied to the encoder.

Vulkan binds a pipeline only when it changes, and doesn't track textures the
caller keeps alive, which matches BufferView's raw pointer mode. Tracking only
deduplicates against the last entry, so it grew by one entry per bind.

OpenGL ES can presize its command storage, and reuses one buffer when
unpadding uniform arrays instead of allocating one per draw.
A steady frame now makes no heap allocations per draw on either thread. Ops
go into pooled 64 KiB chunks, a pass retains each object it uses once through
a stamp, and pipeline state is plain data resolved through a per-pipeline
cache, so a state change costs one hash. Replay binds through non-owning
references, caches samplers, and presizes backend command storage. Tests hold
recording and replay to zero allocations with a counting operator new.
Encoding happens on the raster thread, so a thread submitting command buffers
in a loop could queue recordings without limit. Submitting now waits, for at
most a second, while 32 MiB of recordings are in flight.
…lure

Some backends report kError for a failed submission before returning the
failure, so the callback ran twice, and the second run used a consumed Dart
handle.
Texture.fromImage built an AiksContext on the calling thread to read an
image's texture, which created the content context and its pipelines there.
It now reads the texture the image already holds.
The target was built in CI but never run.
@bdero
bdero force-pushed the bdero/gpu-raster-timeline-followups branch from 35fb63a to 539e941 Compare November 20, 2026 02:38
@bdero
bdero marked this pull request as ready for review November 20, 2026 02:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces performance optimizations and resource tracking improvements in Impeller and Flutter GPU, such as batching pipeline usage logging via PipelineUseCounter, optimizing PassBindingsCacheMTL with fixed-size arrays and __unsafe_unretained pointers, and refactoring RenderPass to use a lightweight PipelineState struct. Feedback suggests checking for null textures in RenderPass::CaptureRenderTargetState to prevent null pointer dereferences, zero-initializing the PipelineState key in RenderPipeline::Core::Resolve to avoid copying uninitialized stack memory, and overriding aligned memory allocation operators in AllocationCounter to track aligned allocations.

@bdero
bdero requested a review from gaaclarke November 20, 2026 04:29
Over-aligned types allocate through the aligned operator new, which the
counter did not replace, so a path that used one would have read as
allocation-free.
@bdero bdero moved this from 🤔 Needs Triage to ⚙️ In Progress in Flutter GPU Nov 20, 2026
…n counter"

This reverts commit 2535668.

Replacing all four overridable forms of operator new leaves nothing
referencing the __lcxx_override section of libc++ on ELF, so the linker
drops it and the nothrow forms can no longer find its bounds. Nothing on
the recorded paths is over-aligned.

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked Gemini to split up the PR, it proposed 17 different PRs lol. Here is a counter proposal. I like this because it makes reviewing impeller changes easier by splitting it up. The agent is really concerned about PR E so it wants a separate PR for that. I think alternatively [A1, A2, B+C+D, E] may be fine too?

I haven't dove into it. I'm trying to be careful, this one touches impeller proper a lot more. What do you think?

PR Scope ~Lines Depends on
A1 Impeller, Metal: flat-array binding cache (with tests), inline GetMTLBuffer, cached resolved MTLTexture 340 —
A2 Impeller, all backends: PipelineUseCounter (usage reported once per pass), Metal and Vulkan skip already-bound pipelines, GLES uniform scratch buffer 230 —
B Flutter GPU fixes and behavior changes: run gpu_unittests in CI, completion runs once, Texture.fromImage without building a renderer, tracing 100 —
C Flutter GPU: plain-data PipelineState, per-pipeline pipeline cache, leaf FFI calls (isLeaf: true), allocation-counting test harness with budgets 750 —
D Flutter GPU (plus Impeller APIs): RetainStamp, RecordingPool (the arena part), non-owning buffer/texture references and ReserveCommands, non-owning replay, zero-allocation tests 720 C
E In-flight memory limit (blocks the UI thread for up to 1 s) 100 D (optional)

A1, A2, B, and C can all be reviewed at the same time.

@bdero

bdero commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Split out B (#197187), A1 (#197188), A2 (#197189), and C (#197190), all against master so they can be reviewed in parallel. This PR becomes D as those land, with the allocation counter moved here from C, and E follows as its own PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels. flutter-gpu team-fluttergpu Owned by Flutter GPU team

Projects

Status: ⚙️ In Progress

Development

Successfully merging this pull request may close these issues.

2 participants