From f8127e3aac014c1691c0432808069e51b1c1a391 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Thu, 22 Jan 2015 16:18:55 -0800 Subject: [PATCH] memoize sqlable? In simple benchmarks, calls to sql/format spend the majority of their time in satisfies. Make the simple cases simpler with caching. --- src/honeysql/format.clj | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..e196f1d 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -260,11 +260,19 @@ nil (-to-sql [x] "NULL")) +(def class-cache (atom nil)) + (defn sqlable? [x] - (satisfies? ToSql x)) + (let [c (class x) + cache @class-cache] + (if (contains? cache c) + (cache c) + (let [result (satisfies? ToSql x)] + (swap! class-cache assoc c result) + result)))) (defn to-sql [x] - (if (satisfies? ToSql x) + (if (sqlable? x) (-to-sql x) (let [[x pname] (if (instance? SqlParam x) (let [pname (param-name x)]