From e163110842d9000dc054ab9432c67e68e268fd7a Mon Sep 17 00:00:00 2001 From: Joni Savolainen Date: Mon, 8 May 2023 19:15:25 +0300 Subject: [PATCH] perf(UIComponent): reduce allocations while loading style sheets --- .../UIComponents.Benchmarks/BenchmarkUtils.cs | 2 + Assets/UIComponents/Core/UIComponent.cs | 40 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/Assets/UIComponents.Benchmarks/BenchmarkUtils.cs b/Assets/UIComponents.Benchmarks/BenchmarkUtils.cs index 75a8c010..c53d1425 100644 --- a/Assets/UIComponents.Benchmarks/BenchmarkUtils.cs +++ b/Assets/UIComponents.Benchmarks/BenchmarkUtils.cs @@ -20,6 +20,7 @@ public static void MeasureComponentInitWithColdCache() where TCompon Measure.Method(async () => { var component = new TComponent(); + component.Initialize(); await component.InitializationTask; }) .SetUp(() => @@ -40,6 +41,7 @@ public static void MeasureComponentInitWithWarmCache() where TCompon Measure.Method(async () => { var component = new TComponent(); + component.Initialize(); await component.InitializationTask; }) .SampleGroup(new SampleGroup("Warm Cache Time")) diff --git a/Assets/UIComponents/Core/UIComponent.cs b/Assets/UIComponents/Core/UIComponent.cs index 38e3117c..e0eb85da 100644 --- a/Assets/UIComponents/Core/UIComponent.cs +++ b/Assets/UIComponents/Core/UIComponent.cs @@ -99,13 +99,23 @@ public async void Initialize() await Task.WhenAll(layoutTask, stylesTask); var layoutAsset = layoutTask.Result; - var styles = stylesTask.Result; + var styleTuples = stylesTask.Result; if (layoutAsset != null) layoutAsset.CloneTree(this); - for (var i = 0; i < styles.Count; i++) - styleSheets.Add(styles[i]); + for (var i = 0; i < styleTuples.Length; i++) + { + var tuple = styleTuples[i]; + + if (tuple.StyleSheet == null) + { + Logger.LogError($"Could not find stylesheet {tuple.Path}", this); + continue; + } + + styleSheets.Add(tuple.StyleSheet); + } var childInitializationTasks = new List(); @@ -209,29 +219,15 @@ protected virtual Task[] UIC_StartStyleSheetLoad() return Array.Empty>(); } - private async Task> GetStyleSheets() + private Task GetStyleSheets() { var styleSheetLoadTasks = UIC_StartStyleSheetLoad(); + + if (styleSheetLoadTasks.Length == 0) + return Task.FromResult(Array.Empty()); - await Task.WhenAll(styleSheetLoadTasks); - - var loadedStyleSheets = new List(styleSheetLoadTasks.Length); - - foreach (var loadTask in styleSheetLoadTasks) - { - var styleSheet = loadTask.Result.StyleSheet; - - if (styleSheet == null) - { - Logger.LogError($"Could not find stylesheet {loadTask.Result.Path}", this); - continue; - } - - loadedStyleSheets.Add(styleSheet); - } - - return loadedStyleSheets; + return Task.WhenAll(styleSheetLoadTasks); } protected virtual void UIC_ApplyEffects() {}