Skip to content

Commit

Permalink
Reduced debug overhead of Events (unity-atoms#435)
Browse files Browse the repository at this point in the history
* fix: memory allocations due to boxing when creating a StackTraceEntry with disabled DebugMode.
fix: slow repeaded lookups of EditorPrefs for determining DebugMode

* Cleaning up code that was affected by work on unity-atoms#434

- removed dead code
- fixed formatting

Commit-Type: Refactor
  • Loading branch information
soraphis committed Dec 19, 2023
1 parent 4576277 commit 7d9bb75
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Packages/Core/Runtime/Preferences/AtomsPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@ public abstract class Preference<T>

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
Expand Down
8 changes: 4 additions & 4 deletions Packages/Core/Runtime/StackTrace/StackTraceEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ private StackTraceEntry(StackTrace trace, object value)
}

#if UNITY_EDITOR
public static StackTraceEntry Create(object obj, int skipFrames = 0) => AtomPreferences.IsDebugModeEnabled ? new StackTraceEntry(new StackTrace(skipFrames), obj) : null;


public static StackTraceEntry Create<T>(T 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(object obj, int skipFrames = 0) => null;

public static StackTraceEntry Create<T>(T obj, int skipFrames = 0) => null;
public static StackTraceEntry Create(int skipFrames = 0) => null;
#endif

Expand Down
1 change: 1 addition & 0 deletions Packages/Core/Runtime/StackTrace/StackTraces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static class StackTraces

public static void AddStackTrace(int id, StackTraceEntry stackTrace)
{
if (stackTrace == null) return;
if (AtomPreferences.IsDebugModeEnabled)
{
GetStackTraces(id).Insert(0, stackTrace);
Expand Down

0 comments on commit 7d9bb75

Please sign in to comment.