Skip to content

Commit

Permalink
perf: Wrapped more profiler API calls with custom ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed May 20, 2024
1 parent 036b214 commit b69cdf6
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 116 deletions.
4 changes: 2 additions & 2 deletions Assets/LDtkUnity/Editor/CustomEditor/LDtkEntityIconFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ private Texture2D CreateSourceOverlayTexture()
//LDtkTextureSpriteSlicer slicer = new LDtkTextureSpriteSlicer(srcOverlay, tile.UnityRect, _importer.PixelsPerUnit);
//RectInt slice = slicer.ImageSlice.ToRectInt();

Profiler.BeginSample("CreateSlice");
LDtkProfiler.BeginSample("CreateSlice");
//Texture2D assetPreview = LDtkTextureUtility.CreateSlice(srcOverlay, slice);
Profiler.EndSample();
LDtkProfiler.EndSample();
//Texture2D assetPreview = AssetPreview.GetAssetPreview(sprite);
//Texture2D tex = sprite.ToTexture2D();

Expand Down
4 changes: 2 additions & 2 deletions Assets/LDtkUnity/Editor/CustomEditor/LDtkJsonEditorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ private byte[] GetFileHash()
FileStream stream = new FileStream(_assetPath, FileMode.Open, FileAccess.Read);
using (stream)
{
Profiler.BeginSample("ComputeHash");
LDtkProfiler.BeginSample("ComputeHash");
byte[] hash = sha1.ComputeHash(stream);
Profiler.EndSample();
LDtkProfiler.EndSample();
return hash;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class LDtkFindInScenes
{
public static List<T> FindInAllScenes<T>()
{
Profiler.BeginSample("FindInAllScenes");
LDtkProfiler.BeginSample("FindInAllScenes");
List<T> interfaces = new List<T>();

PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
Expand All @@ -41,7 +41,7 @@ public static List<T> FindInAllScenes<T>()
}
}

Profiler.EndSample();
LDtkProfiler.EndSample();
return interfaces;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ internal static class LDtkSceneDrawer
[DrawGizmo(GizmoType.NonSelected | GizmoType.Selected)]
private static void DrawGizmos(Transform objectTransform, GizmoType gizmoType)
{
Profiler.BeginSample("LDtkSceneGizmos");
LDtkProfiler.BeginSample("LDtkSceneGizmos");
DrawSceneGizmos(objectTransform);
Profiler.EndSample();
LDtkProfiler.EndSample();
}

private static void DrawSceneGizmos(Transform objectTransform)
{
if (objectTransform.TryGetComponent<LDtkComponentLevel>(out var lvl))
{
Profiler.BeginSample("LDtkSceneDrawerLevel");
LDtkProfiler.BeginSample("LDtkSceneDrawerLevel");
new LDtkLevelDrawer(lvl).OnDrawHandles();
Profiler.EndSample();
LDtkProfiler.EndSample();
return;
}

if (objectTransform.TryGetComponent<LDtkEntityDrawerComponent>(out var entity))
{
Profiler.BeginSample("LDtkSceneDrawerEntity");
LDtkProfiler.BeginSample("LDtkSceneDrawerEntity");
ProcessData(entity.EntityDrawer, LDtkSceneDrawerEntity.DrawEntity);
Profiler.EndSample();
LDtkProfiler.EndSample();

Profiler.BeginSample("LDtkSceneDrawerField");
LDtkProfiler.BeginSample("LDtkSceneDrawerField");
foreach (LDtkFieldDrawerData data in entity.FieldDrawers)
{
ProcessData(data, LDtkSceneDrawerField.DrawField);
}
Profiler.EndSample();
LDtkProfiler.EndSample();

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ static LDtkWorldDepthGUIDrawer()
}
private static void CustomOnSceneGUI(SceneView view)
{
Profiler.BeginSample("WorldDepthGUI");
LDtkProfiler.BeginSample("WorldDepthGUI");
WorldDepthGUI.Draw();
Profiler.EndSample();
LDtkProfiler.EndSample();
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ private void TryRestructureArray(T[] defs)

string[] keepers = GetAssetKeysFromDefs(defs);

Profiler.BeginSample("ShouldRestructureArray");
LDtkProfiler.BeginSample("ShouldRestructureArray");
bool shouldRestructureArray = ShouldRestructureArray(keepers);
Profiler.EndSample();
LDtkProfiler.EndSample();

if (!shouldRestructureArray)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public override void Draw()

private Texture2D GetIcon()
{
Profiler.BeginSample("GetEntityIcon");
LDtkProfiler.BeginSample("GetEntityIcon");
Texture2D copyTexture = _iconFactory.GetIcon();
Profiler.EndSample();
LDtkProfiler.EndSample();
return copyTexture;
}
}
Expand Down
36 changes: 18 additions & 18 deletions Assets/LDtkUnity/Editor/ParsedField/LDtkFieldsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public void SetEntityFieldsComponent()
fields = _instance.AddComponent<LDtkFields>();
}

Profiler.BeginSample("GetFields");
LDtkProfiler.BeginSample("GetFields");
LDtkField[] fieldData = GetFields();
Profiler.EndSample();
LDtkProfiler.EndSample();

fields.SetFieldData(fieldData);

Expand All @@ -59,9 +59,9 @@ private LDtkField GetFieldFromInstance(FieldInstance fieldInstance)
FieldDefinition def = fieldInstance.Definition;
bool isArray = def.IsArray;

Profiler.BeginSample($"GetObjectElements {fieldInstance.Identifier}");
LDtkProfiler.BeginSample($"GetObjectElements {fieldInstance.Identifier}");
LDtkFieldElement[] elements = GetObjectElements(fieldInstance, isArray);
Profiler.EndSample();
LDtkProfiler.EndSample();

LDtkDefinitionObjectField defObj = null;

Expand All @@ -77,19 +77,19 @@ private LDtkField GetFieldFromInstance(FieldInstance fieldInstance)

private LDtkFieldElement[] GetObjectElements(FieldInstance fieldInstance, bool isArray)
{
Profiler.BeginSample($"GetElements");
LDtkProfiler.BeginSample($"GetElements");
object[] elements = GetElements(fieldInstance, isArray);
Profiler.EndSample();
LDtkProfiler.EndSample();

Profiler.BeginSample($"new LDtkFieldElements");
LDtkProfiler.BeginSample($"new LDtkFieldElements");
LDtkFieldElement[] fieldElements = new LDtkFieldElement[elements.Length];
for (int i = 0; i < fieldElements.Length; i++)
{
fieldElements[i] = new LDtkFieldElement(elements[i], fieldInstance);
}
Profiler.EndSample();
LDtkProfiler.EndSample();

Profiler.BeginSample($"Setup point transforms");
LDtkProfiler.BeginSample($"Setup point transforms");
//setup transforms for the points so that they are easy to follow along on
if (fieldInstance.IsPoint)
{
Expand All @@ -101,7 +101,7 @@ private LDtkFieldElement[] GetObjectElements(FieldInstance fieldInstance, bool i
newPoint.SetParent(_instance.transform, true);
}
}
Profiler.EndSample();
LDtkProfiler.EndSample();


return fieldElements;
Expand All @@ -111,9 +111,9 @@ public object[] GetElements(FieldInstance fieldInstance, bool isArray)
{
if (isArray)
{
Profiler.BeginSample("GetArray");
LDtkProfiler.BeginSample("GetArray");
Array array = GetArray(fieldInstance);
Profiler.EndSample();
LDtkProfiler.EndSample();

object[] objArray = new object[array.Length];
for (int i = 0; i < array.Length; i++)
Expand All @@ -123,9 +123,9 @@ public object[] GetElements(FieldInstance fieldInstance, bool isArray)
return objArray;
}

Profiler.BeginSample("GetSingle");
LDtkProfiler.BeginSample("GetSingle");
object single = GetSingle(fieldInstance);
Profiler.EndSample();
LDtkProfiler.EndSample();

return new[] { single };
}
Expand All @@ -145,15 +145,15 @@ private Array GetArray(FieldInstance fieldInstance)
}

//parse em
Profiler.BeginSample("CopyArray");
LDtkProfiler.BeginSample("CopyArray");
object[] srcObjs = new object[objs.Count];
for (int i = 0; i < objs.Count; i++)
{
srcObjs[i] = GetParsedValue(fieldInstance, objs[i]);
}
Profiler.EndSample();
LDtkProfiler.EndSample();

Profiler.BeginSample("CopyArray");
LDtkProfiler.BeginSample("CopyArray");
Array array = new object[srcObjs.Length];
try
{
Expand All @@ -164,7 +164,7 @@ private Array GetArray(FieldInstance fieldInstance)
string srcObjsStrings = string.Join(", ", srcObjs);
LDtkDebug.LogError($"Issue copying array for field instance \"{fieldInstance.Identifier}\"; LDtk type: {fieldInstance.Type}, ParsedObjects: {srcObjsStrings}. {e}");
}
Profiler.EndSample();
LDtkProfiler.EndSample();

return array;
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/LDtkUnity/Editor/PropertyDrawer/LDtkFieldDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
Init(property, label);

Profiler.BeginSample("LDtkFieldDrawer.OnGUI");
LDtkProfiler.BeginSample("LDtkFieldDrawer.OnGUI");
Draw(position);
Profiler.EndSample();
LDtkProfiler.EndSample();
}

private void Draw(Rect position)
Expand All @@ -80,7 +80,7 @@ private void Draw(Rect position)
/// </summary>
public bool DrawDefAndField(Rect position, Rect labelRect, SerializedProperty fieldProp, GUIContent label, bool includeChildren = false)
{
Profiler.BeginSample("LDtkFieldDrawer.DrawDefAndField");
LDtkProfiler.BeginSample("LDtkFieldDrawer.DrawDefAndField");

const float desiredObjectWidth = 35;
float objectWidth = Mathf.Min(desiredObjectWidth, position.width - desiredObjectWidth * 0.83f);
Expand All @@ -107,7 +107,7 @@ public bool DrawDefAndField(Rect position, Rect labelRect, SerializedProperty fi
EditorGUI.ObjectField(objRect, _defProp.objectReferenceValue, typeof(LDtkDefinitionObjectField), true);
}

Profiler.EndSample();
LDtkProfiler.EndSample();
return true;
}
}
Expand Down
24 changes: 12 additions & 12 deletions Assets/LDtkUnity/Editor/PropertyDrawer/LDtkFieldElementDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Profiler.BeginSample("LDtkFieldElementDrawer.OnGUI");
LDtkProfiler.BeginSample("LDtkFieldElementDrawer.OnGUI");
using (new EditorGUIUtility.IconSizeScope(Vector2.one * 14))
{
Draw(position, property, label);
}
Profiler.EndSample();
LDtkProfiler.EndSample();
}

private void Draw(Rect position, SerializedProperty property, GUIContent label)
{
Profiler.BeginSample("LDtkFieldElementDrawer.Draw");
LDtkProfiler.BeginSample("LDtkFieldElementDrawer.Draw");

TryInitTex();

Expand All @@ -89,7 +89,7 @@ private void Draw(Rect position, SerializedProperty property, GUIContent label)


_valueProp = GetPropertyToDraw(property, type);
Profiler.EndSample();
LDtkProfiler.EndSample();

if (_valueProp == null)
{
Expand All @@ -108,9 +108,9 @@ private void Draw(Rect position, SerializedProperty property, GUIContent label)
return;
}

Profiler.BeginSample("LDtkFieldElementDrawer.PropertyField");
LDtkProfiler.BeginSample("LDtkFieldElementDrawer.PropertyField");
EditorGUI.PropertyField(_position, _valueProp, label);
Profiler.EndSample();
LDtkProfiler.EndSample();
}

private static void TryInitTex()
Expand All @@ -127,11 +127,11 @@ private static void TryInitTex()

private bool TryDrawNullable(GUIContent label, LDtkFieldType type)
{
Profiler.BeginSample("LDtkFieldElementDrawer.TryDrawNullable");
LDtkProfiler.BeginSample("LDtkFieldElementDrawer.TryDrawNullable");
if (!CanBeNull(type))
{

Profiler.EndSample();
LDtkProfiler.EndSample();
return false;
}

Expand All @@ -143,7 +143,7 @@ private bool TryDrawNullable(GUIContent label, LDtkFieldType type)
if (!IsNull(type))
{
EditorGUI.PropertyField(boolRect, _isNotNullProp, NullToggle);
Profiler.EndSample();
LDtkProfiler.EndSample();
return false;
}

Expand All @@ -153,7 +153,7 @@ private bool TryDrawNullable(GUIContent label, LDtkFieldType type)
EditorGUI.LabelField(_labelRect, label);
EditorGUI.LabelField(_fieldRect, nullContent);

Profiler.EndSample();
LDtkProfiler.EndSample();
return true;
}

Expand Down Expand Up @@ -206,7 +206,7 @@ private bool DrawSlider(GUIContent label, bool isInt)

private bool DrawMultiline(GUIContent label)
{
Profiler.BeginSample("LDtkFieldElementDrawer.DrawMultiline");
LDtkProfiler.BeginSample("LDtkFieldElementDrawer.DrawMultiline");

GUIStyle labelStyle = _valueProp.prefabOverride ? EditorStyles.boldLabel : EditorStyles.label;

Expand All @@ -219,7 +219,7 @@ private bool DrawMultiline(GUIContent label)

_valueProp.stringValue = EditorGUI.TextArea(_fieldRect, _valueProp.stringValue, textAreaStyle);

Profiler.EndSample();
LDtkProfiler.EndSample();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions Assets/LDtkUnity/Editor/PropertyDrawer/LDtkFieldsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public override void OnInspectorGUI()

EditorGUILayout.HelpBox(HelpBox, true);

Profiler.BeginSample("LDtkFieldsEditor DrawElements");
LDtkProfiler.BeginSample("LDtkFieldsEditor DrawElements");
for (int i = 0; i < _elements.Length; i++)
{
SerializedProperty prop = _elements[i];
EditorGUILayout.PropertyField(prop);
}
Profiler.EndSample();
LDtkProfiler.EndSample();

serializedObject.ApplyModifiedProperties();
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public sealed override void OnImportAsset(AssetImportContext ctx)
}

//serialize dependencies to display them in the inspector for easier dependency tracking.
Profiler.BeginSample("SerializeStringDependencies");
LDtkProfiler.BeginSample("SerializeStringDependencies");
LDtkDependencyCache.Set(assetPath, GetGatheredDependencies());
Profiler.EndSample();
LDtkProfiler.EndSample();
}

private void MainImport()
Expand Down
Loading

0 comments on commit b69cdf6

Please sign in to comment.