Improve efficiency of equiv impl of jvm-custom-record

This commit is contained in:
Andrey Antukh 2023-08-28 14:58:41 +02:00
parent 7f91619075
commit b28cad2250

View file

@ -9,8 +9,7 @@
(:refer-clojure :exclude [defrecord assoc! clone]) (:refer-clojure :exclude [defrecord assoc! clone])
#?(:cljs (:require-macros [app.common.record])) #?(:cljs (:require-macros [app.common.record]))
#?(:clj #?(:clj
(:import java.util.Map (:import java.util.Map$Entry)))
java.util.Map$Entry)))
#_:clj-kondo/ignore #_:clj-kondo/ignore
(defmacro caching-hash (defmacro caching-hash
@ -44,11 +43,12 @@
key-sym (gensym "key-") key-sym (gensym "key-")
val-sym (gensym "val-") val-sym (gensym "val-")
this-sym (with-meta 'this {:tag tagname})] this-sym (with-meta 'this {:tag tagname})]
['cljs.core/ICloneable ['cljs.core/IRecord
'cljs.core/ICloneable
`(~'-clone [~this-sym] `(~'-clone [~this-sym]
(new ~tagname ~@(generate-field-access this-sym val-sym fields))) (new ~tagname ~@(generate-field-access this-sym val-sym fields)))
'IHash 'cljs.core/IHash
`(~'-hash [~this-sym] `(~'-hash [~this-sym]
(caching-hash ~this-sym (caching-hash ~this-sym
(fn [coll#] (fn [coll#]
@ -175,16 +175,17 @@
val-sym 'val val-sym 'val
this-sym (with-meta 'this {:tag tagname})] this-sym (with-meta 'this {:tag tagname})]
['clojure.lang.MapEquivalence ['clojure.lang.IRecord
'clojure.lang.IPersistentMap 'clojure.lang.IPersistentMap
`(~'equiv [~this-sym ~val-sym]
`(~'equiv [~this-sym ~'other] (and (some? ~val-sym)
(and (instance? java.util.Map ~'other) (= (.count ~this-sym) (.size ^Map ~'other)) (instance? ~tagname ~val-sym)
(every? (fn [^clojure.lang.MapEntry e#] ~@(map (fn [field]
(let [k# (.key e#)] `(= (.. ~this-sym ~(property-symbol field))
(and (.containsKey ^Map ~'other k#) (.. ~(with-meta val-sym {:tag tagname}) ~(property-symbol field))))
(= (.val e#) (.get ^Map ~'other k#))))) base-fields)
(.seq ~this-sym)))) (= (. ~this-sym ~'-$extmap)
(. ~(with-meta val-sym {:tag tagname}) ~'-$extmap))))
`(~'entryAt [~this-sym ~key-sym] `(~'entryAt [~this-sym ~key-sym]
(let [v# (.valAt ~this-sym ~key-sym ::not-found)] (let [v# (.valAt ~this-sym ~key-sym ::not-found)]