c# – 罗斯林启动时间慢

c# – 罗斯林启动时间慢,第1张

概述我注意到Roslyn解析/编译的启动时间是一个相当重要的一次性成本.编辑:我正在使用Roslyn CTP MSI(程序集在GAC中).这是预期的吗?有没有解决方法? 运行下面的代码与1次迭代(约3秒)和300次迭代(约3秒)的时间几乎相同. [Test]public void Test(){ var iters = 300; foreach (var i in Enumerab 我注意到Roslyn解析/编译的启动时间是一个相当重要的一次性成本.编辑:我正在使用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);    }}
解决方法 我认为一个问题是使用内存流,而应该尝试使用动态模块和ModuleBuilder.总体而言,代码执行速度更快,但仍然有较重的首次加载方案.我自己对罗斯林很陌生,所以我不确定为什么这会更快,但这里是改变的代码.
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",new[] { SyntaxTree },references);            var assemblyBuilder = AppDomain.CurrentDomain.defineDynamicAssembly(new System.Reflection.Assemblyname("CustomerA"),System.Reflection.Emit.AssemblyBuilderAccess.RunAndCollect);            var moduleBuilder = assemblyBuilder.defineDynamicModule("MyModule");            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();            watch.Start();            // if we comment out from this line and down,the runtime drops to ~.5 seconds            var emitResult = compilation.Emit(moduleBuilder);            watch.Stop();            System.Diagnostics.DeBUG.Writeline(watch.ElapsedMilliseconds);            if (emitResult.Diagnostics.LongCount() == 0)            {                var type = moduleBuilder.GetTypes().Single(t => t.name == "Foo" + i);                System.Diagnostics.DeBUG.Write(type != null);            }        }

通过使用这种技术,编译只需96毫秒,在后续迭代中需要大约3到15毫秒.所以我认为你可能在第一个负载场景方面增加一些开销.

对不起,我无法解释为什么它更快!我自己就是在研究Roslyn,今晚晚些时候会做更多的挖掘,看看我是否能找到更多关于ModuleBuilder在内存流上提供的证据.

总结

以上是内存溢出为你收集整理的c# – 罗斯林启动时间慢全部内容,希望文章能够帮你解决c# – 罗斯林启动时间慢所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1252542.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存