John Lam 1997 — 2026

Foiled by csc.exe

I was writing a NAnt build script yesterday that compiles a number of separate projects. Some of these projects are really simple, and I felt that it was a waste to have to create a new Win32 process to host csc.exe as it compiled the code. So today, I spent some time poking around with Reflector to see whether the System.CodeDom classes would let me host the C# compiler inside of the NAnt Win32 process.

Unfortunately, this does not seem to be the case. If you decompile System.CodeDom.Compiler.Compile() using Reflector, you’ll see that it simply shells out to csc.exe via the call to Executor.ExecWaitWithCapture(). This is somewhat disappointing since csc.exe is simply a wrapper around cscomp.dll, which exports a method called CreateCompilerFactory() which is called from csc.exe.

I’m surprised that MS didn’t choose to use cscomp.dll from Microsoft.CSharp.CSharpCodeGenerator. I would imagine that this would give ASP.NET a significant performance boost when compiling .aspx and related files since this avoids the overhead involved in calling CreateProcess. I’ll have to hunt around a bit more in System.Web.dll to make sure that this isn’t in fact the case.

2 archived reader comments

Duncan Smart (2003-07-22)

“I’ll have to hunt around a bit more in System.Web.dll to make sure that this isn’t in fact the case.” — I think you’ll find it *is* the case, if you keep an eye on the processes you see csc.exe being spawned. Using SysInternals’ process monitor it’s more noticeable. Also if you look in Temporary ASP.NET Files you can see the *.cmdline files that are passed to csc.exe using the “@” command-line option.

Ian MacLean (2003-07-23)

Hey John, I just saw this on the DOTNET-LANGUAGE-DEVS@DISCUSS.MICROSOFT.COM list today. The quote is from larsberg@MICROSOFT.COM. It kinda looks like Cscomp.dll isn’t going to be supported going forward and that they don’t consider the process creation cost significant. Incidently with NAnt now supporting targetting multiple framework versions its easier to spawn different csc.exe’s then it is to load and unload the different versions of Cscomp.dll to ensure that the currect version is run.

“This is from our C# compiler guru, Grant Richins: Cscomp.dll is loaded in-process for the IDE scenario. He is correct that avoiding the call to CreateProcess would save some time, but by running the compiler out-of-process, ASP.NET is able to isolate itself even better by giving the compiler process very few privileges (at one point the compiler couldn’t even write to the temp directory!). Also any exception that happens in the compiler is completely isolated to the compiler process and has no way to bring down all of ASP.NET.

We had considered exposing a managed in-process C# compiler for ASP.NET and the CodeDom. In fact we even wrote the code (see Microsoft.CSharp.Compiler), but when ASP.NET and the CodeDom decided not to use it, we decided to deprecate it. Its only real savings (besides the unneeded CreateProcess) was that it could compile source code passed in as strings and thus not cause file I/O, but I’m not sure if that savings wasn’t swallowed up by the cost of interop. We wanted to remove it entirely because we didn’t have good test coverage for it, but too late in our ship cycle we discovered some other teams had introduced dependencies on this interface unbeknownst to us. So we recommend customers don’t use it because we are planning on ripping it out in a few versions.

–Grant “