Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 使用Clojure宏内联函数_Performance_Compiler Construction_Macros_Clojure - Fatal编程技术网

Performance 使用Clojure宏内联函数

Performance 使用Clojure宏内联函数,performance,compiler-construction,macros,clojure,Performance,Compiler Construction,Macros,Clojure,更奇怪的是,其他任何东西(但期望它偶尔会成为性能调整的有用技巧)是否可以使用Clojure宏“内联”现有函数 i、 e.我希望能够做到以下几点: (defn my-function [a b] (+ a b)) (defn add-3-numbers [a b c] (inline (my-function a (inline (my-function b c))))) 并让它(在编译时)生成与我自己内联添加内容完全相同的函数,例如: (

更奇怪的是,其他任何东西(但期望它偶尔会成为性能调整的有用技巧)是否可以使用Clojure宏“内联”现有函数

i、 e.我希望能够做到以下几点:

(defn my-function [a b] (+ a b))

(defn add-3-numbers [a b c] 
  (inline (my-function 
    a 
    (inline (my-function 
      b 
      c)))))
并让它(在编译时)生成与我自己内联添加内容完全相同的函数,例如:

(defn add-3-numbers [a b c] 
  (+ a (+ b c)))

如果您不知道,可以使用
definline

(doc definline)
-------------------------
clojure.core/definline
([name & decl])
Macro
  Experimental - like defmacro, except defines a named function whose
  body is the expansion, calls to which may be expanded inline as if
  it were a macro. Cannot be used with variadic (&) args.
nil
还查了来源,

(source definline)
-------------------------
(defmacro definline
  [name & decl]
  (let [[pre-args [args expr]] (split-with (comp not vector?) decl)]
    `(do
       (defn ~name ~@pre-args ~args ~(apply (eval (list `fn args expr)) args))
       (alter-meta! (var ~name) assoc :inline (fn ~name ~args ~expr))
       (var ~name))))

definline
仅使用元数据
{:inline(fn定义)}
定义一个
var
。因此,虽然这并不完全是您所要求的,但您可以使用新的元数据重新绑定var以获得内联行为。

您是否查看了
apply
函数?apply在运行时动态工作,我正在寻找在编译时执行内联的东西……
(readstring(clojure.repl/source-fn`my function))
似乎是一个很好的起点!有用的链接-在许多情况下,这看起来绝对是一个有用的工具。与我所寻找的关键区别在于,它似乎要求将函数显式定义为内联函数,而我希望能够内联任意函数。我没有考虑过如何实现它,但我暗示了一个潜在的解决方案。您可以尝试编写一个宏,将函数var重新绑定为包含:inline标记的元数据的函数。需要解决的关键问题是确保重新绑定在编译时而不是运行时完成。