什么使FSharpFunc<>快于Func? [英] What makes FSharpFunc<> faster than Func<>?

查看:188
本文介绍了什么使FSharpFunc<>快于Func?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇FSharpFunc的性能提升。
事实上它不包含多个委托,所以在触发函数调用时不需要循环遍历所有引用?还有其他什么?

解决方案

我认为使用 FSharpFunc 而不是 Func<> 或任何其他委托是您不能创建一个继承自委托类型的类(首先,这听起来很合理,但在.NET,委托实际上只是一些特殊的类,所以原则上可能允许这个)。为什么需要这样?



如果您在F#中写一个函数,那么(以相对较少但非常重要的情况)以柯里格形式处理。例如 int - > int - > int 实际上是一个函数类型 int - > (int - > int)(currying意味着您只使用单个参数的函数来编写函数 - 如果使用第一个参数调用它,则会得到一个函数,因此可以使用第二个参数调用返回的函数)。



如果F#使用代理,类型将类似于 Func< int,Func< int,int>> 。正如Brian所说,调用 f x y 将被翻译成两个调用: f(x)(y)。但是这种调用是最常见的(仅指定一个参数称为部分功能应用程序)。所以当F#编译一个这样的函数时,它会使用一个优化的调用方法创建一个继承的类,所以它可以被调用为 f.Invoke(x,y)

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

不幸的是,不可能通过继承标准的 Func (因为它是一个委托)来创建这样的类,所以F#必须声明它可以用作基类的自己的类型...


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 ?

解决方案

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...

这篇关于什么使FSharpFunc&lt;&gt;快于Func?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆