Does anybody know the reason why QuotationEvaluator is so slow?
It's common knowledge in the F# community that the PowerPack's quotation
compiling facility produces very slow code, so slow in fact that it
performs even worse than naive interpretation. I've been looking into the
reasons for this, but I haven't been able to find a convincing answer so
far. There have been claims that this happens either because of
inefficient representation of things like pattern matches in quotations or
because of an inherent inefficient with Expression Trees used by the
library. I'd like to illustrate why I think neither is true with a simple
example:
#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)
let eval = Expression.Call(addM, constExpr 1, constExpr 1)
let lambda = Expression.Lambda>(eval)
lambda.Compile()
#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
// naive expression tree ~ 19 msec
for i in 1 .. 1000000 do
expressionTree.Invoke () |> ignore
Something clearly goes wrong here. The question is, what?
No comments:
Post a Comment