Skip to content

Commit

Permalink
perf(UIComponent): reduce allocations while loading style sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed May 8, 2023
1 parent 1d2edd4 commit e163110
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Assets/UIComponents.Benchmarks/BenchmarkUtils.cs
Expand Up @@ -20,6 +20,7 @@ public static void MeasureComponentInitWithColdCache<TComponent>() where TCompon
Measure.Method(async () =>
{
var component = new TComponent();
component.Initialize();
await component.InitializationTask;
})
.SetUp(() =>
Expand All @@ -40,6 +41,7 @@ public static void MeasureComponentInitWithWarmCache<TComponent>() where TCompon
Measure.Method(async () =>
{
var component = new TComponent();
component.Initialize();
await component.InitializationTask;
})
.SampleGroup(new SampleGroup("Warm Cache Time"))
Expand Down
40 changes: 18 additions & 22 deletions Assets/UIComponents/Core/UIComponent.cs
Expand Up @@ -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<Task>();

Expand Down Expand Up @@ -209,29 +219,15 @@ protected virtual Task<StyleSheetLoadTuple>[] UIC_StartStyleSheetLoad()
return Array.Empty<Task<StyleSheetLoadTuple>>();
}

private async Task<List<StyleSheet>> GetStyleSheets()
private Task<StyleSheetLoadTuple[]> GetStyleSheets()
{
var styleSheetLoadTasks =
UIC_StartStyleSheetLoad();

if (styleSheetLoadTasks.Length == 0)
return Task.FromResult(Array.Empty<StyleSheetLoadTuple>());

await Task.WhenAll(styleSheetLoadTasks);

var loadedStyleSheets = new List<StyleSheet>(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() {}
Expand Down

0 comments on commit e163110

Please sign in to comment.