Skip to content

Instantly share code, notes, and snippets.

@hybridherbst
Created January 15, 2021 15:56
Show Gist options
  • Save hybridherbst/1240e67332f61b839add1a08b5f76d58 to your computer and use it in GitHub Desktop.
Save hybridherbst/1240e67332f61b839add1a08b5f76d58 to your computer and use it in GitHub Desktop.
Set the number of concurrent compiler threads for Unity3D
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public static class SetThreadCount
{
[InitializeOnLoadMethod]
static void Init()
{
// change this to test out different configurations
// in my tests there were pretty negligible performance differences between various options (*2, *4, *0.5)
// an easy way to test compilation performance is with https://github.com/needle-tools/compilation-visualizer
int threadCount = SystemInfo.processorCount;
if (threadCount != SystemInfo.processorCount) {
// EXPERIMENT: set thread count for compilation "UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface"
var eci = Type.GetType(
"UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface, UnityEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
if (eci != null) {
var instanceProp = eci.GetProperty("Instance", (BindingFlags) (-1));
if (instanceProp != null) {
var instance = instanceProp.GetValue(null);
var setComp = instance.GetType().GetMethod("SetMaxConcurrentCompilers", (BindingFlags) (-1));
setComp?.Invoke(instance, new object[] { threadCount });
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment