Skip to content

Conversation

@flar
Copy link
Contributor

@flar flar commented Jul 31, 2019

This PR reintroduces the changes from PR #36396 that were reverted
because the calculations would turn a rect that spanned from -maxFinite
to maxFinite into an infinite result rect.

The revert included a new test for infinite rects. I renamed the description
of that test to more closely reflect what was being tested, but it now passes
with the new fix (and the full tests were all run with the test for infinite size
hardcoded to "true" so the new path accurately passes all existing tests for
matrix transformation even though it is rarely executed).

The new fix now checks if the width or height of the rect is infinite and backs
off to a safer version of the transform operation that more closely matches
what used to be done, but eliminates unnecessary operations.

The tests cost a few percentage points regression from the original fix to check
the size of the rect and even if the slow but safe path is taken, it is still about
twice as fast as the original code.

The results below exercise only the optimized affine and perspective paths,
but if you hard-code the test for infinite rects to just "true", you can see the
results for the slow path which are generally about twice as fast as the original
code and maybe 25-50% slower than the fast paths.

Here is the original commit message with some updated numbers:

Primarily these methods no longer allocate any objects other than their
return values.

Additionally, the math in the methods is reduced compared to the general
case math based on known input conditions.

Results from a Moto E2 phone...
Before:

I/flutter (12908): MatrixUtils.transformRectPerspective: 3167.3 ns per iteration
I/flutter (12908): MatrixUtils.transformRectAffine: 3098.9 ns per iteration
I/flutter (12908): MatrixUtils.TransformPointPerspective: 640.2 ns per iteration
I/flutter (12908): MatrixUtils.TransformPointAffine: 633.0 ns per iteration

After original fix:

I/flutter ( 8339): MatrixUtils.transformRectPerspective: 1026.5 ns per iteration
I/flutter ( 8339): MatrixUtils.transformRectAffine: 647.7 ns per iteration
I/flutter ( 8339): MatrixUtils.transformPointPerspective: 511.9 ns per iteration
I/flutter ( 8339): MatrixUtils.transformPointAffine: 477.4 ns per iteration

After new fix:

I/flutter (18075): MatrixUtils.transformRectPerspective: 1030.8 ns per iteration
I/flutter (18075): MatrixUtils.transformRectAffine: 679.1 ns per iteration
I/flutter (18075): MatrixUtils.transformPointPerspective: 501.8 ns per iteration
I/flutter (18075): MatrixUtils.transformPointAffine: 468.5 ns per iteration

Results from a Samsung Note 9:
Before

I/flutter ( 3761): MatrixUtils.transformRectPerspective: 432.0 ns per iteration
I/flutter ( 3761): MatrixUtils.transformRectAffine: 430.4 ns per iteration
I/flutter ( 3761): MatrixUtils.transformPointPerspective: 64.6 ns per iteration
I/flutter ( 3761): MatrixUtils.transformPointAffine: 63.5 ns per iteration

After original fix:

I/flutter ( 5862): MatrixUtils.transformRectPerspective: 80.4 ns per iteration
I/flutter ( 5862): MatrixUtils.transformRectAffine: 55.0 ns per iteration
I/flutter ( 5862): MatrixUtils.transformPointPerspective: 39.4 ns per iteration
I/flutter ( 5862): MatrixUtils.transformPointAffine: 39.4 ns per iteration

After new fix:

I/flutter (14548): MatrixUtils.transformRectPerspective: 83.3 ns per iteration
I/flutter (14548): MatrixUtils.transformRectAffine: 58.9 ns per iteration
I/flutter (14548): MatrixUtils.transformPointPerspective: 41.2 ns per iteration
I/flutter (14548): MatrixUtils.transformPointAffine: 39.2 ns per iteration

flutter#36396)

Primarily these methods no longer allocate any objects other than their
return values.

Additionally, the math in the methods is reduced compared to the general
case math based on known input conditions.

Modified to no longer generate infinite values in some finite cases.
@flar flar requested review from dnfield and yjbanov July 31, 2019 00:21
@fluttergithubbot fluttergithubbot added the framework flutter/packages/flutter repository. See also f: labels. label Jul 31, 2019
Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

LGTM

@flar flar merged commit 76d13ab into flutter:master Jul 31, 2019
@flar flar deleted the reoptimize-transform-rect-point branch September 4, 2020 19:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants