Page 1 of 1

Embedded C# compiler

PostPosted: Sat Sep 19, 2009 11:08 pm
by Charles
The Short

-- Cobra is faster under Mono due to embedding the Mono C# compiler.

-- I need folks on Mono to test and give feedback.


The Long

As previously discussed, Cobra generates C# code for its back-end for various benefits including byte code that's guaranteed to be correct, fast, high quality and 100% compatible with VM standard classes. On the down side, performance can drag because Cobra writes that C# to disk and invokes the C# compiler as a separate process, which then reads the fresh code from disk. Until now.

I took the Novell Mono C# compiler, which is open source under the MIT license, and hacked on it to accept in-memory strings for source code. I compiled that into a Cobra.Sharp.dll which cobra.exe will now look for and use--at least under Mono. The result is that the Cobra test suite runs about 41% - 43% faster under Mac and Linux.

Cobra.Sharp.dll is basically a slightly enhanced gmcs.exe. Unfortunately, gmcs.exe won't run under Microsoft .NET which I'll look into later. Note that the Microsoft compiler and .NET run-time are so fast compared to Mono, that I still get better times on Windows (same machine) even without this enhancement. But now the gap is much smaller.

This will also make it more rewarding to optimize the Cobra compiler (and even the Mono C# compiler) as heavy disk I/O and process spawning are no longer major factors.


-native-compiler

Also see: cobra -h

cobra -native-compiler:provider ...
# use the .NET standard C# compiler provider interface

cobra -native-compiler:/path/to/Cobra.Sharp.dll ...
# use a DLL; can be any .dll filename, but
# it better be a hacked gmcs.exe under the hood
# C# code is written in memory and passed directly - no disk I/O

cobra -native-compiler:auto ...
# on .NET, use the provider
# on Mono, use the .dll if available, otherwise the provider
# this is the default
# disk I/O and process spawning make this slower

cobra -native-compiler:/path/to/cs.exe
# specify an external executable C# compiler
# any .exe name
# works on Mono and .NET
# this is not the default nor the auto
# also does disk I/O and process spawning


Wrapping Up

Please recall I need Mono folks to take this for a test spin. I'm on Mono 2.4.2.3 so I'm especially interested in what happens if you are on Mono 2.2.x, 2.0.x, etc.

I'll submit a patch to Novell when this settles down so Cobra can just use gmcs.exe directly (it's an assembly after all).

Now it's time for some poker...


-Chuck

Re: Embedded C# compiler

PostPosted: Wed Sep 23, 2009 7:35 pm
by eric.sellon
Very cool update. It worked just fine on Mono 2.0.1 under Ubuntu.