+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]
[clojure.edn :as edn]
[reitit.ring :as ring]
[reitit.core :as r])
(:import (reitit SegmentTrie)))
[reitit.core :as r]))
;;
;; start repl with `lein perf repl`
@ -87,34 +86,23 @@
{:inject-match? false, :inject-router? false}))
(comment
(let [request {:request-method :get
:uri "/user/1234/profile/compact/"}]
;; OLD: 1338ns
;; NEW: 981ns
;; JAVA: 805ns
;; NO-INJECT: 704ns
#_(cc/quick-bench
(handler-reitit request))
(let [request {:request-method :get, :uri "/user/1234/profile/compact/"}]
;; 1338ns (old)
;; 981ns (new)
;; 805ns (java)
;; 704ns (no-inject)
;; 458ns (trie)
(cc/quick-bench
(handler-reitit request))
(handler-reitit request)))
(comment
;; 281ns
;; 190ns
(let [router (r/router [["/user/:id/profile/:type" ::1]
["/user/:id/permissions" ::2]
["/company/:cid/dept/:did" ::3]
["/this/is/a/static/route" ::4]])]
#_(cc/quick-bench
(r/match-by-path router "/user/1234/profile/compact"))
(cc/quick-bench
(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)
{:inject-match? false, :inject-router? false}))
(defrecord Req [uri request-method])
(defrecord Req [uri request-method path-params])
(defn route->req [route]
(map->Req {:request-method (-> route keys first str/lower-case keyword)
@ -317,6 +317,7 @@
;; 120ns (faster decode params)
;; 140µs (java-segment-router)
;; 60ns (java-segment-router, no injects)
;; 55ns (trie-router, no injects)
(let [req (map->Req {:request-method :get, :uri "/user/repos"})]
(title "static")
(assert (= {:status 200, :body "/user/repos"} (app req)))
@ -328,6 +329,7 @@
;; 560µs (java-segment-router)
;; 490ns (java-segment-router, no injects)
;; 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"})]
(title "param")
(assert (= {:status 200, :body "/repos/:owner/:repo/stargazers"} (app req)))
@ -339,6 +341,7 @@
;; 120µs (java-segment-router)
;; 100µs (java-segment-router, no injects)
;; 90µs (java-segment-router, no injects, single-wild-optimization)
;; 66µs (trie-router, no injects)
(let [requests (mapv route->req routes)]
(title "all")
(cc/quick-bench

View file

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

View file

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