What makes FSharpFunc<> faster than Func<>?

delegates, f#

Solution

I think that the primary motivation for using `FSharpFunc<>` rather than `Func<>` or any other delegate is that you cannot create a class that would inherit from a delegate type (at first, this sounds reasonable, but in .NET, delegate is actually just some special class, so it may be in principle possible to allow this). Why is this needed?

If you write a function in F# then it is (in a relatively few, but quite important cases) treated in a curried form. For example `int -> int -> int` is actually a function type `int -> (int -> int)` (currying means that you write a function using just functions of single parameter - if you call it with the first argument, you'll get a function as a result and you can invoke the returned function with the second argument).

If F# used delegates, the type would be something like `Func<int, Func<int, int>>`. As Brian mentioned, the invocation `f x y` would be translated into two invocations: `f(x)(y)`. This kind of invocation is however the most common (specifying just a single argument is called partial function application). So, when F# compiles a function like this, it creates an inherited class with an optimized invoke method, so that it can be invoked as `f.Invoke(x, y)`:

class @some_F#_name@ : Func<int, Func<int, int>> {
   public int Invoke(int arg1, int arg2) { /* optimized call */ }
}

Unfortunately, it isn't possible to create such class by inheriting from standard `Func` (because it is a delegate), so F# has to declare its own type which can be used as a base class...

Problem

I'm curious about the performance enhancements that have been made for `FSharpFunc<_>`. Is it the fact that it does not contain multiple delegate so there is no need to loop over all the references when firing a function call ? Anything else ?

Original source