-
-
Notifications
You must be signed in to change notification settings - Fork 934
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
Conversation
packages/flame/lib/src/sprite.dart
Outdated
@@ -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(); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 <[email protected]>
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
docs
and added dartdoc comments with///
.examples
ordocs
.Breaking Change?
Related Issues