Skip to content

Commit

Permalink
Merge pull request #43 from jonisavo/test/post-hierarchy-perf
Browse files Browse the repository at this point in the history
test: measure performance of post-hierarchy work
  • Loading branch information
jonisavo committed Sep 18, 2022
2 parents 07af5a7 + 0a8f9e4 commit 8a301f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Assets/UIComponents.Benchmarks/BenchmarkUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace UIComponents.Benchmarks
{
public static class BenchmarkUtils
{
public const string Version = "0.22.0.0";
public const string Version = "0.22.1.0";

private static SampleGroup[] GetProfilerMarkers()
{
return new[]
{
new SampleGroup("UIComponent.DependencySetup"),
new SampleGroup("UIComponent.CacheSetup"),
new SampleGroup("UIComponent.LayoutAndStylesSetup")
new SampleGroup("UIComponent.PostHierarchySetup"),
};
}

Expand Down
79 changes: 41 additions & 38 deletions Assets/UIComponents/Core/UIComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class UIComponent : VisualElement
{
private static readonly Dictionary<Type, UIComponentCache> CacheDictionary =
new Dictionary<Type, UIComponentCache>();

/// <summary>
/// Clears the cache of the UIComponent. Used primarily for testing.
/// </summary>
Expand All @@ -37,7 +37,7 @@ public abstract class UIComponent : VisualElement
{
CacheDictionary.Remove(typeof(TComponent));
}

internal static bool TryGetCache<TComponent>(out UIComponentCache cache) where TComponent : UIComponent
{
return CacheDictionary.TryGetValue(typeof(TComponent), out cache);
Expand All @@ -48,7 +48,7 @@ public abstract class UIComponent : VisualElement
/// Defaults to <see cref="ResourcesAssetResolver"/>.
/// </summary>
public readonly IAssetResolver AssetResolver;

/// <summary>
/// Whether the UIComponent has been fully initialized.
/// </summary>
Expand All @@ -68,64 +68,65 @@ public abstract class UIComponent : VisualElement
private readonly DependencyInjector _dependencyInjector;

private readonly Type _componentType;

private readonly TaskCompletionSource<UIComponent> _initCompletionSource =
new TaskCompletionSource<UIComponent>();

private static readonly ProfilerMarker DependencySetupProfilerMarker =
new ProfilerMarker("UIComponent.DependencySetup");
private static readonly ProfilerMarker CacheSetupProfilerMarker =
new ProfilerMarker("UIComponent.CacheSetup");
private static readonly ProfilerMarker LayoutAndStylesSetupProfilerMarker =
new ProfilerMarker("UIComponent.LayoutAndStylesSetup");
private static readonly ProfilerMarker PostHierarchySetupProfilerMarker =
new ProfilerMarker("UIComponent.PostHierarchySetup");

/// <summary>
/// UIComponent's constructor loads the configured layout and stylesheets.
/// </summary>
protected UIComponent()
{
CacheSetupProfilerMarker.Begin();

_componentType = GetType();
if (!CacheDictionary.ContainsKey(_componentType))
CacheDictionary.Add(_componentType, new UIComponentCache(_componentType));

CacheSetupProfilerMarker.End();

DependencySetupProfilerMarker.Begin();

_dependencyInjector = DiContext.Current.GetInjector(_componentType);
AssetResolver = Provide<IAssetResolver>();
Logger = Provide<IUIComponentLogger>();
PopulateProvideFields();

DependencySetupProfilerMarker.End();

Initialize();
}

private async void Initialize()
{
LayoutAndStylesSetupProfilerMarker.Begin();

var layoutTask = GetLayout();
var stylesTask = GetStyleSheets();

await Task.WhenAll(layoutTask, stylesTask);

var layoutAsset = layoutTask.Result;
var styles = stylesTask.Result;

LoadLayout(layoutAsset);
LoadStyles(styles);
LayoutAndStylesSetupProfilerMarker.End();

PostHierarchySetupProfilerMarker.Begin();

ApplyEffects();
PopulateQueryFields();
RegisterEventInterfaceCallbacks();

PostHierarchySetupProfilerMarker.End();

OnInit();

Initialized = true;
_initCompletionSource.SetResult(this);
}
Expand All @@ -134,25 +135,25 @@ private void RegisterEventInterfaceCallbacks()
{
if (this is IOnAttachToPanel onAttachToPanel)
RegisterCallback<AttachToPanelEvent>(onAttachToPanel.OnAttachToPanel);

if (this is IOnDetachFromPanel onDetachFromPanel)
RegisterCallback<DetachFromPanelEvent>(onDetachFromPanel.OnDetachFromPanel);

if (this is IOnGeometryChanged onGeometryChanged)
RegisterCallback<GeometryChangedEvent>(onGeometryChanged.OnGeometryChanged);

if (this is IOnMouseEnter onMouseEnter)
RegisterCallback<MouseEnterEvent>(onMouseEnter.OnMouseEnter);

if (this is IOnMouseLeave onMouseLeave)
RegisterCallback<MouseLeaveEvent>(onMouseLeave.OnMouseLeave);

#if UNITY_2020_3_OR_NEWER
if (this is IOnClick onClick)
RegisterCallback<ClickEvent>(onClick.OnClick);
#endif
}

private void LoadLayout([CanBeNull] VisualTreeAsset layoutAsset)
{
if (layoutAsset != null)
Expand All @@ -175,20 +176,20 @@ private void LoadStyles(IList<StyleSheet> styles)
/// In this case, this method will be called before the constructor returns.
/// </remarks>
public virtual void OnInit() {}

/// <returns>A Task which resolves when the component has initialized</returns>
[Obsolete("Use InitializationTask instead.")]
public Task<UIComponent> WaitForInitialization()
{
return _initCompletionSource.Task;
}

/// <returns>An enumerator which yields when the component has initialized</returns>
public IEnumerator WaitForInitializationEnumerator()
{
yield return _initCompletionSource.Task.AsEnumerator();
}

/// <summary>
/// Returns an IEnumerable of all of the asset paths configured
/// for the component.
Expand All @@ -202,7 +203,7 @@ public IEnumerable<string> GetAssetPaths()
for (var i = 0; i < assetPathCount; i++)
yield return assetPathAttributes[i].Path;
}

/// <summary>
/// Returns the component's type's name.
/// </summary>
Expand Down Expand Up @@ -237,22 +238,23 @@ public string GetTypeName()
{
return _dependencyInjector.TryProvide(out instance);
}

private Task<VisualTreeAsset> GetLayout()
{
var layoutAttribute = CacheDictionary[_componentType].LayoutAttribute;

if (layoutAttribute == null)
return Task.FromResult<VisualTreeAsset>(null);

var assetPath = layoutAttribute.GetAssetPathForComponent(this);

return AssetResolver.LoadAsset<VisualTreeAsset>(assetPath);
}

private async Task<List<StyleSheet>> GetStyleSheets()
{
var stylesheetAttributes = CacheDictionary[_componentType].StylesheetAttributes;
var stylesheetAttributes =
CacheDictionary[_componentType].StylesheetAttributes;
var stylesheetAttributeCount = stylesheetAttributes.Count;
var styleSheetLoadTasks =
new Task<StyleSheet>[stylesheetAttributeCount];
Expand All @@ -264,7 +266,7 @@ private async Task<List<StyleSheet>> GetStyleSheets()
var loadOperation = AssetResolver.LoadAsset<StyleSheet>(styleSheetAssetPaths[i]);
styleSheetLoadTasks[i] = loadOperation;
}

await Task.WhenAll(styleSheetLoadTasks);

var loadedStyleSheets = new List<StyleSheet>(stylesheetAttributeCount);
Expand All @@ -278,6 +280,7 @@ private async Task<List<StyleSheet>> GetStyleSheets()
Logger.LogError($"Could not find stylesheet {styleSheetAssetPaths[i]}", this);
continue;
}

loadedStyleSheets.Add(styleSheet);
}

Expand All @@ -288,7 +291,7 @@ private void ApplyEffects()
{
var effectAttributes = CacheDictionary[_componentType].EffectAttributes;
var effectAttributeCount = effectAttributes.Count;

for (var i = 0; i < effectAttributeCount; i++)
effectAttributes[i].Apply(this);
}
Expand All @@ -307,7 +310,7 @@ private void PopulateQueryFields()
var concreteType = TypeUtils.GetConcreteType(fieldType);

var results = new List<VisualElement>();

for (var i = 0; i < queryAttributes.Length; i++)
{
#if !UNITY_2020_3_OR_NEWER
Expand All @@ -319,7 +322,7 @@ private void PopulateQueryFields()
#endif
queryAttributes[i].Query(this, results);
}

results.RemoveAll(result => !concreteType.IsInstanceOfType(result));

object value = null;
Expand All @@ -344,10 +347,10 @@ private void PopulateProvideFields()
foreach (var fieldInfo in provideAttributeDictionary.Keys)
{
var fieldType = fieldInfo.FieldType;

if (provideAttributeDictionary[fieldInfo].CastFrom != null)
fieldType = provideAttributeDictionary[fieldInfo].CastFrom;

object value;

try
Expand Down

0 comments on commit 8a301f3

Please sign in to comment.