F#代码引用调用,性能和运行时要求 [英] F# code quotation invocation, performance, and run-time requirements

查看:145
本文介绍了F#代码引用调用,性能和运行时要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是有关F#代码引用的4个与之息息相关的问题-

Here are 4 deeply related questions about F# code quotations -

如何调用F#代码引用?

How do I invoke an F# code quotation?

是否会以比普通的旧F#lambda更低的效率调用它?到什么程度?

Will it be invoked in a manner less efficient than if it were just a plain old F# lambda? to what degree?

是否需要运行时支持高级反射或代码发射功能(我所针对的嵌入式平台通常不存在或禁止这样做)?

Will it require run-time support for advanced reflection or code-emitting functionality (which is often absent or prohibited from embedded platforms I am targeting)?

推荐答案

报价只是数据,因此您可能会以自己想出的任何巧妙方式调用"它们.例如,您可以简单地遍历树并在运行时解释每个节点,但是如果您尝试多次使用该值而不是简单的值(例如,如果引用了lambda值),则执行效果会特别差您想反复调用的内容.

Quotations are just data, so you can potentially "invoke" them in whatever clever way you come up with. For instance, you can simply walk the tree and interpret each node as you go, though that wouldn't perform particularly well if you're trying use the value many times and its not a simple value (e.g. if you've quoted a lambda that you want to invoke repeatedly).

如果您想要更高性能(也更简单)的产品,则可以使用Linq.RuntimeHelpers.LeafExpressionConverter.EvaluateQuotation.这不支持所有可能的引号(大致相当于C#LINQ表达式的设置),并且需要做更多的工作才能实际生成IL,等等,但是如果您重新使用结果,这应该会更有效率.通过首先将引号转换为C#表达式树,然后使用在那里定义的标准Compile函数来完成此工作,因此它将仅在支持该功能的平台上工作.

If you want something more performant (and also simpler), then you can just use Linq.RuntimeHelpers.LeafExpressionConverter.EvaluateQuotation. This doesn't support all possible quotations (just roughly the set equivalent to C# LINQ expressions), and it's got to do a bit more work to actually generate IL, etc., but this should be more efficient if you're reusing the result. This does its work by first converting the quotation to a C# expression tree and then using the standard Compile function defined there, so it will only work on platforms that support that.

这篇关于F#代码引用调用,性能和运行时要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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