7

Roslyn の解析/コンパイルの起動時間は、かなりの 1 回限りのコストであることに気付きました。編集: Roslyn CTP MSI を使用しています (アセンブリは GAC にあります)。これは期待されていますか?回避策はありますか?

以下のコードを実行すると、1 回の反復 (約 3 秒) と 300 回の反復 (約 3 秒) でほぼ同じ時間がかかります。

[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);
    }
}
4

4 に答える 4

0

Compilation.Emit() を呼び出すと、実際にメタデータが必要になるのは初めてなので、メタデータ ファイルへのアクセスが発生します。その後、キャッシュされます。ただし、mscorlib だけで 3 秒を占めるわけではありません。

于 2014-01-21T19:03:33.977 に答える