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

Core SRP CPU Performance Improvements #7657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

smitdylan2001
Copy link
Contributor

Some simple performance improvements. Would be good to take these into new commits as well and could be backported Since nothing changed for the end user.
I did not make changelog edits. Is that necessary?

Improved CPU performance with:

  • Remove indirect gameObject call
  • Re order vector calculations
  • Merge .position and .rotation into .SetPositionAndRotation
  • Use TryGetComponent to reduce garbage allocation
  • Use native length and count instead of LINQ
  • Merge if-else return into return
  • Use AddRange
  • Simplify string comparison

Purpose of this PR

Why is this PR needed, what hard problem is it solving/fixing?
Improve CPU performance with minor fixes


Testing status

Describe what manual/automated tests were performed for this PR
No major changes made to code


Comments to reviewers

Notes for the reviewers you have assigned.
Do I need to add changelog changes?
Can this be backported?

Some simple performance improvements. Would be good to take these into new commits as well and could be backported Since nothing changed for the end user I did not make changelog edits. Is that necessary?

Improved CPU performance with:
- Remove indirect gameObject call
- Re order vector calculations
- Merge .position and .rotation into .SetPositionAndRotation
- Use TryGetComponent to reduce garbage allocation
- Use native length and count instead of LINQ
- Merge if-else return into return
- Use AddRange
- Simplify string comparison
Copy link
Collaborator

@RSlysz RSlysz left a comment

Choose a reason for hiding this comment

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

Thank you for all your tips. This is right. Nothing here should add a drastic improvement but it will contribute to have a more efficient editor. 👍

Adding more people to counter check change in case I miss something.

@@ -890,7 +890,7 @@ static string GetTextureAutoName(int width, int height, string format, TextureDi
temp = string.Format("{0}x{1}{2}_{3}", width, height, mips ? "_Mips" : "", format);
else
temp = string.Format("{0}x{1}x{2}{3}_{4}", width, height, depth, mips ? "_Mips" : "", format);
temp = String.Format("{0}_{1}_{2}", name == "" ? "Texture" : name, (dim == TextureDimension.None) ? "" : dim.ToString(), temp);
temp = String.Format("{0}_{1}_{2}", name?.Length == 0 ? "Texture" : name, (dim == TextureDimension.None) ? "" : dim.ToString(), temp);
Copy link
Collaborator

Choose a reason for hiding this comment

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

At this point, to add the null check, it is even better to rely on String.IsNullOrEmpty

Suggested change
temp = String.Format("{0}_{1}_{2}", name?.Length == 0 ? "Texture" : name, (dim == TextureDimension.None) ? "" : dim.ToString(), temp);
temp = String.Format("{0}_{1}_{2}", String.IsNullOrEmpty(name) ? "Texture" : name, (dim == TextureDimension.None) ? "" : dim.ToString(), temp);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I benchmarked it and isNullOrEmpty actually does a string comparison instead of a null check, so is slower than this!
It is more readable however, so just say what you prefer. I can share the benchmark numbers tomorrow, but it was more than I expected

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please do, I am curious about the difference.

Copy link
Contributor

@arttu-peltonen arttu-peltonen left a comment

Choose a reason for hiding this comment

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

All of these look like valid improvements to me. I'm curious if you can share some performance numbers, I wouldn't expect these to make a big difference but I'm open to be wrong :)

@smitdylan2001
Copy link
Contributor Author

smitdylan2001 commented Sep 15, 2022

Made a public post for others to see. Could not test everything due to time limits.
Interesting enough my looped .Add was faster than .AddRange. Not sure how that happens. (edit: someone explained)

https://forum.unity.com/threads/performance-tips.1336262/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants