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

Performance optimizations #200

Merged
merged 4 commits into from
Mar 16, 2022
Merged

Performance optimizations #200

merged 4 commits into from
Mar 16, 2022

Conversation

easyaspi314
Copy link
Contributor

@easyaspi314 easyaspi314 commented Mar 7, 2022

Contributes to #141 on the lower level.

This greatly reduces memory and CPU usage without touching any algorithms.

  • Change most Vector2 operations to be destructive ( += ) if possible, returning the this pointer instead of a new vector.
    • Non-destructive ones now use static methods
    • Some operations were shifted to allow this to work.
  • Add more complex Vector2 functions that do not use temporaries (distance, angle, direction)
  • Convert SpriteTransformer to static methods and directly pass the Canvas to the callback instead.
  • Make Paint brushes static if possible
  • Convert some ArrayLists to HashSets if applicable, allowing O(1) contains lookup
  • Avoid creating UnmodifiableList/UnmodifiableCollection instances, instead return the list directly.

This significantly reduces the number of Vector2 temporaries.

 - Direction, angle, and distance now use special functions, the latter
   two are zero allocation.
 - Add, sub, mul, etc are destructive. In places where a temporary is required, a static method is used.

Some code has been reordered to make this work using the distributive
property.
Pass the Canvas to the callback instead, saves a lot of tiny
allocations.
@easyaspi314
Copy link
Contributor Author

I'm going to add a few more optimizations since I have these changes already.

- Use HashSet instead of ArrayList when appropriate, avoiding slow linear
  searches
- Fix one instance of the ConcurrentModificationException requiring
  a CopyOnWriteArrayList (uses Iterator.remove() instead of Collection.remove())
- Avoid creating UnmodifiableList objects. While these don't deep copy the
  list, they still allocate an UnmodifiableList object, where a reference
  would not allocate anything.
Move constant Paint objects into static data.
@easyaspi314 easyaspi314 changed the title Reduce SpriteTransformer and Vector2 memory allocations Performance optimizations Mar 7, 2022
@mjaun
Copy link
Owner

mjaun commented Mar 16, 2022

Looks very good! Thanks a lot for your improvements!

@mjaun mjaun merged commit 30606b8 into mjaun:master Mar 16, 2022
@easyaspi314 easyaspi314 mentioned this pull request Mar 20, 2022
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.

2 participants