Skip to content

Commit

Permalink
perf: reduce GC pressure from ComputeContext.GetComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Sep 1, 2024
1 parent c5816cb commit 2dfa2a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- [#362] Fixed unclosed profiler scope in TargetSet
- [#355] Excessive invalidation when scene view visibility states change
- [#363] Reduce GC pressure caused by `ComputeContext.GetComponent`

### Changed

Expand Down
22 changes: 17 additions & 5 deletions Editor/PreviewSystem/ComputeContext/SingleObjectQueries.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#region

#if NDMF_VRCSDK3_AVATARS
using VRC.SDK3.Avatars.Components;
#endif
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using nadena.dev.ndmf.cs;
using UnityEngine;
#if NDMF_VRCSDK3_AVATARS
using VRC.SDK3.Avatars.Components;
#endif
using Object = UnityEngine.Object;

#endregion
Expand Down Expand Up @@ -200,20 +200,32 @@ public static bool ActiveAndEnabled(this ComputeContext ctx, Behaviour c)
return ActiveInHierarchy(ctx, c.gameObject) && ctx.Observe(c, c2 => c2.enabled);
}

private static C InternalGetComponent<C>(GameObject obj) where C : Component
{
if (obj != null && obj.TryGetComponent<C>(out var c)) return c;
return null;
}

private static Component InternalGetComponent(GameObject obj, Type type)
{
if (obj != null && obj.TryGetComponent(type, out var c)) return c;
return null;
}

public static C GetComponent<C>(this ComputeContext ctx, GameObject obj) where C : Component
{
if (obj == null) return null;

return ObjectWatcher.Instance.MonitorGetComponent(obj, ctx,
() => obj != null ? obj.GetComponent<C>() : null);
() => obj != null ? InternalGetComponent<C>(obj) : null);
}

public static Component GetComponent(this ComputeContext ctx, GameObject obj, Type type)
{
if (obj == null) return null;

return ObjectWatcher.Instance.MonitorGetComponent(obj, ctx,
() => obj != null ? obj.GetComponent(type) : null);
() => obj != null ? InternalGetComponent(obj, type) : null);
}

public static C[] GetComponents<C>(this ComputeContext ctx, GameObject obj) where C : Component
Expand Down

0 comments on commit 2dfa2a1

Please sign in to comment.