Embeddable C# compiler and ASP.NET hacks

by Miguel de Icaza

Mono's C# compiler has a fairly complete regression test suite: we routinely use mcs to compile about the two million lines of code that make Mono up but it also contains about two thousand individual positive and negative test cases.

Since we run continuous builds of Mono to detect regressions on the code base the two thousand test cases on the compiler were starting to slow down quite a bit the regular Mono builds and the release regression test cases by quite some time.

Faced with this problem Marek Safar, one of the main C# compiler contributors, refactored the compiler so that it could be reused as a library. Then he built a couple of drivers that would process the compiler's negative and positive tests. This reduced the time to run all the tests from about 6-8 minutes to 24 seconds.

The time savings come from not having to restart the compiler over and over for each one of the invocations and instead just have the compiler reset at the end of each compilation and move on to the next target:

	test-anon-28.cs...	OK
	test-anon-29.cs...	OK
	test-anon-30.cs...	OK
I used to relax watching the screen scroll

The bottle-neck moved from C# to my terminal program.

This embedding turned out to be useful for a small hack. ASP.NET invokes the compiler on a page the first time that a page is hit. When you reference an ASP.NET page like "demo.aspx" the file is compiled down into C# code, which is then passed to the C# compiler and the output is dynamically loaded into the server which in turn dispatches the request from it.

While developing applications developers might notice a 1-2 second delay on the first hit to the page as the compiler processes the file, some months ago I cooked a patch that uses Marek's interface to embed the C# compiler directly into the ASP.NET engine (original posting, patch).

On my machine the time went from about 1.5 second to .9 seconds to compile and load a page on the first hit.

This patch is not very interesting anymore as ASP.NET 2.x allows for websites to be deployed by preocompiling the whole site and copying the resulting DLL.

Posted on 28 Jul 2005