Massive ALINE drawing slowdown when in Prefab editing mode

I noticed Unity slowing down while I was editing a prefab, and enabled the Profiler to narrow down the root cause, which turned out to be ALINE’s DrawingManager.DrawGizmos function, which gives bad performance even if all individual Gizmos are disabled:

You can see that the biggest offender is the repeated calls to StageUtility.GetStage, which is called for every ALINE-registered object even if that object’s Gizmos are disabled.

I rearranged DrawGizmos inner loop to early-out if possible before calling GetStage, which improved performance dramatically:

// ...snip...

MarkerDrawGizmos.Begin();
GizmoContext.drawingGizmos = true;
try {
    Draw.builder = gizmoBuilder;
    if (usingRenderPipeline) {
        for (int i = gizmoDrawers.Count - 1; i >= 0; i--) {
            var mono = gizmoDrawers[i] as MonoBehaviour;
#if UNITY_2022_1_OR_NEWER
            var gizmosEnabled = mono.isActiveAndEnabled && typeToGizmosEnabled[gizmoDrawers[i].GetType()];
#else
            var gizmosEnabled = mono.isActiveAndEnabled;
#endif
            if (!gizmosEnabled || (mono.hideFlags & HideFlags.HideInHierarchy) != 0) {
                continue;
            }


#if UNITY_EDITOR && UNITY_2020_1_OR_NEWER
            // True if the scene is in isolation mode (e.g. focusing on a single prefab) and this object is not part of that sub-stage
            var disabledDueToIsolationMode = isInNonMainStage && StageUtility.GetStage(mono.gameObject) != currentStage;
#else
            var disabledDueToIsolationMode = false;
#endif

            if (!disabledDueToIsolationMode) {
                continue;
            }

            try {
                gizmoDrawers[i].DrawGizmos();
            } catch (System.Exception e) {
                Debug.LogException(e, mono);
            }
        }
    } else {
        // ...snip...

Thanks! I’ll incoporate this into the next release.

1 Like