5

F# 社区的常识是,PowerPack 的引用编译工具生成的代码非常慢,实际上它的执行速度比天真的解释还要糟糕。我一直在研究造成这种情况的原因,但到目前为止我还没有找到令人信服的答案。有人声称,这种情况的发生要么是因为引用中的模式匹配等事物的表示效率低下,要么是因为库使用的表达式树的固有效率低下。我想用一个简单的例子来说明为什么我认为两者都不正确:


#r "FSharp.Powerpack.Linq.dll"

open System
open System.Linq.Expressions

open Microsoft.FSharp.Quotations.Patterns

let powerpack = Microsoft.FSharp.Linq.QuotationEvaluator.Compile <@ 1 + 1 @>

// explicitly rewrite above quotation with expression trees
let expressionTree =
    let (Call(_,addM,_)) = <@ 1 + 1 @>
    let constExpr (x : 'T) = Expression.Constant(box x, typeof<'T>)
    let eval = Expression.Call(addM, constExpr 1, constExpr 1)
    let lambda = Expression.Lambda<Func<int>>(eval)
    lambda.Compile()

// reflection - based evaluation
let reflection =
    let (Call(_,addM,_)) = <@ 1 + 1 @>
    fun () -> addM.Invoke(null, [| 1 :> obj ; 1 :> obj |]) :?> int

#time 

// QuotationEvaluator ~ 2.5 secs
for i in 1 .. 1000000 do
    powerpack () |> ignore

// native evaluation ~ 1 msec
for i in 1 .. 1000000 do
    (fun () -> 1 + 1) () |> ignore

// reflection evaluation ~ 700 msec
for i in 1 .. 1000000 do
    reflection () |> ignore

// naive expression tree ~ 19 msec
for i in 1 .. 1000000 do
    expressionTree.Invoke () |> ignore

这里显然出了问题。问题是,什么?

编辑: FSharpx.Linq 编译器也会发生相同的行为

4

1 回答 1

2

Below is the implementation of the compile:

let CompileImpl (e: #Expr, eraseEquality) = 
       let ty = e.Type
       let e = Expr.NewDelegate(GetFuncType([|typeof<unit>; ty |]), [new Var("unit",typeof<unit>)],e)
       let linqExpr = Conv (e,eraseEquality)
       let linqExpr = (linqExpr :?> LambdaExpression)
       let d = linqExpr.Compile()
       (fun () -> 
           try 
             d.DynamicInvoke [| box () |]
           with :? System.Reflection.TargetInvocationException as exn -> 
               raise exn.InnerException)

Notice the use of DynamicInvoke on the delegate, which is much slower than Invoke and the reason for the result you are getting.

于 2013-08-18T16:06:55.533 回答