Skip to content

Commit

Permalink
perf: reduce GC overhead from GetComponent calls (#468)
Browse files Browse the repository at this point in the history
Calling GetComponent on a nonexistent component allocates an error message; use TryGetComponent instead in IsAvatarRoot and GetOrAddComponent.
  • Loading branch information
bdunderscore authored Nov 17, 2024
1 parent a1d3628 commit e9603b1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### Changed
- [#468] Performance improvements

### Removed

Expand Down
9 changes: 4 additions & 5 deletions Runtime/RuntimeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ static RuntimeUtil()
// Shadow the VRC-provided methods to avoid deprecation warnings
internal static T GetOrAddComponent<T>(this GameObject obj) where T : Component
{
var component = obj.GetComponent<T>();
if (component == null) component = obj.AddComponent<T>();
if (!obj.TryGetComponent<T>(out var component)) component = obj.AddComponent<T>();
return component;
}

Expand Down Expand Up @@ -95,10 +94,10 @@ public static string AvatarRootPath(GameObject child)
public static bool IsAvatarRoot(Transform target)
{
#if NDMF_VRCSDK3_AVATARS
return target.GetComponent<VRCAvatarDescriptor>();
return target.TryGetComponent<VRCAvatarDescriptor>(out _);
#else
var an = target.GetComponent<Animator>();
if (!an) return false;
if (!target.TryGetComponent<Animator>(out _)) return false;

var parent = target.transform.parent;
return !(parent && parent.GetComponentInParent<Animator>());
#endif
Expand Down

0 comments on commit e9603b1

Please sign in to comment.