I just stumbled on a fascinating performance improvement coming up in .Net 3.5 SP1.
When working with value types (structs) the JIT currently hits the stack each time you access a field - which is quite inefficient.
In SP1 they've added an algorithm called "value type scalar replacement"; which intuitively does exactly what it sounds like!
Take the following code:
The JIT will replace the value type (Point is a struct) with scalars:
After the JIT inlines the structure, it can optimise the routine by placing the integers on the register rather than the stack.
What's the difference?
We are roughly looking at a 10x improvement! (Using a quick and dodgy test app)
This is fantastic, especially when dealing with lots of structs like points and rectangles etc.
What's the catch?
This isn't magically going to work on all your value types; there are strict guidelines when this replacement can occur:
- The value type must have no more than 4 fields
- These fields must be value types, or object references
- You must use [StructLayout(LayoutKind.Sequential)], which is the default anyhow.
There some other catches, but if you are creating complicated structures, you really should think about making them a class.
This is just a snippet of the algorithm, whereas the original article explains it in much more detail.
BTW: This is in the CLR, so all .Net languages benefit! And of course this includes F# and FParsec.
Cheers to SP1!