From faaaedaa29a5c41af3397c238cd5e0103e972f47 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 26 Apr 2020 21:27:55 +0300 Subject: [PATCH] Enable quick creation of routers --- modules/reitit-core/src/reitit/core.cljc | 10 ++--- .../clj/reitit/router_creation_perf_test.clj | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 perf-test/clj/reitit/router_creation_perf_test.clj diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index a582badb..25bd3ef8 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -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)) diff --git a/perf-test/clj/reitit/router_creation_perf_test.clj b/perf-test/clj/reitit/router_creation_perf_test.clj new file mode 100644 index 00000000..de9f4b23 --- /dev/null +++ b/perf-test/clj/reitit/router_creation_perf_test.clj @@ -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))