+20% tps, +30% faster wildcard routing

This commit is contained in:
Tommi Reiman 2019-02-02 17:52:28 +02:00
parent bf068d22d9
commit 1d6cec7148
4 changed files with 22 additions and 28 deletions

View file

@ -5,8 +5,7 @@
[reitit.impl] [reitit.impl]
[clojure.edn :as edn] [clojure.edn :as edn]
[reitit.ring :as ring] [reitit.ring :as ring]
[reitit.core :as r]) [reitit.core :as r]))
(:import (reitit SegmentTrie)))
;; ;;
;; start repl with `lein perf repl` ;; start repl with `lein perf repl`
@ -87,34 +86,23 @@
{:inject-match? false, :inject-router? false})) {:inject-match? false, :inject-router? false}))
(comment (comment
(let [request {:request-method :get (let [request {:request-method :get, :uri "/user/1234/profile/compact/"}]
:uri "/user/1234/profile/compact/"}] ;; 1338ns (old)
;; OLD: 1338ns ;; 981ns (new)
;; NEW: 981ns ;; 805ns (java)
;; JAVA: 805ns ;; 704ns (no-inject)
;; NO-INJECT: 704ns ;; 458ns (trie)
#_(cc/quick-bench (cc/quick-bench
(handler-reitit request)) (handler-reitit request))
(handler-reitit request))) (handler-reitit request)))
(comment (comment
;; 281ns ;; 190ns
(let [router (r/router [["/user/:id/profile/:type" ::1] (let [router (r/router [["/user/:id/profile/:type" ::1]
["/user/:id/permissions" ::2] ["/user/:id/permissions" ::2]
["/company/:cid/dept/:did" ::3] ["/company/:cid/dept/:did" ::3]
["/this/is/a/static/route" ::4]])] ["/this/is/a/static/route" ::4]])]
#_(cc/quick-bench (cc/quick-bench
(r/match-by-path router "/user/1234/profile/compact")) (r/match-by-path router "/user/1234/profile/compact"))
(r/match-by-path router "/user/1234/profile/compact"))) (r/match-by-path router "/user/1234/profile/compact")))
(comment
(edn/read-string
(str
(.matcher
(doto (SegmentTrie.)
(.add "/user" 1)
(.add "/user/:id" 2)
(.add "/user/:id/orders" 3)
(.add "/user/id/permissions" 4))))))

View file

@ -300,7 +300,7 @@
(ring/create-default-handler) (ring/create-default-handler)
{:inject-match? false, :inject-router? false})) {:inject-match? false, :inject-router? false}))
(defrecord Req [uri request-method]) (defrecord Req [uri request-method path-params])
(defn route->req [route] (defn route->req [route]
(map->Req {:request-method (-> route keys first str/lower-case keyword) (map->Req {:request-method (-> route keys first str/lower-case keyword)
@ -317,6 +317,7 @@
;; 120ns (faster decode params) ;; 120ns (faster decode params)
;; 140µs (java-segment-router) ;; 140µs (java-segment-router)
;; 60ns (java-segment-router, no injects) ;; 60ns (java-segment-router, no injects)
;; 55ns (trie-router, no injects)
(let [req (map->Req {:request-method :get, :uri "/user/repos"})] (let [req (map->Req {:request-method :get, :uri "/user/repos"})]
(title "static") (title "static")
(assert (= {:status 200, :body "/user/repos"} (app req))) (assert (= {:status 200, :body "/user/repos"} (app req)))
@ -328,6 +329,7 @@
;; 560µs (java-segment-router) ;; 560µs (java-segment-router)
;; 490ns (java-segment-router, no injects) ;; 490ns (java-segment-router, no injects)
;; 440ns (java-segment-router, no injects, single-wild-optimization) ;; 440ns (java-segment-router, no injects, single-wild-optimization)
;; 305ns (trie-router, no injects)
(let [req (map->Req {:request-method :get, :uri "/repos/julienschmidt/httprouter/stargazers"})] (let [req (map->Req {:request-method :get, :uri "/repos/julienschmidt/httprouter/stargazers"})]
(title "param") (title "param")
(assert (= {:status 200, :body "/repos/:owner/:repo/stargazers"} (app req))) (assert (= {:status 200, :body "/repos/:owner/:repo/stargazers"} (app req)))
@ -339,6 +341,7 @@
;; 120µs (java-segment-router) ;; 120µs (java-segment-router)
;; 100µs (java-segment-router, no injects) ;; 100µs (java-segment-router, no injects)
;; 90µs (java-segment-router, no injects, single-wild-optimization) ;; 90µs (java-segment-router, no injects, single-wild-optimization)
;; 66µs (trie-router, no injects)
(let [requests (mapv route->req routes)] (let [requests (mapv route->req routes)]
(title "all") (title "all")
(cc/quick-bench (cc/quick-bench

View file

@ -69,7 +69,7 @@
;; 25310 / 25126 ;; 25310 / 25126
"regex" "regex"
;; 88060 / 90778 ;; 112017 / 113811
(title "reitit") (title "reitit")
;; wrk -d ${DURATION:="30s"} http://127.0.0.1:2048/product/foo ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:2048/product/foo
;; wrk -d ${DURATION:="30s"} http://127.0.0.1:2048/twenty/bar ;; wrk -d ${DURATION:="30s"} http://127.0.0.1:2048/twenty/bar

View file

@ -68,10 +68,13 @@
[org.clojure/clojurescript "1.10.439"] [org.clojure/clojurescript "1.10.439"]
;; modules dependencies ;; modules dependencies
;[metosin/reitit "0.2.13"]
[meta-merge]
[metosin/schema-tools] [metosin/schema-tools]
[metosin/spec-tools] [metosin/spec-tools]
[metosin/muuntaja]
[metosin/sieppari]
[metosin/jsonista]
[lambdaisland/deep-diff]
[meta-merge]
[expound "0.7.2"] [expound "0.7.2"]
[orchestra "2018.12.06-2"] [orchestra "2018.12.06-2"]