« March 2009 | Main | October 2010 »

1 posts from April 2009

April 19, 2009

IL Features Missing in C#

I came back from the Lang.NET conference and spoke to members of the C# team about two features which are exposed in IL, but are not available in C#.

1) Extended precision for floating-point calculations.

2) Tail call optimizations.

In C++, intermediate calculations can be performed at the highest precision offered by hardware. In C#, intermediate calculations with doubles are fixed at the standard or double precision--the precision of a double data type. The same is possibly true for floats. Double precision is not large enough to store a long value. The rationale is to prevents errors in which same code yields different results depending on optimization settings and environment.

Tail call optimizations arise when the result of a function call is directly returned without any intervening operations. The new function call reuses the caller’s stack frame and it’s use is not limited to tail recursion scenarios.

Tail calls are currently used by F# and Nemerle. C# programs benefit from tail call optimization in 64-bit architectures as tail calls are aggressively performed regardless of the presence of the .tail prefix. In 64-bit, the limited stack space is depleted much more quickly. Tail call optimization is turned off during debugging.

Because of the existing optimization in 64-bit, it seems harmless that the C# could automatically insert tail calls into the compiler in the VS 2010 timeframe. This would encourage programming in a more functional style. (Actually, tails calls are somewhat slower in x86.)

In my own development, I hit upon the stack limit through finite recursion and had to double the amount of stack space in my thread.

My current approach for dealing with these two problems is to disassemble my executable with ILDASM, insert the .TAIL prefix, remove the CONV.R8 or CONV.R4 instructions, and rebuild the executable with ILASM.

Some tail call links:

http://blogs.msdn.com/davbr/pages/tail-call-jit-conditions.aspx

https://beta.blogs.msdn.com/shrib/archive/2005/01/25/360370.aspx