Skip to content

FAQ: Roslyn compiler performance. #91

@oleg-shilo

Description

@oleg-shilo

This is a copy of the interesting discussion on Google groups.


It runs quite a bunch of scripts, could it have a worse performance with Roslyn?

does Roslyn needs every file contained in the \Microsoft.Net.Compilers\2.2.0\tools folder or just a couple of DLLs?

You need to consider quite a few things.

You are using legacy API CS-Script.Native. It can only rely on CodeDom API, meaning that every script being executed is passed through csc.exe compiler. Either the one that comes with .NET (< C#6) or the one that comes from Roslyn (>=C#6). You are using Roslyn's csc.exe.

Roslyn and its C#7 comes with a hefty price tag attached. Roslyn compiler is horrible in term's of startup time. On my PC it takes ~3-4 seconds to load. Though consecutive compilations will be very fast due to the compiler binaries already being loaded in the memory. However the truth is that regardless which package the compiler comes (csc.ex) the compilation triggers forking another process and as such it is an expensive operation. Though forking the process is much faster on Linux. If one uses Roslyn compilers he needs to distribute Microsoft.Net.Compilers\2.2.0\tools. This is an unfortunate decision that Microsoft has made and nothing we can do about it. The topic fully covered in this discussion. Interestingly Mono does not imposes this restriction on developers as it includes Roslyn naturally in the framework.

All this means that your best strategy of improving scripting performance is to use CS-Script caching (or implement your own). Caching is an incredible simple concept borrowed from Python. Thus if your script text is not changes since the last execution it's never compiled and the compilation result from the previous execution is reused. If one uses caching correctly the performance of the scripting is 100% identical to the performance of the fully compiled .NET assembly. You can find information about caching on the CS-Script Wiki.

You also have another option to choose if caching for some reason does not fit your purpose. You can use newer API CS-Script.Evaluator (read more on Wiki). This API unifies all available scripting .NET approaches and allows user to switch the underlying compilers as he wish.

CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn

Thus one can choose "EvaluatorEngine.CodeDom", which will be roughly equivalent of what you have right now. Though you can chose EvaluatorEngine.Roslyn, which is a slightly different favour of compiler comparing to what you are using right now. This compiler is in fact in-process compiler thus it compiles scripts much faster. Another advantage is that you need to deploy much less files with your app. Only the files from your Release/Debug folder.
Negatives - you cannot debug scripts and you'll still pay the price of the first loading of the compiler.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions