diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da21dae..47855f42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Editor/PreviewSystem/ComputeContext/SingleObjectQueries.cs b/Editor/PreviewSystem/ComputeContext/SingleObjectQueries.cs index 2d60d558..ecf09c2a 100644 --- a/Editor/PreviewSystem/ComputeContext/SingleObjectQueries.cs +++ b/Editor/PreviewSystem/ComputeContext/SingleObjectQueries.cs @@ -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 @@ -200,12 +200,24 @@ public static bool ActiveAndEnabled(this ComputeContext ctx, Behaviour c) return ActiveInHierarchy(ctx, c.gameObject) && ctx.Observe(c, c2 => c2.enabled); } + private static C InternalGetComponent(GameObject obj) where C : Component + { + if (obj != null && obj.TryGetComponent(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(this ComputeContext ctx, GameObject obj) where C : Component { if (obj == null) return null; return ObjectWatcher.Instance.MonitorGetComponent(obj, ctx, - () => obj != null ? obj.GetComponent() : null); + () => obj != null ? InternalGetComponent(obj) : null); } public static Component GetComponent(this ComputeContext ctx, GameObject obj, Type type) @@ -213,7 +225,7 @@ public static Component GetComponent(this ComputeContext ctx, GameObject obj, Ty 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(this ComputeContext ctx, GameObject obj) where C : Component