#Source generator profiling?

1 messages · Page 1 of 1 (latest)

zealous lotus
#

Is there a way to profile how long does each source generator take when recompiling code? I'm using ECS 0.51 but I also have my own custom source generators in my project and I'd like to see how much time each source generator takes to try to improve iteration time.

idle sandal
#

hi @zealous lotus ! nice to meet you, here Nico from Unity :D. You can use any native profiler tool like Jetbrains dotTrace, Superliminal, or even ETW (in example, https://github.com/google/UIforETW/releases/tag/v1.56) to profile the execution of the roslyn compiler for the assembly-csharp.dll, there you will be able to see the cost of the source gen for your project's source code. consider that if you are using assembly definitons, replace any mention to assembly-chsarp in this post to the name of your assembly definition file.

In order to do this, you need to profile the dotnet.exe executable (usually located in C:\Program Files\dotnet\dotnet.exe) being executed with two arguments, the roslyn compiler dll path used by unity, and the RSP file path which contains build commands that unity used to build the dll. The roslyn dll is called csc.dll and is located in the unity install folder, usually under Data/DotNetSdkRoslyn, but this location might vary in unity versions. Then to locate the rsp file, in Unity 2021.3 using entities 0.51, you can open the editor.log file and look for Assembly-Csharp.rsp, which will show the entire path to the rsp file. Again, the location might vary between unity and entities versions. You can test your arguments are correct by executing the following command line in the root folder of your project: [DOTNETEXE PATH] [ROSLYN DLL] @[RSP PATH], in my case it was something like: "C:\Program Files\dotnet\dotnet.exe" "C:\Program Files\Unity\Hub\Editor\2021.3.4f1\Editor\Data\DotNetSdkRoslyn\csc.dll"@C:\Users\nico\Downloads\mytestproject\Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.rsp. Remember the quotation marks are needed whenever you have a space in a path while using windows command line.

#

With all this info, the profilers usually have three fields to fill, the path of the executable, which will be just the path to dotnet.exe, then the arguments, where you will need to put [ROSLYN DLL] @[RSP PATH] (in my case "C:\Program Files\Unity\Hub\Editor\2021.3.4f1\Editor\Data\DotNetSdkRoslyn\csc.dll"@C:\Users\nico\Downloads\mytestproject\Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.rsp) and the working directory, which would be your project folder.

Once you capture the profiling session, you can look for source gen function calls like the ones seen in the following screenshot from dottrace

#

if you need any further help dont hesitate to reply here 😄

zealous lotus
zealous lotus
#

Just wanted to mention it works great 👍