What are the benefits of using Roslyn - compiler as a service
Asked Answered
L

4

26

What is CaaS (Compiler As A Service) in regards to the Roslyn project?

How does using the Roslyn feature improve the performance of a C# application over the current C# 4.0 compiler?

What are the known limitations/issues in the Roslyn-CTP?

Leatherjacket answered 19/1, 2012 at 6:9 Comment(3)
It's not clear to me what exactly do you mean by "compiling process". What exactly do you want to know?Lytton
@Lytton i.e. What exactly is CaaS as applied to Roslyn and also some light on CaaS in general..Leatherjacket
See my updated answer for that.Lytton
L
19

What exactly does Compiler as a Service (CaaS) mean in relation to Roslyn? You can watch a video where Anders Hejlsberg explains that (talk about Roslyn starts at 35 minutes in). Basically, the old C# compiler is a "black box": source code comes in, compiled assemblies come out. Roslyn gives you access inside that box. That means you can get syntactic and semantic information about some code, modify it and give it back to the compiler to process it further. You can use that to do code analysis, refactoring, code generation and more.

There is a long list of features that are not implemented in the current CTP on the Roslyn forum.

Regarding performance, I don't think that's among the goals of Roslyn. Besides, the JIT compiler is more important for performance optimizations than the C#/VB compiler. And Roslyn replaces the C#/VB compiler, not th JIT compiler.

Lytton answered 19/1, 2012 at 11:28 Comment(3)
Just an update : As per microsoft performance is actually one of the key "features" they now want in Roslyn blogs.msdn.com/b/csharpfaq/archive/2014/01/15/…Flapdoodle
@Flapdoodle The performance of the Roslyn compiler itself, yes. But not the performance of the compiled code, which is what the question asked about.Lytton
@svick.. Oops sorry I was carried away by the awesomeness of "Roslyn".. my badFlapdoodle
B
9

Compiler as a service (CaaS) with respect to Roslyn just means the compilation process is broken down into pieces with a public API that lets you examine the syntactic and semantic models built by the compiler during compilation. The Roslyn C# and VB compilers completely replace the existing compilers, so you can continue to use them in the same way you use the compilers today (as separate executables that converts text files into .net assemblies.) You can also use the compilers as a library of APIs that help you build tools that do deeper or different kinds of code analysis.

Roslyn does not give you specific performance advantage over using the existing compilers because when Roslyn releases they will be one and the same. However, it is possible to use roslyn to build specialized code refactorings that improve your source code.

Balling answered 23/1, 2012 at 22:34 Comment(0)
B
7

One advantage that Roslyn has is when your application needs a scripting interface. With Roslyn, you can directly compile the script as C# source code with same programming possibilities as the source of the application and directly use it.

Brownfield answered 19/1, 2012 at 6:54 Comment(3)
do you mean after compiling, the script is converted to C# source code?Leatherjacket
Yes, the user could be given a text box into which he can script in a high level language like VB.NET or C# and the the code can be directly compiled and used by the application...Brownfield
You can do that without Roslyn. VS currently supports compiling a source code string into the current project at runtime.Laser
C
3

I'm excited about Roslyn because it will allow an easier way to implement Aspect Oriented Programming. Using AOP, you can write common "policies" and apply them across your code instead of spreading it across all your code. Using the Roslyn compiler you could weave these policies across your code at compile time, and keep cross-cutting concerns for polluting your business code. If you want some use cases, check out what PostSharp lists as their use cases

http://www.sharpcrafters.com/

  • Logging policies
  • Exception handling policies
  • Automatic thread synchronization (make sure the right things get called on the UI thread)
  • Automatic auditing of function calls
  • Automatically generate INotifyPropertyChanged implementations for automatic properties
  • and more
Cumine answered 11/8, 2012 at 18:47 Comment(1)
But isn't the AST immutable?Saga

© 2022 - 2024 — McMap. All rights reserved.