After I upgraded our project to use Addressables 1.21.9 from 1.20.3, BuildLayoutGenerationTask became very slow. Before, it took only less than 6 seconds to finish, but after upgrading, the Editor stuck in BuildLayoutGenerationTask for over 10 minutes and I had to kill the Editor process.
After some investigation, I found it’s due to the following code:
public class BuildLayoutGenerationTask : IBuildTask
{
// ...
private LayoutLookupTables GenerateLookupTables(AddressableAssetsBuildContext aaContext)
{
// ...
foreach (BuildLayout.File file in lookup.Files.Values)
{
// ...
// Cache all object types from results to find type for when implicit asset
Dictionary<ObjectIdentifier, Type[]> objectTypes = new Dictionary<ObjectIdentifier, Type[]>(1024);
foreach (KeyValuePair<GUID, AssetResultData> assetResult in m_Results.AssetResults)
{
foreach (var resultEntry in assetResult.Value.ObjectTypes)
{
if(!objectTypes.ContainsKey(resultEntry.Key))
objectTypes.Add(resultEntry.Key, resultEntry.Value);
}
}
// ...
}
// ...
}
}
In our project, lookup.Files.Values has 26336 items, m_Results.AssetResults has 36242 items and objectTypes has 695450 items.
It seems the code of constructing objectTypes can be moved outside the loop:
public class BuildLayoutGenerationTask : IBuildTask
{
// ...
private LayoutLookupTables GenerateLookupTables(AddressableAssetsBuildContext aaContext)
{
// ...
// Cache all object types from results to find type for when implicit asset
Dictionary<ObjectIdentifier, Type[]> objectTypes = new Dictionary<ObjectIdentifier, Type[]>(1024);
foreach (KeyValuePair<GUID, AssetResultData> assetResult in m_Results.AssetResults)
{
foreach (var resultEntry in assetResult.Value.ObjectTypes)
{
if(!objectTypes.ContainsKey(resultEntry.Key))
objectTypes.Add(resultEntry.Key, resultEntry.Value);
}
}
foreach (BuildLayout.File file in lookup.Files.Values)
{
// ...
}
// ...
}
}
With this modification, BuildLayoutGenerationTask took about 80 seconds to finish, which is still worse than the old version, but is usable in our project.
Hope Unity developers can take a look at this issue.