Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: deduplicate listener invocations in ShadowHierarchy #450

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Fixed

- [#450] Improved performance when a large number of object change events are generated (e.g. when exiting animation
mode)
- [#441] Fixed an issue where the preview object pickable status could get out of sync with the original

### Changed
Expand Down
119 changes: 89 additions & 30 deletions Editor/ChangeStream/ChangeStreamMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,58 @@ private static void OnChange(ref ObjectChangeEventStream stream)
Profiler.BeginSample("ChangeStreamMonitor.OnChange");

int length = stream.length;
for (int i = 0; i < length; i++)

using (ObjectWatcher.Instance.Hierarchy.SuspendEvents())
{
try
for (int i = 0; i < length; i++)
{
_handleEventSampler.Begin();
try
{
_handleEventSampler.Begin();

HandleEvent(stream, i);
}
catch (Exception e)
{
Debug.LogError($"Error handling event {i}: {e}");
}
finally
{
_handleEventSampler.End();
HandleEvent(stream, i);
}
catch (Exception e)
{
Debug.LogError($"Error handling event {i}: {e}");
}
finally
{
_handleEventSampler.End();
}
}
}

Profiler.BeginSample("ComputeContext.FlushInvalidates");
ComputeContext.FlushInvalidates();
Profiler.EndSample();

Profiler.EndSample();
}

private static void HandleEvent(ObjectChangeEventStream stream, int i)
private static TraceScope OpenTrace(ObjectChangeEventStream stream, int i)
{
var trace = TraceBuffer.RecordTraceEvent(
return TraceBuffer.RecordTraceEvent(
"ChangeStreamMonitor.HandleEvent",
(ev) => $"Handling event {ev.Arg0}",
ev => $"Handling event {ev.Arg0}",
stream.GetEventType(i),
level: TraceEventLevel.Trace
);

using (trace.Scope())
).Scope();
}

private static void HandleEvent(ObjectChangeEventStream stream, int i)
{
switch (stream.GetEventType(i))
{
case ObjectChangeKind.None: break;

case ObjectChangeKind.ChangeScene:
{
ObjectWatcher.Instance.Hierarchy.InvalidateAll();
using (OpenTrace(stream, i))
using (new ProfilerScope("ChangeScene"))
{
ObjectWatcher.Instance.Hierarchy.InvalidateAll();
}

break;
}
Expand All @@ -77,47 +87,76 @@ private static void HandleEvent(ObjectChangeEventStream stream, int i)
{
stream.GetCreateGameObjectHierarchyEvent(i, out var data);

ObjectWatcher.Instance.Hierarchy.FireGameObjectCreate(data.instanceId);
using (OpenTrace(stream, i))
using (new ProfilerScope("CreateGameObjectHierarchy"))
{
ObjectWatcher.Instance.Hierarchy.FireGameObjectCreate(data.instanceId);
}

break;
}

case ObjectChangeKind.ChangeGameObjectStructureHierarchy:
{
stream.GetChangeGameObjectStructureHierarchyEvent(i, out var data);

OnChangeGameObjectStructureHierarchy(data);
using (OpenTrace(stream, i))
using (new ProfilerScope("OnChangeGameObjectStructHierarchy"))
{
OnChangeGameObjectStructureHierarchy(data);
}

break;
}

case ObjectChangeKind.ChangeGameObjectStructure: // add/remove components
{
stream.GetChangeGameObjectStructureEvent(i, out var data);
OnChangeGameObjectStructure(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnChangeGameObjectStructure"))
{
OnChangeGameObjectStructure(data);
}

break;
}

case ObjectChangeKind.ChangeGameObjectParent:
{
stream.GetChangeGameObjectParentEvent(i, out var data);
OnChangeGameObjectParent(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnChangeGameObjectParent"))
{
OnChangeGameObjectParent(data);
}

break;
}

case ObjectChangeKind.ChangeGameObjectOrComponentProperties:
{
stream.GetChangeGameObjectOrComponentPropertiesEvent(i, out var data);
OnChangeGameObjectOrComponentProperties(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnChangeGameObjectOrComponentProperties"))
{
OnChangeGameObjectOrComponentProperties(data);
}

break;
}

case ObjectChangeKind.DestroyGameObjectHierarchy:
{
stream.GetDestroyGameObjectHierarchyEvent(i, out var data);
OnDestroyGameObjectHierarchy(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnDestroyGameObjectHierarchy"))
{
OnDestroyGameObjectHierarchy(data);
}

break;
}
Expand All @@ -126,31 +165,51 @@ private static void HandleEvent(ObjectChangeEventStream stream, int i)
case ObjectChangeKind.DestroyAssetObject:
{
stream.GetDestroyAssetObjectEvent(i, out var data);
OnDestroyAssetObject(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnDestroyAssetObject"))
{
OnDestroyAssetObject(data);
}

break;
}

case ObjectChangeKind.ChangeAssetObjectProperties:
{
stream.GetChangeAssetObjectPropertiesEvent(i, out var data);
OnChangeAssetObjectProperties(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnChangeAssetObjectProperties"))
{
OnChangeAssetObjectProperties(data);
}

break;
}

case ObjectChangeKind.UpdatePrefabInstances:
{
stream.GetUpdatePrefabInstancesEvent(i, out var data);
OnUpdatePrefabInstances(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnUpdatePrefabInstances"))
{
OnUpdatePrefabInstances(data);
}

break;
}

case ObjectChangeKind.ChangeChildrenOrder:
{
stream.GetChangeChildrenOrderEvent(i, out var data);
OnChangeChildrenOrder(data);

using (OpenTrace(stream, i))
using (new ProfilerScope("OnChangeChildrenOrder"))
{
OnChangeChildrenOrder(data);
}

break;
}
Expand Down
Loading
Loading