Ho notato che il tempo di avvio per l'analisi/compilazione di Roslyn è un costo una tantum abbastanza significativo. EDIT: sto usando il Roslyn CTP MSI (l'assembly è nel GAC). È previsto? C'è qualche soluzione?Roslyn tempo di avvio lento
L'esecuzione del codice seguente richiede quasi la stessa quantità di tempo con 1 iterazione (~ 3 secondi) come con 300 iterazioni (~ 3 secondi).
[Test]
public void Test()
{
var iters = 300;
foreach (var i in Enumerable.Range(0, iters))
{
// Parse the source file using Roslyn
SyntaxTree syntaxTree = SyntaxTree.ParseText(@"public class Foo" + i + @" { public void Exec() { } }");
// Add all the references we need for the compilation
var references = new List<MetadataReference>();
references.Add(new MetadataFileReference(typeof(int).Assembly.Location));
var compilationOptions = new CompilationOptions(outputKind: OutputKind.DynamicallyLinkedLibrary);
// Note: using a fixed assembly name, which doesn't matter as long as we don't expect cross references of generated assemblies
var compilation = Compilation.Create("SomeAssemblyName", compilationOptions, new[] {syntaxTree}, references);
// Generate the assembly into a memory stream
var memStream = new MemoryStream();
// if we comment out from this line and down, the runtime drops to ~.5 seconds
EmitResult emitResult = compilation.Emit(memStream);
var asm = Assembly.Load(memStream.GetBuffer());
var type = asm.GetTypes().Single(t => t.Name == "Foo" + i);
}
}
Non sono del tutto sorpreso, ricordate che è necessario caricare tutti gli assembly di Roslyn e probabilmente c'è anche un sacco di compilation JIT. Potresti provare a delinearlo per vedere cosa succede. –
In effetti, un profiler è tuo amico. Inoltre, cosa succede se si estrae quell'assemblaggio. Ciò non aiuterà affatto il perf. –
Come si ottengono i file binari di Roslyn nel sistema? Se stai ricevendo il pacchetto NuGet, è probabile che questa volta sia JIT. Se è stato installato il CTP MSI, GAC e NGen verranno assemblati e probabilmente si vedranno prestazioni di avvio migliori. –