Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the transformRect and transformPoint methods in matrix_utils… #37275

Merged
merged 1 commit into from Jul 31, 2019

Conversation

@flar
Copy link
Contributor

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
#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 yjbanov and dnfield Jul 31, 2019
@googlebot googlebot added the cla: yes label Jul 31, 2019
Copy link
Member

dnfield left a comment

LGTM

@flar flar merged commit 76d13ab into flutter:master Jul 31, 2019
79 checks passed
79 checks passed
tool_coverage-linux Task Summary
Details
WIP Ready for review
Details
add2app-macos Task Summary
Details
add2app-macos
Details
analyze Task Summary
Details
analyze
Details
build_tests-linux Task Summary
Details
build_tests-linux
Details
build_tests-macos Task Summary
Details
build_tests-macos
Details
build_tests-windows Task Summary
Details
build_tests-windows
Details
cla/google All necessary CLAs are signed
customer_testing-linux Task Summary
Details
customer_testing-linux
Details
customer_testing-macos Task Summary
Details
customer_testing-macos
Details
customer_testing-windows Task Summary
Details
customer_testing-windows
Details
deploy_gallery Task Summary
Details
deploy_gallery
Details
deploy_gallery-macos Task Summary
Details
deploy_gallery-macos
Details
docs Task Summary
Details
docs
Details
flutter-build
Details
integration_tests-linux Task Summary
Details
integration_tests-linux
Details
integration_tests-macos Task Summary
Details
integration_tests-macos
Details
integration_tests-windows Task Summary
Details
integration_tests-windows
Details
integration_tests_gradle1-linux Task Summary
Details
integration_tests_gradle1-linux
Details
integration_tests_gradle1-windows Task Summary
Details
integration_tests_gradle1-windows
Details
integration_tests_gradle2-linux Task Summary
Details
integration_tests_gradle2-linux
Details
integration_tests_gradle2-windows Task Summary
Details
integration_tests_gradle2-windows
Details
release_smoke_tests Task Summary
Details
release_smoke_tests
Details
tests_extras-linux Task Summary
Details
tests_extras-linux
Details
tests_extras-macos Task Summary
Details
tests_extras-macos
Details
tests_extras-windows Task Summary
Details
tests_extras-windows
Details
tests_framework_other-linux Task Summary
Details
tests_framework_other-linux
Details
tests_framework_other-macos Task Summary
Details
tests_framework_other-macos
Details
tests_framework_other-windows Task Summary
Details
tests_framework_other-windows
Details
tests_widgets-linux Task Summary
Details
tests_widgets-linux
Details
tests_widgets-macos Task Summary
Details
tests_widgets-macos
Details
tests_widgets-windows Task Summary
Details
tests_widgets-windows
Details
tool_coverage-linux
Details
tool_tests-linux Task Summary
Details
tool_tests-linux
Details
tool_tests-macos Task Summary
Details
tool_tests-macos
Details
tool_tests-windows Task Summary
Details
tool_tests-windows
Details
tool_tests_create-linux Task Summary
Details
tool_tests_create-linux
Details
tool_tests_create-macos Task Summary
Details
tool_tests_create-macos
Details
tool_tests_create-windows Task Summary
Details
tool_tests_create-windows
Details
tool_tests_integration-linux Task Summary
Details
tool_tests_integration-linux
Details
tool_tests_integration-macos Task Summary
Details
tool_tests_integration-macos
Details
tool_tests_integration-windows Task Summary
Details
tool_tests_integration-windows
Details
creativecreatorormaybenot added a commit to creativecreatorormaybenot/flutter that referenced this pull request Oct 31, 2019
…ix_utils. (flutter#36396) (flutter#37275)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.