Flattened matching, 1.2ms -> 0.8ms!!!

This commit is contained in:
Tommi Reiman 2017-11-07 20:36:26 +02:00
parent eeea39ca83
commit e12bfeabf1

View file

@ -44,14 +44,15 @@
(reify (reify
Node Node
(lookup [this path params] (lookup [this path params]
#_(println "w=>" segment "..." path)
(let [i (.indexOf ^String path "/")] (let [i (.indexOf ^String path "/")]
(if (pos? i) (if (pos? i)
(let [value (subs path 0 i)] (let [value (subs path 0 i)]
(let [childs [(impl/fast-get children' (char-key path (inc i))) ?wild ?catch] (let [child (impl/fast-get children' (char-key path (inc i)))
path' (subs path (inc i)) path' (subs path (inc i))
params (assoc params param value)] params (assoc params param value)]
(some #(lookup % path' params) childs))) (or (lookup child path' params)
(lookup ?wild path' params)
(lookup ?catch path' params))))
(assoc params param path)))) (assoc params param path))))
(get-segment [this] (get-segment [this]
segment) segment)
@ -76,14 +77,15 @@
(reify (reify
Node Node
(lookup [this path params] (lookup [this path params]
#_(println "s=>" segment "..." path)
(if (.equals segment path) (if (.equals segment path)
params params
(let [p (if (>= (count path) size) (subs path 0 size))] (let [p (if (>= (count path) size) (subs path 0 size))]
(if (.equals segment p) (if (.equals segment p)
(let [childs [(impl/fast-get children' (char-key path size)) ?wild ?catch] (let [child (impl/fast-get children' (char-key path size))
path (subs path size)] path (subs path size)]
(some #(lookup % path params) childs)))))) (or (lookup child path params)
(lookup ?wild path params)
(lookup ?catch path params)))))))
(get-segment [this] (get-segment [this]
segment) segment)
(update-segment [this subs lcs] (update-segment [this subs lcs]
@ -315,4 +317,5 @@
;; 1.3ms (fast-map) ;; 1.3ms (fast-map)
;; 1.3ms (dissoc wild & catch-all from children) ;; 1.3ms (dissoc wild & catch-all from children)
;; 1.3ms (reified protocols) ;; 1.3ms (reified protocols)
;; 0.8ms (flattened matching)
(cc/quick-bench (dotimes [_ 1000] (lookup tree-new "/v1/orgs/1/topics" {})))) (cc/quick-bench (dotimes [_ 1000] (lookup tree-new "/v1/orgs/1/topics" {}))))