Enable quick creation of routers

This commit is contained in:
Tommi Reiman 2020-04-26 21:27:55 +03:00
parent c93b42cd6f
commit faaaedaa29
2 changed files with 47 additions and 6 deletions

View file

@ -364,10 +364,10 @@
([raw-routes]
(router raw-routes {}))
([raw-routes opts]
(let [{:keys [router] :as opts} (merge (default-router-options) opts)]
(let [{:keys [router conflicts] :as opts} (merge (default-router-options) opts)]
(try
(let [routes (impl/resolve-routes raw-routes opts)
path-conflicting (impl/path-conflicting-routes routes opts)
path-conflicting (if-not (and router (not conflicts)) (impl/path-conflicting-routes routes opts))
name-conflicting (impl/name-conflicting-routes routes)
compiled-routes (impl/compile-routes routes opts)
wilds? (boolean (some (impl/->wild-route? opts) compiled-routes))
@ -380,10 +380,8 @@
all-wilds? trie-router
:else mixed-router)]
(when-let [conflicts (:conflicts opts)]
(when-let [conflict-report (impl/unresolved-conflicts
path-conflicting)]
(conflicts conflict-report)))
(when-let [conflict-report (and conflicts (impl/unresolved-conflicts path-conflicting))]
(conflicts conflict-report))
(when name-conflicting
(exception/fail! :name-conflicts name-conflicting))

View file

@ -0,0 +1,43 @@
(ns reitit.router-creation-perf-test
(:require [reitit.perf-utils :refer [bench!]]
[reitit.core :as r]
[clojure.string :as str])
(:import (java.util Random)))
;;
;; start repl with `lein perf repl`
;; perf measured with the following setup:
;;
;; Model Name: MacBook Pro
;; Model Identifier: MacBookPro113
;; Processor Name: Intel Core i7
;; Processor Speed: 2,5 GHz
;; Number of Processors: 1
;; Total Number of Cores: 4
;; L2 Cache (per Core): 256 KB
;; L3 Cache: 6 MB
;; Memory: 16 GB
;;
(defn random [^long seed]
(Random. seed))
(defn rand-str [^Random rnd len]
(apply str (take len (repeatedly #(char (+ (.nextInt rnd 26) 97))))))
(defn route [rnd]
(str/join "/" (repeatedly (+ 2 (.nextInt rnd 8)) (fn [] (rand-str rnd (.nextInt rnd 10))))))
(def hundred-routes
(let [rnd (random 1)]
(mapv (fn [n] [(route rnd) (keyword (str "route" n))]) (range 100))))
(defn bench-routers []
;; 104ms
(bench! "default" (r/router hundred-routes))
;; 7ms
(bench! "linear" (r/router hundred-routes {:router r/linear-router, :conflicts nil})))
(comment
(bench-routers))