From 98bc16b8f3036a153f498b7e96c06517bb235c7a Mon Sep 17 00:00:00 2001 From: Ben Sless Date: Tue, 24 Aug 2021 15:32:39 +0300 Subject: [PATCH] Add lightweight keyset wrapper Saves on allocation and provides an immutable facade to underlying regular set --- src/malli/core.cljc | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 3b244cca8..dab9d0b81 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -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) @@ -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))))))