Skip to content

Commit

Permalink
perf(UIComponent): determine stylesheets asset paths in the same thre…
Browse files Browse the repository at this point in the history
…ad as the load operation

UIComponent no longer waits for each StylesheetAttribute's path to be resolved before starting asset loading; both are now done simultaneously for each attribute.
  • Loading branch information
jonisavo committed Sep 18, 2022
1 parent a0dc173 commit 33dcef6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Assets/UIComponents/Addressables/AddressableAssetResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public async Task<bool> AssetExists(string assetPath)

var exists = locations.Count > 0;

AssetPathExistsCache.Add(assetPath, exists);
if (!AssetPathExistsCache.ContainsKey(assetPath))
AssetPathExistsCache.Add(assetPath, exists);

return exists;
}
Expand Down
35 changes: 25 additions & 10 deletions Assets/UIComponents/Core/UIComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,34 +250,49 @@ private async Task<VisualTreeAsset> GetLayout()

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

private readonly struct StyleSheetLoadTuple
{
public readonly string Path;
public readonly StyleSheet StyleSheet;

public StyleSheetLoadTuple(string path, StyleSheet styleSheet)
{
Path = path;
StyleSheet = styleSheet;
}
}

private async Task<StyleSheetLoadTuple> GetSingleStyleSheet(StylesheetAttribute stylesheetAttribute)
{
var assetPath = await stylesheetAttribute.GetAssetPathForComponent(this);
var styleSheet = await AssetResolver.LoadAsset<StyleSheet>(assetPath);

return new StyleSheetLoadTuple(assetPath, styleSheet);
}

private async Task<List<StyleSheet>> GetStyleSheets()
{
var stylesheetAttributes =
CacheDictionary[_componentType].StylesheetAttributes;
var stylesheetAttributeCount = stylesheetAttributes.Count;
var styleSheetLoadTasks =
new Task<StyleSheet>[stylesheetAttributeCount];
var styleSheetAssetPaths = new string[stylesheetAttributeCount];
new Task<StyleSheetLoadTuple>[stylesheetAttributeCount];

for (var i = 0; i < stylesheetAttributeCount; i++)
{
styleSheetAssetPaths[i] = await stylesheetAttributes[i].GetAssetPathForComponent(this);
var loadOperation = AssetResolver.LoadAsset<StyleSheet>(styleSheetAssetPaths[i]);
styleSheetLoadTasks[i] = loadOperation;
}
styleSheetLoadTasks[i] = GetSingleStyleSheet(stylesheetAttributes[i]);

await Task.WhenAll(styleSheetLoadTasks);

var loadedStyleSheets = new List<StyleSheet>(stylesheetAttributeCount);

for (var i = 0; i < stylesheetAttributeCount; i++)
foreach (var loadTask in styleSheetLoadTasks)
{
var styleSheet = styleSheetLoadTasks[i].Result;
var styleSheet = loadTask.Result.StyleSheet;

if (styleSheet == null)
{
Logger.LogError($"Could not find stylesheet {styleSheetAssetPaths[i]}", this);
Logger.LogError($"Could not find stylesheet {loadTask.Result.Path}", this);
continue;
}

Expand Down

0 comments on commit 33dcef6

Please sign in to comment.