fix: memory allocations due to boxing when creating a StackTraceEntry with disabled DebugMode.

fix: slow repeaded lookups of EditorPrefs for determining DebugMode
This commit is contained in:
Oliver Biwer 2023-10-15 10:01:11 +02:00
parent 37ead24dbb
commit 6b12b87079
3 changed files with 15 additions and 3 deletions

View File

@ -24,24 +24,31 @@ namespace UnityAtoms
public class BoolPreference : Preference<bool>
{
private bool? _cached = null;
public override bool Get()
{
if (_cached != null) return _cached.Value;
if (!EditorPrefs.HasKey(Key))
{
EditorPrefs.SetBool(Key, DefaultValue);
_cached = DefaultValue;
return _cached.Value;
}
return EditorPrefs.GetBool(Key);
_cached = EditorPrefs.GetBool(Key);
return _cached.Value;
}
public override void Set(bool value)
{
_cached = value;
EditorPrefs.SetBool(Key, value);
}
}
public static bool IsDebugModeEnabled { get => DEBUG_MODE_PREF.Get(); }
private static BoolPreference DEBUG_MODE_PREF = new BoolPreference() { DefaultValue = false, Key = "UnityAtoms.DebugMode" };
#if UNITY_2019_1_OR_NEWER

View File

@ -61,10 +61,14 @@ namespace UnityAtoms
}
#if UNITY_EDITOR
public static StackTraceEntry Create<T>(T obj, int skipFrames = 0) => AtomPreferences.IsDebugModeEnabled ? new StackTraceEntry(new StackTrace(skipFrames), obj) : null;
public static StackTraceEntry Create(object obj, int skipFrames = 0) => AtomPreferences.IsDebugModeEnabled ? new StackTraceEntry(new StackTrace(skipFrames), obj) : null;
public static StackTraceEntry Create(int skipFrames = 0) => AtomPreferences.IsDebugModeEnabled ? new StackTraceEntry(new StackTrace(skipFrames)) : null;
#else
public static StackTraceEntry Create<T>(T obj, int skipFrames = 0) => null;
public static StackTraceEntry Create(object obj, int skipFrames = 0) => null;
public static StackTraceEntry Create(int skipFrames = 0) => null;

View File

@ -11,6 +11,7 @@ namespace UnityAtoms
public static void AddStackTrace(int id, StackTraceEntry stackTrace)
{
if(stackTrace == null) return;
if (AtomPreferences.IsDebugModeEnabled)
{
GetStackTraces(id).Insert(0, stackTrace);