extend ToSql to cover Object

This commit avoids extraneous calls to satisfies?. Satisfies? is very
slow compared to protocol method dispatch, because method dispatch is
cached and satisfies? is not. Instead of using satisfies? to check for
cases where we need to fall back to a default behavior, we extend ToSql
to java.lang.Object, providing a default behavior directly.

This commit boosts honeysql's speed substantially. In my benchmarks,
80-90% of the time spent calling sql/format was spent in satisfies?.
This commit is contained in:
Mike Blume 2015-01-22 18:23:46 -08:00
parent 0c3f3d0403
commit e06ddbdeb0

View file

@ -258,26 +258,26 @@
(paren-wrap sql-str)
sql-str)))
nil
(-to-sql [x] "NULL"))
(-to-sql [x] "NULL")
Object
(-to-sql [x] (let [[x pname] (if (instance? SqlParam x)
(let [pname (param-name x)]
(if (map? @*input-params*)
[(get @*input-params* pname) pname]
(let [x (first @*input-params*)]
(swap! *input-params* rest)
[x pname])))
;; Anonymous param name -- :_1, :_2, etc.
[x (keyword (str "_" (swap! *param-counter* inc)))])]
(swap! *param-names* conj pname)
(swap! *params* conj x)
"?")))
(defn sqlable? [x]
(satisfies? ToSql x))
(defn to-sql [x]
(if (satisfies? ToSql x)
(-to-sql x)
(let [[x pname] (if (instance? SqlParam x)
(let [pname (param-name x)]
(if (map? @*input-params*)
[(get @*input-params* pname) pname]
(let [x (first @*input-params*)]
(swap! *input-params* rest)
[x pname])))
;; Anonymous param name -- :_1, :_2, etc.
[x (keyword (str "_" (swap! *param-counter* inc)))])]
(swap! *param-names* conj pname)
(swap! *params* conj x)
"?")))
(-to-sql x))
;;;;