From e06ddbdeb04ea55775bca522284aa16703c9aff2 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 22 Jan 2015 18:23:46 -0800 Subject: [PATCH] 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?. --- src/honeysql/format.clj | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..058f164 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -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)) ;;;;