From 80a62392c8ecc16dc32c70dddc38b2766139d65c Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 8 Sep 2023 14:39:54 +0900 Subject: [PATCH 1/5] chore: split logic for activeness --- .../FindUnusedObjectsProcessor.cs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs index 14b3c167f..559f1bb3b 100644 --- a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs +++ b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs @@ -45,34 +45,41 @@ public void Process() new Dictionary(); private readonly Queue<(Component, bool)> _processPending = new Queue<(Component, bool)>(); - private void MarkComponent(Component component, - bool ifTargetCanBeEnabled, - ComponentDependencyCollector.DependencyType type) + private bool? GetActiveness(Component component) { - bool? activeNess; + bool? activeness; switch (component) { case Transform transform: var gameObject = transform.gameObject; - activeNess = _modifications.GetConstantValue(gameObject, "m_IsActive", gameObject.activeSelf); + activeness = _modifications.GetConstantValue(gameObject, "m_IsActive", gameObject.activeSelf); break; case Cloth cloth: - activeNess = _modifications.GetConstantValue(cloth, "m_IsEnable", cloth.enabled); + activeness = _modifications.GetConstantValue(cloth, "m_IsEnable", cloth.enabled); break; case Renderer cloth: - activeNess = _modifications.GetConstantValue(cloth, "m_IsEnable", cloth.enabled); + activeness = _modifications.GetConstantValue(cloth, "m_IsEnable", cloth.enabled); break; case Behaviour behaviour: - activeNess = _modifications.GetConstantValue(behaviour, "m_IsEnable", behaviour.enabled); + activeness = _modifications.GetConstantValue(behaviour, "m_IsEnable", behaviour.enabled); break; case Component _: - activeNess = null; + activeness = null; break; default: throw new Exception($"Unexpected type: {component.GetType().Name}"); } - if (ifTargetCanBeEnabled && activeNess == false) + return activeness; + } + + private void MarkComponent(Component component, + bool ifTargetCanBeEnabled, + ComponentDependencyCollector.DependencyType type) + { + bool? activeness = GetActiveness(component); + + if (ifTargetCanBeEnabled && activeness == false) return; // The Target is not active so not dependency if (_marked.TryGetValue(component, out var existingFlags)) @@ -81,7 +88,7 @@ private void MarkComponent(Component component, } else { - _processPending.Enqueue((component, activeNess != false)); + _processPending.Enqueue((component, activeness != false)); _marked.Add(component, type); } } From 5588f948c875e10f5b8640e70f4beda52c2582d8 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 8 Sep 2023 14:40:51 +0900 Subject: [PATCH 2/5] chore: cache activeness --- .../TraceAndOptimize/FindUnusedObjectsProcessor.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs index 559f1bb3b..53fd5e0ca 100644 --- a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs +++ b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs @@ -44,9 +44,13 @@ public void Process() private readonly Dictionary _marked = new Dictionary(); private readonly Queue<(Component, bool)> _processPending = new Queue<(Component, bool)>(); + private readonly Dictionary _activeNessCache = new Dictionary(); private bool? GetActiveness(Component component) { + if (_activeNessCache.TryGetValue(component, out var activenessCached)) + return activenessCached; + bool? activeness; switch (component) { @@ -70,6 +74,8 @@ public void Process() throw new Exception($"Unexpected type: {component.GetType().Name}"); } + _activeNessCache.Add(component, activeness); + return activeness; } From f6676c777f1798a5723be002f915a49fe2d29eea Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 8 Sep 2023 14:52:27 +0900 Subject: [PATCH 3/5] chore: think about activeness of object or parent object --- .../FindUnusedObjectsProcessor.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs index 53fd5e0ca..564187959 100644 --- a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs +++ b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs @@ -48,8 +48,17 @@ public void Process() private bool? GetActiveness(Component component) { - if (_activeNessCache.TryGetValue(component, out var activenessCached)) - return activenessCached; + if (_activeNessCache.TryGetValue(component, out var activeness)) + return activeness; + activeness = ComputeActiveness(component); + _activeNessCache.Add(component, activeness); + return activeness; + } + + private bool? ComputeActiveness(Component component) + { + var parentActiveness = component is Transform t ? GetActiveness(t.parent) : GetActiveness(component.transform); + if (parentActiveness == false) return false; bool? activeness; switch (component) @@ -74,9 +83,10 @@ public void Process() throw new Exception($"Unexpected type: {component.GetType().Name}"); } - _activeNessCache.Add(component, activeness); + if (activeness == false) return false; + if (parentActiveness == true && activeness == true) return false; - return activeness; + return null; } private void MarkComponent(Component component, From 4d9612d08f98ad4d770e000bdcc493c548e983ac Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 8 Sep 2023 15:03:57 +0900 Subject: [PATCH 4/5] chore: inactivating parent or GameObject is not accounted --- .../FindUnusedObjectsProcessor.cs | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs index 564187959..cafec25f5 100644 --- a/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs +++ b/Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs @@ -67,20 +67,42 @@ public void Process() var gameObject = transform.gameObject; activeness = _modifications.GetConstantValue(gameObject, "m_IsActive", gameObject.activeSelf); break; + case Behaviour behaviour: + activeness = _modifications.GetConstantValue(behaviour, "m_IsEnable", behaviour.enabled); + break; case Cloth cloth: activeness = _modifications.GetConstantValue(cloth, "m_IsEnable", cloth.enabled); break; - case Renderer cloth: - activeness = _modifications.GetConstantValue(cloth, "m_IsEnable", cloth.enabled); + case Collider collider: + activeness = _modifications.GetConstantValue(collider, "m_IsEnable", collider.enabled); break; - case Behaviour behaviour: - activeness = _modifications.GetConstantValue(behaviour, "m_IsEnable", behaviour.enabled); + case LODGroup lodGroup: + activeness = _modifications.GetConstantValue(lodGroup, "m_IsEnable", lodGroup.enabled); + break; + case Renderer renderer: + activeness = _modifications.GetConstantValue(renderer, "m_IsEnable", renderer.enabled); + break; + // components without isEnable + case CanvasRenderer _: + case Joint _: + case MeshFilter _: + case OcclusionArea _: + case OcclusionPortal _: + case ParticleSystem _: + case ParticleSystemForceField _: + case Rigidbody _: + case Rigidbody2D _: + case TextMesh _: + case Tree _: + case WindZone _: + case UnityEngine.XR.WSA.WorldAnchor _: + activeness = true; break; case Component _: + case null: + // fallback: all components type should be proceed with above switch activeness = null; break; - default: - throw new Exception($"Unexpected type: {component.GetType().Name}"); } if (activeness == false) return false; From 7687314ae66b64f00d457d362359fc3270c625a3 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 8 Sep 2023 15:07:08 +0900 Subject: [PATCH 5/5] docs(changelog): Inactivating parent GameObject or GameObject of component is not accounted in GC Objects --- CHANGELOG-PRERELEASE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index d18c657db..1467199b2 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog]. ### Fixed - GC Objects will remove VRC Contact Components `#438` +- Inactivating parent GameObject or GameObject of component is not accounted in GC Objects `#441` ### Security