Skip to content

Commit

Permalink
Add lightweight keyset wrapper
Browse files Browse the repository at this point in the history
Saves on allocation and provides an immutable facade to underlying
regular set
  • Loading branch information
bsless committed Aug 24, 2021
1 parent bb05259 commit 98bc16b
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,35 @@
:cljs
(.slice arr 0 to)))

#?(:clj
(defn- -set-wrapper
[^java.util.Set s]
(reify
Collection
(size [_] (.size s))
(containsAll [_ c] (.containsAll s c))
clojure.lang.IPersistentSet
(disjoin [_ key] (disj (set s) key))
(contains [_ key] (.contains s key))
(get [_ key] (when (.contains s key) key))
clojure.lang.Counted
(count [_] (.size s))
clojure.lang.IPersistentCollection
(cons [_ o] (.cons (set s) o))
(empty [_] #{})
(equiv [_ o]
(if (instance? java.util.Set o)
(if (== (.size s) (.size ^Collection o))
(.containsAll s o)
false)
false))
clojure.lang.Seqable
(seq [_] (seq s))
clojure.lang.IFn
(invoke [_ o] (when (.contains s o) o))
Iterable
(iterator [_] (.iterator s)))))

(defn -parse-entries
[children {:keys [naked-keys lazy-refs]} options]
(let [n (count children)
Expand All @@ -351,11 +380,11 @@
{:children (arr->vec -children)
:entries (arr->vec -entries)
:forms (arr->vec -forms)
:keyset (#?(:clj set, :cljs clojure.core/deref) -keyset)}
:keyset (#?(:clj -set-wrapper, :cljs clojure.core/deref) -keyset)}
{:children (arr->vec (arange -children i))
:entries (arr->vec (arange -entries i))
:forms (arr->vec (arange -forms i))
:keyset (#?(:clj set, :cljs clojure.core/deref) -keyset)})
:keyset (#?(:clj -set-wrapper, :cljs clojure.core/deref) -keyset)})
(recur
(-parse-entry* (nth children i) naked-keys lazy-refs options i -children -entries -forms -keyset)
(unchecked-inc-int ci))))))
Expand Down

0 comments on commit 98bc16b

Please sign in to comment.