Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster registry #517

Merged
merged 4 commits into from
Aug 23, 2021
Merged

Faster registry #517

merged 4 commits into from
Aug 23, 2021

Conversation

bsless
Copy link
Contributor

@bsless bsless commented Aug 21, 2021

Speed up registry creation and usage

Part of #513

@bsless
Copy link
Contributor Author

bsless commented Aug 21, 2021

(def regs
  {:nil nil
   :map {}
   :simple (mr/simple-registry {})
   :composite (mr/composite-registry (mr/simple-registry {}))
   :mutable (mr/mutable-registry (atom {}))})

(doseq [[name r] regs]
  (let [mean (first (:mean (cc/quick-benchmark (mr/registry r) {})))
        [scale unit] (cc/scale-time mean)]
    (println name (* mean scale) unit)))

;;; Before
;; :nil 11.314252099384198 ns
;; :map 11.553363773721745 µs
;; :simple 5.894620337443649 ns
;; :composite 5.851375239599097 ns
;; :mutable 5.893465716928456 ns

;;; After
;; :nil 3.259309437187958 ns
;; :map 22.65670884744675 ns
;; :simple 3.267212075708806 ns
;; :composite 3.287585829320794 ns
;; :mutable 3.289205443217265 ns

@bsless
Copy link
Contributor Author

bsless commented Aug 21, 2021

Also think I found a mistake in one of the tests

@ikitommi ikitommi merged commit 1055b3c into metosin:master Aug 23, 2021
@ikitommi
Copy link
Member

thanks!

(defn registry [?registry]
(cond (satisfies? Registry ?registry) ?registry
(map? ?registry) (simple-registry ?registry)))
(defn registry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplified this into:

(defn registry [?registry]
  (cond (nil? ?registry) nil
        #?@(:clj [(instance? malli.registry.Registry ?registry) ?registry])
        (map? ?registry) (simple-registry ?registry)
        (satisfies? Registry ?registry) ?registry))

instance? -check works with reified classes? woot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do. I dug into why satisfies? is fast for reified classes, and voila!
I also found that nil? checks are slightly slower than raw if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants