Skip to content

perf: Cache camera render closures instead of allocating per frame - #3979

Merged
spydon merged 3 commits into
mainfrom
perf/render-closure-caching
Aug 16, 2026
Merged

perf: Cache camera render closures instead of allocating per frame#3979
spydon merged 3 commits into
mainfrom
perf/render-closure-caching

Conversation

@spydon

@spydon spydon commented Aug 5, 2026

Copy link
Copy Markdown
Member

Description

CameraComponent.render declared a local renderWorld function and, on the post-processing path, an inline context-setter closure. Both were allocated on every frame for every camera. This PR turns them into the instance methods _renderWorld and _updatePostProcessContext, so the render pass no longer allocates closures per camera per frame and the tear-offs passed to PostProcess.render stay stable.

There is no behavior change, only where the functions live.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • [-] I have updated/added tests for ALL new/updated/fixed functionality.
  • [-] I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Comment thread packages/flame/lib/src/components/router/route.dart Outdated
Comment thread packages/flame/lib/src/components/position_component.dart Outdated
Comment thread packages/flame/lib/src/post_process/post_process_component.dart Outdated
Comment thread packages/flame/lib/src/rendering/decorator.dart Outdated

@luanpotter luanpotter 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.

LGTM

@spydon
spydon force-pushed the perf/render-closure-caching branch from f549bd5 to add80f5 Compare August 16, 2026 13:25
Base automatically changed from perf/lifecycle-queue-early-out to main August 16, 2026 13:43
@spydon
spydon force-pushed the perf/render-closure-caching branch from add80f5 to 302f920 Compare August 16, 2026 13:43
@spydon spydon changed the title perf: Cache decorator and camera render closures instead of allocating per frame perf: Cache camera render closures instead of allocating per frame Aug 16, 2026
spydon added 3 commits August 16, 2026 16:39
…per-frame allocation benchmark

The Decorator-internal chained-draw cache only helped chained
decorators, which are rare, and complicated the class, so Decorator is
back to its version on main. The caller-side tear-off caching and the
empty-queue lifecycle early-out remain, now backed by a benchmark that
isolates the fixed per-frame overhead they remove.
@spydon
spydon force-pushed the perf/render-closure-caching branch from 9bf97d0 to 955aed8 Compare August 16, 2026 14:39
@spydon
spydon merged commit f212fbc into main Aug 16, 2026
8 checks passed
@spydon
spydon deleted the perf/render-closure-caching branch August 16, 2026 14:52
spydon added a commit that referenced this pull request Aug 16, 2026
# Description
<!-- End of exclude from commit message -->
Every `Component` eagerly allocated a `QueueList` for render contexts
plus two debug-paint `ValueCache`s. The context stack is now a lazily
created plain list (most components never provide or receive a render
context) and the debug caches are `late final`, so plain components
allocate none of them.

Extracted from #3960 so the data-structure change there stands alone (as
requested in [this
comment](#3957 (comment))).
Stacked on #3979.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants