-
Notifications
You must be signed in to change notification settings - Fork 17
opti: skip redundant avatar layer weight writes and cache animator layer indices #9701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
181 changes: 102 additions & 79 deletions
181
Explorer/Assets/DCL/AvatarRendering/AvatarShape/UnityInterface/AvatarBase.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 3 additions & 22 deletions
25
Explorer/Assets/DCL/Infrastructure/Utility/Animations/AnimatorEmoteLayers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,10 @@ | ||
| using DCL.ECSComponents; | ||
|
|
||
| namespace Utility.Animations | ||
| { | ||
| public static class AnimatorEmoteLayers | ||
| { | ||
| public const string BASE_LAYER = "Base Layer"; | ||
| public const string UPPER_BODY_LAYER = "Upper Body Layer"; | ||
|
|
||
| public static readonly string[] ALL_LAYERS = | ||
| { | ||
| BASE_LAYER, | ||
| UPPER_BODY_LAYER, | ||
| }; | ||
| // Unity's Animator always places the base layer at index 0. | ||
| public const int BASE_LAYER_INDEX = 0; | ||
|
|
||
| public static readonly string[] NON_BASE_LAYERS = | ||
| { | ||
| UPPER_BODY_LAYER, | ||
| }; | ||
|
|
||
| public static string GetFromEmoteMask(AvatarEmoteMask mask) => | ||
| mask switch | ||
| { | ||
| AvatarEmoteMask.AemFullBody => BASE_LAYER, | ||
| AvatarEmoteMask.AemUpperBody => UPPER_BODY_LAYER, | ||
| _ => BASE_LAYER, | ||
| }; | ||
| public const string UPPER_BODY_LAYER = "Upper Body Layer"; | ||
| } | ||
| } |
132 changes: 132 additions & 0 deletions
132
Explorer/Assets/DCL/Tests/PlayMode/PerformanceTests/AvatarLayerWeightGuardPerformanceTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| using DCL.AvatarRendering.AvatarShape.UnityInterface; | ||
| using NUnit.Framework; | ||
| using System.Diagnostics; | ||
| using System.Reflection; | ||
| using Unity.PerformanceTesting; | ||
| using Unity.Profiling; | ||
| using UnityEngine; | ||
| #if UNITY_EDITOR | ||
| using UnityEditor; | ||
| #endif | ||
|
|
||
| namespace DCL.Tests.PlayMode.PerformanceTests | ||
| { | ||
| /// <summary> | ||
| /// Verifies SetPointAtLayerWeight / SetRotationLayerWeight skip the native Animator.SetLayerWeight write when the | ||
| /// value is unchanged from the last call, and that ResetState clears the shadowed value so the next write after | ||
| /// a rebind (e.g. pool reuse) is not skipped. | ||
| /// </summary> | ||
| [Category("Performance")] | ||
| public class AvatarLayerWeightGuardPerformanceTest | ||
| { | ||
| #if UNITY_EDITOR | ||
| private const string AVATAR_BASE_TEST_ASSET_PATH = "Assets/DCL/AvatarRendering/AvatarShape/Tests/Instantiate/TestAssets/AvatarBase_TestAsset.prefab"; | ||
| private const string ANIMATOR_CONTROLLER_PATH = "Assets/DCL/AvatarRendering/AvatarShape/Assets/Animator/CharacterAnimator.controller"; | ||
|
|
||
| private GameObject avatarGameObject = null!; | ||
| private AvatarBase avatarBase = null!; | ||
| private Animator animator = null!; | ||
| private int pointAtIndex; | ||
| private int rotationIndex; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(AVATAR_BASE_TEST_ASSET_PATH); | ||
| Assert.IsNotNull(prefab, $"Could not load AvatarBase test prefab from {AVATAR_BASE_TEST_ASSET_PATH}"); | ||
|
|
||
| avatarGameObject = Object.Instantiate(prefab); | ||
| avatarBase = avatarGameObject.GetComponentInChildren<AvatarBase>(); | ||
| animator = avatarBase.AvatarAnimator; | ||
|
|
||
| var controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(ANIMATOR_CONTROLLER_PATH); | ||
| Assert.IsNotNull(controller, $"Could not load animator controller from {ANIMATOR_CONTROLLER_PATH}"); | ||
| animator.runtimeAnimatorController = controller; | ||
|
|
||
| typeof(AvatarBase).GetMethod("Awake", BindingFlags.NonPublic | BindingFlags.Instance)! | ||
| .Invoke(avatarBase, null); | ||
|
|
||
| // The test prefab leaves several serialized IK references unassigned (fileID: 0). ResetState() touches | ||
| // Armature (via ResetArmatureInclination), HipsConstraint and FeetIKRig, so each would throw | ||
| // UnassignedReferenceException. Wire throwaway objects into the private serialized backing fields via | ||
| // reflection (the same technique sibling AvatarBase tests use). The rigging component types are resolved | ||
| // from the property types, so the test needs no Animation-Rigging asmref. | ||
| var rigHolder = new GameObject("test-rig-holder"); | ||
| rigHolder.transform.SetParent(avatarGameObject.transform, false); | ||
|
|
||
| WireBackingField("Armature", rigHolder.transform); | ||
| WireBackingField("FeetIKRig", rigHolder.AddComponent(typeof(AvatarBase).GetProperty("FeetIKRig")!.PropertyType)); | ||
| WireBackingField("HipsConstraint", rigHolder.AddComponent(typeof(AvatarBase).GetProperty("HipsConstraint")!.PropertyType)); | ||
|
|
||
| pointAtIndex = animator.GetLayerIndex("RightPointAtHand"); | ||
| rotationIndex = animator.GetLayerIndex("Rotation"); | ||
| Assert.GreaterOrEqual(pointAtIndex, 0, "RightPointAtHand layer missing from controller"); | ||
| Assert.GreaterOrEqual(rotationIndex, 0, "Rotation layer missing from controller"); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| if (avatarGameObject != null) Object.DestroyImmediate(avatarGameObject); | ||
| } | ||
|
|
||
| private void WireBackingField(string propertyName, Object value) => | ||
| typeof(AvatarBase).GetField($"<{propertyName}>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance)! | ||
| .SetValue(avatarBase, value); | ||
|
|
||
| [Test] | ||
| [Performance] | ||
| public void RedundantLayerWeightWrites_AreElided_AndResetStateRearms() | ||
| { | ||
| avatarBase.SetPointAtLayerWeight(0.5f); | ||
| Assert.AreEqual(0.5f, animator.GetLayerWeight(pointAtIndex), 1e-4f); | ||
| avatarBase.SetPointAtLayerWeight(1f); | ||
| Assert.AreEqual(1f, animator.GetLayerWeight(pointAtIndex), 1e-4f); | ||
|
|
||
| avatarBase.SetRotationLayerWeight(0.25f); | ||
| Assert.AreEqual(0.25f, animator.GetLayerWeight(rotationIndex), 1e-4f); | ||
|
|
||
| avatarBase.SetPointAtLayerWeight(0.7f); | ||
| avatarBase.SetRotationLayerWeight(0.7f); | ||
| avatarBase.ResetState(); | ||
| Assert.AreNotEqual(0.7f, animator.GetLayerWeight(pointAtIndex), "Rebind should have reset the native weight"); | ||
|
|
||
| avatarBase.SetPointAtLayerWeight(0.7f); | ||
| Assert.AreEqual(0.7f, animator.GetLayerWeight(pointAtIndex), 1e-4f, "shadow was not re-armed by ResetState (point-at)"); | ||
| avatarBase.SetRotationLayerWeight(0.7f); | ||
| Assert.AreEqual(0.7f, animator.GetLayerWeight(rotationIndex), 1e-4f, "shadow was not re-armed by ResetState (rotation)"); | ||
|
|
||
| avatarBase.SetPointAtLayerWeight(0f); | ||
|
|
||
| const int ITER = 200_000; | ||
| long bestRedundant = long.MaxValue, bestAlternating = long.MaxValue; | ||
|
|
||
| for (int run = 0; run < 3; run++) | ||
| { | ||
| var sw = Stopwatch.StartNew(); | ||
| for (int k = 0; k < ITER; k++) avatarBase.SetPointAtLayerWeight(0f); | ||
| sw.Stop(); | ||
| bestRedundant = System.Math.Min(bestRedundant, sw.ElapsedTicks); | ||
|
|
||
| sw.Restart(); | ||
| for (int k = 0; k < ITER; k++) avatarBase.SetPointAtLayerWeight(k % 2); | ||
| sw.Stop(); | ||
| bestAlternating = System.Math.Min(bestAlternating, sw.ElapsedTicks); | ||
| } | ||
|
|
||
| Measure.Custom(new SampleGroup("RedundantWeightWrites", SampleUnit.Nanosecond), bestRedundant); | ||
| Measure.Custom(new SampleGroup("AlternatingWeightWrites", SampleUnit.Nanosecond), bestAlternating); | ||
|
|
||
| Assert.Less(bestRedundant, bestAlternating * 0.5, $"redundant writes ({bestRedundant}) should be far cheaper than alternating ({bestAlternating})"); | ||
|
|
||
| avatarBase.SetPointAtLayerWeight(0f); | ||
| ProfilerRecorder gcAlloc = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC.Alloc"); | ||
| Measure.Method(() => { for (int k = 0; k < 1000; k++) avatarBase.SetPointAtLayerWeight(0f); }) | ||
| .WarmupCount(5).MeasurementCount(10).GC().Run(); | ||
| long gcBytes = gcAlloc.LastValue; | ||
| gcAlloc.Dispose(); | ||
| Assert.AreEqual(0, gcBytes, $"redundant weight writes must be allocation-free, allocated {gcBytes} bytes"); | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
.../Assets/DCL/Tests/PlayMode/PerformanceTests/AvatarLayerWeightGuardPerformanceTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Non-blocking — timing assertion stability.
The
bestRedundant < bestAlternating * 0.5threshold (≥2× speedup) should hold comfortably since guarded writes skip the native call entirely, and best-of-3 mitigates noise. If this ever flakes on loaded CI runners, widening to0.75would still demonstrate the optimization without weakening the test's intent.(No change suggested — keeping current threshold is fine.)