Skip to content

Commit

Permalink
Avoid copying plain map when converting to instance (#165)
Browse files Browse the repository at this point in the history
* Avoid copying plain map when converting to instance

* Try again

* Missing case
  • Loading branch information
crisptrutski authored Jul 16, 2024
1 parent 3cb3667 commit ac7f07d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/toucan2/instance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
[potemkin :as p]
[pretty.core :as pretty]
[toucan2.protocols :as protocols]
[toucan2.realize :as realize]))
[toucan2.realize :as realize]
[toucan2.util :as u]))

(set! *warn-on-reflection* true)

Expand Down Expand Up @@ -267,9 +268,13 @@
;; (instance? m)
;; (protocols/with-model m model)

:else
;; Strip any customizations, e.g. a different underlying empty map or key transform.
(u/custom-map? m)
(let [m* (into {} m)]
(->Instance model m* m* (meta m)))))
(->Instance model m* m* (meta m)))

:else
(->Instance model m m (meta m))))

(^toucan2.instance.Instance [model k v & more]
(let [m (into {} (partition-all 2) (list* k v more))]
Expand Down
6 changes: 5 additions & 1 deletion src/toucan2/jdbc/row.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
[toucan2.instance :as instance]
[toucan2.log :as log]
[toucan2.protocols :as protocols]
[toucan2.realize :as realize])
[toucan2.realize :as realize]
[toucan2.util :as u])
(:import
(java.sql ResultSet)))

Expand Down Expand Up @@ -280,6 +281,9 @@
(realize [_this]
@realized-row)

u/IsCustomMap
(custom-map? [_] true)

(toString [this]
(str/join \space (map str (print-representation-parts this)))))

Expand Down
16 changes: 15 additions & 1 deletion src/toucan2/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
[clojure.walk :as walk]
[methodical.util.dispatch :as m.dispatch]
[pretty.core :as pretty]
[toucan2.protocols :as protocols]))
[toucan2.protocols :as protocols])
(:import
(clojure.lang IPersistentMap)
(potemkin.collections PotemkinMap)))

;;; TODO -- there is a lot of repeated code in here to make sure we don't accidentally realize and print `IReduceInit`,
;;; and at least 3 places we turn an `eduction` into the same pretty form. Maybe we should try to consolidate some of
Expand Down Expand Up @@ -110,3 +113,14 @@
(keyword (csk/->kebab-case (namespace x))
(csk/->kebab-case (name x)))
(csk/->kebab-case x)))

(defprotocol IsCustomMap
"Is this a map a transient row, or created using p/def-map-type? This includes Instances."
(custom-map? [m]))

(extend-protocol IsCustomMap
IPersistentMap
(custom-map? [_] false)

PotemkinMap
(custom-map? [_] true))

0 comments on commit ac7f07d

Please sign in to comment.