Skip to content

Conversation

EmandM
Copy link
Collaborator

@EmandM EmandM commented Sep 17, 2025

Purpose of this PR

@smitdylan2001 found and fixed some small performance improvements, which do not change any functionality. Especially the position and rotation calls and LINQ call removals are very helpful for performance

  1. Merge looped Add calls on List<T> variables into AddRange
    • AddRange will ensure the list is only extended once.
  2. Optimize empty string checks
    • Comparing strings using String.Length is faster than using Equals (C# docs reference)
  3. Merge position & rotation calls into one
    • SetPositionAndRotation has a small performance improvement (docs)
  4. Short circuit operators for bools
    • We had a couple of places that were using bytewise operations on boolean properties. This fixes them.
  5. Use native Count instead of LINQ
    • .Count() uses LINQ and has a cost. .Count does the same thing without the LINQ cost.
  6. Use StringBuilder.AppendJoin rather than StringBuilder.Append(String.Join

continues: #3680

Jira ticket

n/a: contribution from external user

Changelog

  • Changed: made many very small performance improvements.

Documentation

  • Updated the scripting in the docs to use SetPositionAndRotation rather than setting the two independently

Testing & QA (How your changes can be verified during release Playtest)

  • These do not change functionality. Automated testing should catch any compiler errors.

Does the change require QA team to:

  • Review automated tests?
  • Execute manual tests?
  • Provide feedback about the PR?

If any boxes above are checked the QA team will be automatically added as a PR reviewer.

Backports

These are small performance improvements so they do not need to be backported.

smitdylan2001 and others added 4 commits September 17, 2025 09:29
- Merge looped Add calls into AddRange
- Optimize empty string checks
- Merge position & rotation calls into one
- Short circuit operators for bools
- Optimize string generation
- Use native Count instead of LINQ
@EmandM EmandM requested review from jabbacakes and a team as code owners September 17, 2025 21:31
@EmandM EmandM changed the title Fix/performance optimizations chore: Small performance improvements Sep 17, 2025
@EmandM EmandM mentioned this pull request Sep 18, 2025
3 tasks
@EmandM EmandM enabled auto-merge (squash) September 19, 2025 22:00
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity left a comment

Choose a reason for hiding this comment

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

Two minor areas to review over.
The rest looks good to me. 👍

m_ServerAddressProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.Address));
m_ServerPortProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.Port));
m_OverrideBindIpProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.ServerListenAddress));
connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.ServerListenAddress));
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that this last adjusted line is not used... and the property found is not being assigned to anything.
Is there a reason to keep this line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for catching that. The m_OverrideBindIpProperty was never being used anywhere so I pulled it out, but obviously not very thoroughly


ClientPositionVisual.transform.position = m_ClientPosition;
ClientPositionVisual.transform.rotation = InLocalSpace ? transform.localRotation : transform.rotation;
var rotation = InLocalSpace ? transform.localRotation : transform.rotation;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This still needs to use the m_ClientPosition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's using m_ClientPosition on the next line down 😄

@EmandM EmandM disabled auto-merge September 29, 2025 15:06
@EmandM EmandM enabled auto-merge (squash) October 1, 2025 13:53
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity left a comment

Choose a reason for hiding this comment

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

:godmode:

@EmandM EmandM merged commit 33a24b1 into develop-2.0.0 Oct 2, 2025
27 checks passed
@EmandM EmandM deleted the fix/performance-optimizations branch October 2, 2025 20:32
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.

5 participants