Skip to content

Commit

Permalink
Remove unnecessary optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hlship committed Oct 10, 2024
1 parent ea97b5b commit 0303603
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
56 changes: 27 additions & 29 deletions route/src/io/pedestal/http/route/sawtooth/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -304,35 +304,33 @@

(defn- subdivide-by-path
[matched paths]
(if (every? #(= :literal (:category %)) paths)
(match-via-lookup paths)
(let [[completed-paths other-paths] (categorize-by #(-> % :unmatched-terms empty?) paths)
;; This is the case where you have a route that is complete, and other routes that
;; extend from it: i.e. "/user" and "/user/:id". The first will be an empty path
;; (once "user" is matched) and it is handled here, "/user/:id" will be handled as part
;; of by-first-token
completed-paths-matcher (when (seq completed-paths)
;; TODO: Should only be one, right? Unless conflicts.
(let [route (-> completed-paths first :route)]
(if (= "/" (:path route))
(fn root-match-completed [remaining-path params-map]
(when (= "" remaining-path)
[route params-map]))
(fn match-completed [remaining-path params-map]
(when (nil? remaining-path)
[route params-map])))))
by-first-token (group-by #(-> % :unmatched-terms first :token) other-paths)
{params :param
wilds :wild} by-first-token
;; wilds is technically plural *but* should not ever be more than 1 (unless conflicts exist)
by-first-literal-token (dissoc by-first-token :param :wild)
literal-matcher (matcher-by-first-token matched by-first-literal-token)
all-matchers (cond-> []
completed-paths-matcher (conj completed-paths-matcher)
literal-matcher (conj literal-matcher)
params (into (mapv #(matcher-from-path matched %) params))
wilds (into (mapv #(matcher-from-path matched %) wilds)))]
(combine-matchers matched all-matchers))))
(let [[completed-paths other-paths] (categorize-by #(-> % :unmatched-terms empty?) paths)
;; This is the case where you have a route that is complete, and other routes that
;; extend from it: i.e. "/user" and "/user/:id". The first will be an empty path
;; (once "user" is matched) and it is handled here, "/user/:id" will be handled as part
;; of by-first-token
completed-paths-matcher (when (seq completed-paths)
;; TODO: Should only be one, right? Unless conflicts.
(let [route (-> completed-paths first :route)]
(if (= "/" (:path route))
(fn root-match-completed [remaining-path params-map]
(when (= "" remaining-path)
[route params-map]))
(fn match-completed [remaining-path params-map]
(when (nil? remaining-path)
[route params-map])))))
by-first-token (group-by #(-> % :unmatched-terms first :token) other-paths)
{params :param
wilds :wild} by-first-token
;; wilds is technically plural *but* should not ever be more than 1 (unless conflicts exist)
by-first-literal-token (dissoc by-first-token :param :wild)
literal-matcher (matcher-by-first-token matched by-first-literal-token)
all-matchers (cond-> []
completed-paths-matcher (conj completed-paths-matcher)
literal-matcher (conj literal-matcher)
params (into (mapv #(matcher-from-path matched %) params))
wilds (into (mapv #(matcher-from-path matched %) wilds)))]
(combine-matchers matched all-matchers)))

(defn- match-by-path
[*conflicts matched routes]
Expand Down
3 changes: 3 additions & 0 deletions tests/bench/io/pedestal/sawtooth_bench.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(ns io.pedestal.sawtooth-bench
"Used to run comparisons of Sawtooth vs. prefix-tree router performance."
(:require [clojure.string :as string]
[criterium.core :as c]
[io.pedestal.http.route.map-tree :as map-tree]
[net.lewisship.bench :as bench]
[io.pedestal.http.route.prefix-tree :as prefix-tree]
Expand Down Expand Up @@ -202,6 +203,8 @@

(comment

(c/quick-bench (sawtooth/router dynamic-routes))

(time (execute-static :large :sawtooth))

(bench/bench-for {:progress? true
Expand Down

0 comments on commit 0303603

Please sign in to comment.