Skip to content

Commit

Permalink
Adding simple path to do use primitive ifn interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Nov 2, 2023
1 parent e12f9c3 commit 4663e8e
Show file tree
Hide file tree
Showing 2 changed files with 1,159 additions and 0 deletions.
79 changes: 79 additions & 0 deletions codegen/gen_prim_invoke.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
(ns gen-prim-invoke
(:require [clojure.java.io :as io]
[ham-fisted.lazy-noncaching :as lznc]
[ham-fisted.api :as hamf]))


(defn single-arg-sigs
[rv]
(for [arg1 [:o :l :d]]
[arg1 rv]))

(defn dual-arg-sigs
[rv arg1]
(for [arg2 [:o :l :d]]
[arg1 arg2 rv]))


(defn triple-arg-sigs
[rv arg1 arg2]
(for [arg3 [:o :l :d]]
[arg1 arg2 arg3 rv]))


(defn quad-arg-sigs
[rv arg1 arg2 arg3]
(for [arg4 [:o :l :d]]
[arg1 arg2 arg3 arg4 rv]))



(def ifn-sigs
(hamf/concatv
[[:l]
[:d]]
(->> (lznc/concat
(for [rv [:o :l :d]]
(single-arg-sigs rv))
(for [rv [:o :l :d]
arg1 [:o :l :d]]
(dual-arg-sigs rv arg1))
(for [rv [:o :l :d]
arg1 [:o :l :d]
arg2 [:o :l :d]]
(triple-arg-sigs rv arg1 arg2))
(for [rv [:o :l :d]
arg1 [:o :l :d]
arg2 [:o :l :d]
arg3 [:o :l :d]]
(quad-arg-sigs rv arg1 arg2 arg3)))
lznc/apply-concat
(lznc/remove #(every? (fn [a](= :o a)) %)))))


(defn writeit
[]
(with-open [w (io/writer (io/output-stream "src/ham_fisted/primitive_invoke.clj"))]
(.write w (str "(ns ham-fisted.primitive-invoke
\"For statically traced calls the Clojure compiler calls the primitive version of type-hinted functions
and this makes quite a difference in tight loops. Often times, however, functions are passed by values
or returned from if-statements and then you need to explicitly call the primitive overload - this makes
that pathway less verbose.\")\n\n"))
(doseq [sig ifn-sigs]
(let [sname (apply str (map name sig))]
(.write w (str "(defn as-" sname " ^clojure.lang.IFn$" (.toUpperCase sname) "[f] f)\n"))
(.write w (str "(defmacro " sname " [f"))
(dotimes [i (dec (count sig))]
(.write w (str " "))
(.write w (str "arg" i)))
(.write w "]\n")
(.write w (str "`(.invokePrim (as-" sname " ~f)"))
(dotimes [i (dec (count sig))]
(.write w (str " "))
(.write w (str "~arg" i)))
(.write w "))\n")))))


(comment
(writeit)
)
Loading

0 comments on commit 4663e8e

Please sign in to comment.