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

perf: Avoid Vector2 creation in Sprite.render #2261

Merged
merged 3 commits into from
Jan 10, 2023

Conversation

spydon
Copy link
Member

@spydon spydon commented Jan 9, 2023

Description

This creates two new private vector2 objects that can be re-used inside of Sprite.render so that we don't have to create new objects in there.

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?

  • No, this PR is not a breaking change.

Related Issues

@spydon spydon requested review from st-pasha and a team January 9, 2023 22:29
packages/flame/lib/src/sprite.dart Outdated Show resolved Hide resolved
@@ -81,6 +81,10 @@ class Sprite {
);
}

// Used to avoid the creation of new Vector2 objects in render.
late final _tmpRenderPosition = Vector2.zero();
late final _tmpRenderSize = Vector2.zero();
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably no need to make them late.

Copy link
Member Author

Choose a reason for hiding this comment

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

No harm in doing so right? If some user has some big collection of sprites that aren't being rendered yet they have a little bit less overhead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, the overhead is that there needs to be a runtime check whether the late variable has actually been initialized or not. And I'm not sure whether such a check is debug-mode only, or if it is also done in production...

Copy link
Member Author

Choose a reason for hiding this comment

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

That must be very minimal? But I don't know how it is done.
I'd say it is worth the trade-off of not carrying around two Vector2 before they are used.
I can do some benchmarking tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another possibility is to declare them static. Then you have only one object instance no matter how many sprites

Copy link
Member Author

Choose a reason for hiding this comment

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

True, single thread ftw 😄

Co-authored-by: Pasha Stetsenko <stpasha@google.com>
@spydon spydon merged commit 736733d into main Jan 10, 2023
@spydon spydon deleted the spydon/avoid-vector2-creation branch January 10, 2023 10:23
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.

3 participants