I’ve been looking into why the AOT Prebuild for visual scripting takes an insane amount of memory (up to 30GB for our project), and it turns out that it loads every asset in the database just to see if the main object in the asset implements IAotStubbable.
There’s a trivial fix to check the type of the asset before bothering to load it in. Just change the GetAllAssetsOfType function in AssetUtility.cs to this:
public static IEnumerable<T> GetAllAssetsOfType<T>()
{
if (typeof(UnityObject).IsAssignableFrom(typeof(T)))
{
return AssetDatabase.FindAssets($"t:{typeof(T).Name}")
.Select(AssetDatabase.GUIDToAssetPath)
.Select(AssetDatabase.LoadMainAssetAtPath)
.OfType<T>();
}
else
{
// GetAllAssetPaths is undocumented and sometimes returns
// paths that are outside the assets folder, hence the where filter.
var result = AssetDatabase.GetAllAssetPaths()
.Where(p => p.StartsWith("Assets"))
.Where(p => typeof(T).IsAssignableFrom(AssetDatabase.GetMainAssetTypeAtPath(p)))
.Select(AssetDatabase.LoadMainAssetAtPath)
.OfType<T>();
EditorUtility.UnloadUnusedAssetsImmediate();
return result;
}
}
This not only fixes the memory usage, but also speeds the process up massively.
Incidentally, I think there’s also a bug in AotPreBuilder.FindAllAssetStubs(). It loads every asset in the project that’s of type GameObject (so, all prefabs) and checks if it has any IAotStubbable components, but it only checks the root objects. I’m pretty sure GetComponents should actually be GetComponentsInChildren
Hope that helps someone! If anyone at Unity is still working on Bolt it’d be great if these fixes could be checked over, and integrated into the package.
Thanks!
Rob.