The Unity 2020.2 release features several optimizations that are now available for testing in beta. Read on to see where you can expect to see major speed-ups and get behind-the-scenes insights into what we’ve done to make these improvements.
Writing high-performance code is an integral part of efficient software development and has always been part of the development process at Unity. Two years ago, we took the bold step of forming a dedicated Optimization Team to focus on performance as a feature in its own right, which I now have the privilege of leading. See below for an overview of what we’ve got for the Unity 2020.2 release, and check out the Unity 2020.2 beta release notes for a list of all the other improvements.
Nested Prefab optimizations
The Optimization Team worked closely with the original developers of this feature, the Scene Management Team, on various optimizations to Nested Prefabs, including:
Reduced modifications of dynamic array of Properties Changed the sorting strategy for Modification array Changed to using a hash set for faster lookups
When loading instances of Prefabs, we apply modifications to the various properties that are different in the instance, compared to the original Prefab asset. These are PropertyModifications. When merging PropertyModifications, there are updates and insertions to a dynamic array, and as this struct is very large, this quickly becomes costly. By not erasing the PropertyModifications from the new Property array, but keeping track of the already updated properties, the method was sped up by 60x (from 3,300 ms to 54 ms on a test project).
When updating PropertyModifications, the modification list might not be sorted correctly, thus a new sort is needed. Previously this was done by sorting the old modifications into a new array. On a test project, this took 11 seconds. By instead doing the sorting in containers and pointing to the modifications, the sorting was brought down to 44 ms (250x faster), and in the case where no modifications were needed down to 11 ms (800x faster).
Additionally, searching for the propertyPath in the list of modifications was sped up by 50x (from 300 ms to 6 ms) by changing the container to a hash set. This gives an overall optimization in generating property diffs.
ScriptedImporter optimization Optimized a nested loop of linear searches in RegisterScriptedImporters
Database scalability tests showed that the performance of the Editor scripted importers registration function scaled badly as the number of importers being registered increased. The function was optimized by storing importers in a dictionary by file extension, to speed up searching for conflicts. The overall optimization was found to be between 12 to over 800 times faster when processing 100 to 5,000 importers (for overall improvement, see the graph on the right):
Editor workflow optimizations Reduced string copies and allocs in key Editor tasks Optimized find references in scenes by using temp memory
The team replaced lots of slow string memory allocations with temp memory labels, for strings that only exist within a single frame. In practice, most strings in Unity exist only as local variables within a single function call, so they can use a fast memory allocator. Most string utility functions now use temp memory.
Some of this work has already landed in 2020.1. Below are some graphs showing how many slow string allocationsContinue reading