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

@ -81,13 +81,13 @@ private void CreateScenePartitions()
}
private void LoadScenePartitions(SortedSet<ulong> partitionIds)
{
using (new ProfilerUtility.ProfilerScope($"{nameof(LoadScenePartitions)}"))
{
if (!Data.HasCreatedPartitions) return;
string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> sceneData = new List<string>();
Data.LoadedScenePartitions.Clear();
// Add always load ids.
@ -97,24 +97,47 @@ private void LoadScenePartitions(SortedSet<ulong> partitionIds)
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];
sceneData.AddRange(File.ReadAllLines(p.filePath));
Data.LoadedScenePartitions.Add(p.id, p);
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);
}
}
// Create scene.
File.WriteAllLines(scenePath, sceneData);
Data.LoadedScenePartitions.Add(p.id, p);
}
}
}
catch (System.Exception ex)
{
Debug.LogException(ex);
}
AssetDatabase.Refresh();
}
}
/// <summary>
/// Convert scene to partitions and save them to disk.
/// </summary>
public void Save()
{
using (new ProfilerUtility.ProfilerScope($"{nameof(Save)}"))
{
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
@ -149,11 +172,14 @@ public void Save()
// Write header to disk.
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", sceneData[0..lastIndex]);
}
}
/// <summary>
/// Empty the scene and save it (so it has no changes in source control).
/// </summary>
public void Unload()
{
using (new ProfilerUtility.ProfilerScope($"{nameof(Unload)}"))
{
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
@ -173,8 +199,11 @@ public void Unload()
AssetDatabase.Refresh();
}
}
private void DeleteLoadedPartitions()
{
using (new ProfilerUtility.ProfilerScope($"{nameof(DeleteLoadedPartitions)}"))
{
if (!Data.HasLoadedPartitions) return;
@ -185,8 +214,11 @@ private void DeleteLoadedPartitions()
File.Delete(scenePartition.Value.filePath);
}
}
}
public void LoadPartitions(ulong[] ids)
{
using (new ProfilerUtility.ProfilerScope($"{nameof(LoadPartitions)}"))
{
SortedSet<ulong> partitionIds = new SortedSet<ulong>();
@ -201,6 +233,7 @@ public void LoadPartitions(ulong[] ids)
LoadScenePartitions(partitionIds);
}
}
private SortedSet<ulong> GetAlwaysLoadIds()
{
@ -219,6 +252,8 @@ private SortedSet<ulong> GetAlwaysLoadIds()
}
public void GenerateSceneGridData()
{
using (new ProfilerUtility.ProfilerScope($"{nameof(GenerateSceneGridData)}"))
{
LoadAll();
@ -277,7 +312,7 @@ public void GenerateSceneGridData()
for (int i = 0; i < ids.Length; i++)
{
Debug.Log($"{ids[i].assetGUID} | {ids[i].identifierType} | {ids[i].targetObjectId} | {ids[i].targetPrefabId}");
//Debug.Log($"{ids[i].assetGUID} | {ids[i].identifierType} | {ids[i].targetObjectId} | {ids[i].targetPrefabId}");
if (ids[i].targetPrefabId == 0) // 0 = no prefab.
{
@ -291,6 +326,7 @@ public void GenerateSceneGridData()
Unload();
}
}
public void LoadCell(int gridId)
{

View File

@ -32,11 +32,14 @@ public static string GetScenePath(ScenePartitionSO scenePartitionSO)
}
public static SortedSet<ulong> FindDeeplyLinkedObjects(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId)
{
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)
{
@ -59,6 +62,8 @@ private static void FindDeeplyLinkedObjectsRecursive(SortedList<ulong, ScenePart
}
public static bool TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)
{
using (new ProfilerUtility.ProfilerScope($"{nameof(TryGetScenePartitionSOForActiveScene)}"))
{
scenePartitionSO = null;
@ -88,4 +93,5 @@ public static bool TryGetScenePartitionSOForActiveScene(out ScenePartitionSO sce
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: