Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity Settings Manager version 1.0.3 package has major performance issue

Discussion in 'Editor & General Support' started by TG-jkhoo, Jun 30, 2021.

  1. TG-jkhoo

    TG-jkhoo

    Joined:
    May 10, 2021
    Posts:
    4
    In the Unity Settings Manager package, this is the Save method in PackageSettingsRepository.cs:


    Code (CSharp):
    1.         /// <summary>
    2.         /// Save all settings to their serialized state.
    3.         /// </summary>
    4.         /// <inheritdoc cref="ISettingsRepository.Save"/>
    5.  
    6.         public void Save()
    7.         {
    8.             Init();
    9.  
    10.             if (!File.Exists(path))
    11.             {
    12.                 var directory = Path.GetDirectoryName(path);
    13.                 Directory.CreateDirectory(directory);
    14.             }
    15.  
    16.             string newSettingsJson = EditorJsonUtility.ToJson(this, k_PrettyPrintJson);
    17.             bool areJsonsEqual = newSettingsJson == m_cachedJson;
    18.  
    19. #if UNITY_2019_3_OR_NEWER
    20.             if (!AssetDatabase.IsOpenForEdit(path) && areJsonsEqual == false)
    21.             {
    22.                 if (!AssetDatabase.MakeEditable(path))
    23.                 {
    24.                     Debug.LogWarning($"Could not save package settings to {path}");
    25.                     return;
    26.                 }
    27.             }
    28. #endif
    29.  
    30.             try
    31.             {
    32.                 if (!areJsonsEqual)
    33.                 {
    34.                     File.WriteAllText(path, newSettingsJson);
    35.                     m_cachedJson = newSettingsJson;
    36.                 }
    37.             }
    38.             catch (UnauthorizedAccessException)
    39.             {
    40.                 Debug.LogWarning($"Could not save package settings to {path}");
    41.             }
    42.         }
    The issue is that
    AssetDatabase.IsOpenForEdit
    can be a very slow synchronous blocking call when Unity is connected to Perforce Version Control. Especially if it's called repeatedly with every EditorGUI redraw, like in the Unity Polybrush package:

    Thankfully, there's a trivial fix. Skip the expensive call in the common case:

    OLD:
    if (!AssetDatabase.IsOpenForEdit(path) && areJsonsEqual == false)

    NEW:
    if (areJsonsEqual == false && !AssetDatabase.IsOpenForEdit(path))


    This simple change resolved my editor performance issue with using Polybrush while connected to Perforce. Can we get an update to the Settings Manager to integrate this fix?
     
  2. TG-jkhoo

    TG-jkhoo

    Joined:
    May 10, 2021
    Posts:
    4