Skip to content

Reduce allocations in Matrix4Tween.lerp to improve transform animation performance - #190659

Draft
FelixMittermeier wants to merge 1 commit into
flutter:masterfrom
FelixMittermeier:fix-32055-matrix4-tween-performance
Draft

Reduce allocations in Matrix4Tween.lerp to improve transform animation performance#190659
FelixMittermeier wants to merge 1 commit into
flutter:masterfrom
FelixMittermeier:fix-32055-matrix4-tween-performance

Conversation

@FelixMittermeier

Copy link
Copy Markdown

Fixes #32055

Matrix4Tween.lerp currently allocates decomposition outputs plus temporary translation, rotation, and scale objects on every animation tick. A single interpolation creates at least 16 avoidable vector and quaternion wrappers, along with their typed-data storage, before composing the required result matrix. This adds allocation and garbage-collection pressure to transform animations.

The solution is to retain six lazily initialized scratch objects per tween, decompose the current endpoints into them on every call, and perform the existing scale, add, and normalize operations in place. The arithmetic order and normalized quaternion interpolation remain unchanged, mutable endpoints are still observed on every evaluation, and each call continues to return an independent Matrix4.

Performance

Measured with the maintained matrix4_tween_bench.dart benchmark using Dart 3.14 on macOS arm64. Each reported runtime covers 2,050 lerps and is the median of five runs.

Metric Before After Difference
JIT runtime 1830.8 µs 1374.8 µs -456.0 µs / -24.9%
AOT runtime 3071.3 µs 1706.1 µs -1365.2 µs / -44.5%

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. a: animation Animation APIs labels Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: animation Animation APIs framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential Matrix4Tween.lerp performance improvement

1 participant