Profiling scopes

- Added profiling scope utility
- Added profiling scopes around some functions
- Testing StreamWriter and FileStream
This commit is contained in:
max 2023-07-16 19:58:37 +02:00
parent d859ad1af5
commit 6667f013bf
4 changed files with 248 additions and 169 deletions

View File

@ -82,33 +82,54 @@ private void CreateScenePartitions()
private void LoadScenePartitions(SortedSet<ulong> partitionIds)
{
if (!Data.HasCreatedPartitions) return;
string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> sceneData = new List<string>();
Data.LoadedScenePartitions.Clear();
// Add always load ids.
SortedSet<ulong> baseIds = GetAlwaysLoadIds();
foreach (var id in baseIds)
using (new ProfilerUtility.ProfilerScope($"{nameof(LoadScenePartitions)}"))
{
partitionIds.Add(id);
if (!Data.HasCreatedPartitions) return;
string scenePath = ScenePartitionUtils.GetScenePath(this);
Data.LoadedScenePartitions.Clear();
// Add always load ids.
SortedSet<ulong> baseIds = GetAlwaysLoadIds();
foreach (var id in baseIds)
{
partitionIds.Add(id);
}
// Clear file.
File.WriteAllText(scenePath, string.Empty);
// Create scene data.
try
{
using (FileStream outputStream = new FileStream(scenePath, FileMode.Append, FileAccess.Write))
{
foreach (ulong id in partitionIds)
{
ScenePartition p = Data.ScenePartitions[id];
using (FileStream inputStream = new FileStream(p.filePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
}
}
Data.LoadedScenePartitions.Add(p.id, p);
}
}
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}
AssetDatabase.Refresh();
}
// Create scene data.
foreach (ulong id in partitionIds)
{
ScenePartition p = Data.ScenePartitions[id];
sceneData.AddRange(File.ReadAllLines(p.filePath));
Data.LoadedScenePartitions.Add(p.id, p);
}
// Create scene.
File.WriteAllLines(scenePath, sceneData);
AssetDatabase.Refresh();
}
/// <summary>
@ -116,38 +137,41 @@ private void LoadScenePartitions(SortedSet<ulong> partitionIds)
/// </summary>
public void Save()
{
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
string pattern = @"&(\d+)";
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
// Read the data from the scene file.
string[] sceneData = File.ReadAllLines(scenePath);
// Split it into blocks.
int lastIndex = sceneData.Length;
for (int i = sceneData.Length - 1; i >= 0; i--)
using (new ProfilerUtility.ProfilerScope($"{nameof(Save)}"))
{
if (sceneData[i].StartsWith("---")) // --- is the start of a new yaml document.
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
string pattern = @"&(\d+)";
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
// Read the data from the scene file.
string[] sceneData = File.ReadAllLines(scenePath);
// Split it into blocks.
int lastIndex = sceneData.Length;
for (int i = sceneData.Length - 1; i >= 0; i--)
{
Match match = Regex.Match(sceneData[i], pattern);
if (match.Success)
if (sceneData[i].StartsWith("---")) // --- is the start of a new yaml document.
{
// Extract the file number
string id = match.Groups[1].Value;
Match match = Regex.Match(sceneData[i], pattern);
// Write data to disk.
File.WriteAllLines($"{dataPath}/{SceneName}-{id}.yaml", sceneData[i..lastIndex]);
if (match.Success)
{
// Extract the file number
string id = match.Groups[1].Value;
// Write data to disk.
File.WriteAllLines($"{dataPath}/{SceneName}-{id}.yaml", sceneData[i..lastIndex]);
}
lastIndex = i;
}
lastIndex = i;
}
}
// Write header to disk.
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", sceneData[0..lastIndex]);
// Write header to disk.
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", sceneData[0..lastIndex]);
}
}
/// <summary>
@ -155,51 +179,60 @@ public void Save()
/// </summary>
public void Unload()
{
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
GameObject[] gameObjects = scene.GetRootGameObjects();
for (int i = gameObjects.Length - 1; i >= 0; i--)
using (new ProfilerUtility.ProfilerScope($"{nameof(Unload)}"))
{
DestroyImmediate(gameObjects[i]);
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
GameObject[] gameObjects = scene.GetRootGameObjects();
for (int i = gameObjects.Length - 1; i >= 0; i--)
{
DestroyImmediate(gameObjects[i]);
}
EditorSceneManager.SaveScene(scene);
Data.LoadedScenePartitions.Clear();
AssetDatabase.Refresh();
}
EditorSceneManager.SaveScene(scene);
Data.LoadedScenePartitions.Clear();
AssetDatabase.Refresh();
}
private void DeleteLoadedPartitions()
{
if (!Data.HasLoadedPartitions) return;
foreach (KeyValuePair<ulong, ScenePartition> scenePartition in Data.LoadedScenePartitions)
using (new ProfilerUtility.ProfilerScope($"{nameof(DeleteLoadedPartitions)}"))
{
if (!File.Exists(scenePartition.Value.filePath)) continue;
if (!Data.HasLoadedPartitions) return;
File.Delete(scenePartition.Value.filePath);
foreach (KeyValuePair<ulong, ScenePartition> scenePartition in Data.LoadedScenePartitions)
{
if (!File.Exists(scenePartition.Value.filePath)) continue;
File.Delete(scenePartition.Value.filePath);
}
}
}
public void LoadPartitions(ulong[] ids)
{
SortedSet<ulong> partitionIds = new SortedSet<ulong>();
for (int i = 0; i < ids.Length; i++)
using (new ProfilerUtility.ProfilerScope($"{nameof(LoadPartitions)}"))
{
SortedSet<ulong> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
foreach (ulong c in connections)
{
partitionIds.Add(c);
}
}
SortedSet<ulong> partitionIds = new SortedSet<ulong>();
LoadScenePartitions(partitionIds);
for (int i = 0; i < ids.Length; i++)
{
SortedSet<ulong> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
foreach (ulong c in connections)
{
partitionIds.Add(c);
}
}
LoadScenePartitions(partitionIds);
}
}
private SortedSet<ulong> GetAlwaysLoadIds()
@ -220,76 +253,79 @@ private SortedSet<ulong> GetAlwaysLoadIds()
public void GenerateSceneGridData()
{
LoadAll();
if (!Data.HasCreatedPartitions) return;
Scene scene = EditorSceneManager.OpenScene(ScenePartitionUtils.GetScenePath(this), OpenSceneMode.Single);
GameObject[] rootGameObjects = scene.GetRootGameObjects();
Data.SceneGrid.Grid.Clear();
//// Maybe later switch to getting the data from disk instead of loading the scene and then unloading it again.
//foreach (GameObject gameObject in rootGameObjects)
//{
// // https://forum.unity.com/threads/how-to-get-the-local-identifier-in-file-for-scene-objects.265686/
// PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
// SerializedObject serializedObject = new SerializedObject(gameObject.transform);
// inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
// SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
// long localId = localIdProp.longValue;
// if (localId == 0)
// {
// if (PrefabUtility.IsPartOfPrefabInstance(gameObject))
// {
// GlobalObjectId id = GlobalObjectId.GetGlobalObjectIdSlow(gameObject); // We could use this funtion for all objects. Might be a bit slower but is also simple.
// localId = long.Parse(id.targetPrefabId.ToString());
// Debug.Log($"{id.assetGUID} | {id.identifierType} | {id.targetObjectId} | {id.targetPrefabId}");
// if (id.targetObjectId == 0 && id.targetPrefabId == 0)
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// }
// else
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// }
// if (!Data.ScenePartitions.ContainsKey(localId))
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// Data.SceneGrid.Insert(localId, gameObject.transform.position);
//}
GlobalObjectId[] ids = new GlobalObjectId[rootGameObjects.Length];
GlobalObjectId.GetGlobalObjectIdsSlow(rootGameObjects, ids);
for (int i = 0; i < ids.Length; i++)
using (new ProfilerUtility.ProfilerScope($"{nameof(GenerateSceneGridData)}"))
{
Debug.Log($"{ids[i].assetGUID} | {ids[i].identifierType} | {ids[i].targetObjectId} | {ids[i].targetPrefabId}");
LoadAll();
if (ids[i].targetPrefabId == 0) // 0 = no prefab.
if (!Data.HasCreatedPartitions) return;
Scene scene = EditorSceneManager.OpenScene(ScenePartitionUtils.GetScenePath(this), OpenSceneMode.Single);
GameObject[] rootGameObjects = scene.GetRootGameObjects();
Data.SceneGrid.Grid.Clear();
//// Maybe later switch to getting the data from disk instead of loading the scene and then unloading it again.
//foreach (GameObject gameObject in rootGameObjects)
//{
// // https://forum.unity.com/threads/how-to-get-the-local-identifier-in-file-for-scene-objects.265686/
// PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
// SerializedObject serializedObject = new SerializedObject(gameObject.transform);
// inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
// SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
// long localId = localIdProp.longValue;
// if (localId == 0)
// {
// if (PrefabUtility.IsPartOfPrefabInstance(gameObject))
// {
// GlobalObjectId id = GlobalObjectId.GetGlobalObjectIdSlow(gameObject); // We could use this funtion for all objects. Might be a bit slower but is also simple.
// localId = long.Parse(id.targetPrefabId.ToString());
// Debug.Log($"{id.assetGUID} | {id.identifierType} | {id.targetObjectId} | {id.targetPrefabId}");
// if (id.targetObjectId == 0 && id.targetPrefabId == 0)
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// }
// else
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// }
// if (!Data.ScenePartitions.ContainsKey(localId))
// {
// Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
// continue;
// }
// Data.SceneGrid.Insert(localId, gameObject.transform.position);
//}
GlobalObjectId[] ids = new GlobalObjectId[rootGameObjects.Length];
GlobalObjectId.GetGlobalObjectIdsSlow(rootGameObjects, ids);
for (int i = 0; i < ids.Length; i++)
{
Data.SceneGrid.Insert(ids[i].targetObjectId, rootGameObjects[i].transform.position, ScenePartitionSceneViewEditor.cellSize);
}
else
{
Data.SceneGrid.Insert(ids[i].targetPrefabId, rootGameObjects[i].transform.position, ScenePartitionSceneViewEditor.cellSize);
//Debug.Log($"{ids[i].assetGUID} | {ids[i].identifierType} | {ids[i].targetObjectId} | {ids[i].targetPrefabId}");
if (ids[i].targetPrefabId == 0) // 0 = no prefab.
{
Data.SceneGrid.Insert(ids[i].targetObjectId, rootGameObjects[i].transform.position, ScenePartitionSceneViewEditor.cellSize);
}
else
{
Data.SceneGrid.Insert(ids[i].targetPrefabId, rootGameObjects[i].transform.position, ScenePartitionSceneViewEditor.cellSize);
}
}
Unload();
}
Unload();
}
public void LoadCell(int gridId)

View File

@ -33,9 +33,12 @@ public static string GetScenePath(ScenePartitionSO scenePartitionSO)
public static SortedSet<ulong> FindDeeplyLinkedObjects(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId)
{
SortedSet<ulong> linkedObjects = new SortedSet<ulong>();
FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects);
return linkedObjects;
using (new ProfilerUtility.ProfilerScope($"{nameof(FindDeeplyLinkedObjects)}"))
{
SortedSet<ulong> linkedObjects = new SortedSet<ulong>();
FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects);
return linkedObjects;
}
}
private static void FindDeeplyLinkedObjectsRecursive(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId, SortedSet<ulong> linkedObjects)
@ -60,32 +63,35 @@ private static void FindDeeplyLinkedObjectsRecursive(SortedList<ulong, ScenePart
public static bool TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)
{
scenePartitionSO = null;
UnityEngine.SceneManagement.Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
if (activeScene == null) return false;
string filter = $"t:{nameof(ScenePartitionSO)}";
string[] assets = AssetDatabase.FindAssets(filter);
if (assets == null || assets.Length <= 0) return false;
for (int i = 0; i < assets.Length; i++)
using (new ProfilerUtility.ProfilerScope($"{nameof(TryGetScenePartitionSOForActiveScene)}"))
{
string path = AssetDatabase.GUIDToAssetPath(assets[i]);
ScenePartitionSO scenePartition = AssetDatabase.LoadAssetAtPath<ScenePartitionSO>(path);
scenePartitionSO = null;
if (scenePartition == null) continue;
UnityEngine.SceneManagement.Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
if (scenePartition.SceneName == activeScene.name)
if (activeScene == null) return false;
string filter = $"t:{nameof(ScenePartitionSO)}";
string[] assets = AssetDatabase.FindAssets(filter);
if (assets == null || assets.Length <= 0) return false;
for (int i = 0; i < assets.Length; i++)
{
scenePartitionSO = scenePartition;
return true;
}
}
string path = AssetDatabase.GUIDToAssetPath(assets[i]);
ScenePartitionSO scenePartition = AssetDatabase.LoadAssetAtPath<ScenePartitionSO>(path);
return false;
if (scenePartition == null) continue;
if (scenePartition.SceneName == activeScene.name)
{
scenePartitionSO = scenePartition;
return true;
}
}
return false;
}
}
}
}

View File

@ -0,0 +1,26 @@
using UnityEngine;
using UnityEngine.Profiling;
namespace VertexColor.ScenePartition
{
public static class ProfilerUtility
{
public class ProfilerScope : System.IDisposable
{
public ProfilerScope(string name)
{
Profiler.BeginSample(name);
}
public ProfilerScope(string name, Object target)
{
Profiler.BeginSample(name, target);
}
public void Dispose()
{
Profiler.EndSample();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7338cbb3bcd0a224fbfca184c7efcd7e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: