diff --git a/modules/reitit-core/java-src/reitit/SegmentTrie.java b/modules/reitit-core/java-src/reitit/SegmentTrie.java index ffd42936..c5cadcac 100644 --- a/modules/reitit-core/java-src/reitit/SegmentTrie.java +++ b/modules/reitit-core/java-src/reitit/SegmentTrie.java @@ -23,7 +23,7 @@ public class SegmentTrie { return segments; } - static String encode(String s) { + private static String encode(String s) { try { if (s.contains("%")) { String _s = s; @@ -293,19 +293,6 @@ public class SegmentTrie { return matcher.match(0, split(path), new Match()); } - public static Matcher sample() { - Map m1 = new HashMap<>(); - m1.put("profile", new WildMatcher(Keyword.intern("type"), new DataMatcher(1))); - m1.put("permissions", new DataMatcher(2)); - - Map m2 = new HashMap<>(); - m2.put("user", new WildMatcher(Keyword.intern("id"), new StaticMapMatcher(m1))); - m2.put("company", new WildMatcher(Keyword.intern("cid"), new StaticMatcher("dept", new WildMatcher(Keyword.intern("did"), new DataMatcher(3))))); - m2.put("public", new CatchAllMatcher(Keyword.intern("*"), new DataMatcher(4))); - m2.put("kikka", new LinearMatcher(Arrays.asList(new StaticMatcher("ping", new DataMatcher(5)), new WildMatcher(Keyword.intern("id"), new StaticMatcher("ping", new DataMatcher(6)))))); - return new StaticMapMatcher(m2); - } - public static void main(String[] args) { SegmentTrie trie = new SegmentTrie(); @@ -314,25 +301,5 @@ public class SegmentTrie { System.err.println(m); System.err.println(m.getClass()); System.out.println(lookup(m, "/repos/metosin/reitit/stargazers")); - /* - SegmentTrie trie = new SegmentTrie(); - trie.add("/user/:id/profile/:type", 1); - trie.add("/user/:id/permissions", 2); - trie.add("/company/:cid/dept/:did", 3); - trie.add("/this/is/a/static/route", 4); - Matcher m = trie.matcher(); - System.out.println(m); - - System.err.println(lookup(m, "/this/is/a/static/route")); - System.err.println(lookup(m, "/user/1234/profile/compact")); - System.err.println(lookup(m, "/company/1234/dept/5678")); - System.err.println(); - */ - /* - System.err.println(lookup(sample(), "/user/1234/profile/compact")); - System.err.println(lookup(sample(), "/public/images/logo.jpg")); - System.err.println(lookup(sample(), "/kikka/ping")); - System.err.println(lookup(sample(), "/kikka/kukka/ping")); - */ } } diff --git a/perf-test/clj/reitit/calf_perf_test.clj b/perf-test/clj/reitit/calf_perf_test.clj index 298b450c..32f678be 100644 --- a/perf-test/clj/reitit/calf_perf_test.clj +++ b/perf-test/clj/reitit/calf_perf_test.clj @@ -3,12 +3,9 @@ [reitit.perf-utils :refer :all] [ring.util.codec] [reitit.impl] - [reitit.segment :as segment] - [reitit.impl :as impl] [reitit.ring :as ring] [reitit.core :as r]) - (:import (reitit SegmentTrie SegmentTrie$Matcher) - (calfpath Util))) + (:import (reitit SegmentTrie))) ;; ;; start repl with `lein perf repl` @@ -85,237 +82,37 @@ :handler (fn [_] (h3x))}] ["/this/is/a/static/route" {:put (fn [_] (h40)) :handler (fn [_] (h4x))}]]) - (fn [_] (hxx)))) - -#_(let [request {:request-method :put - :uri "/this/is/a/static/route"}] - (handler-reitit request) - (cc/quick-bench - (handler-reitit request))) - -(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)) - (handler-reitit request)) - -(comment - (impl/segments "/user/1234/profile/compact") - ;; 145ns - (cc/quick-bench - (impl/segments "/user/1234/profile/compact"))) - -(comment - (SegmentTrie/split "/user/1234/profile/compact") - ;; 91ns - (cc/quick-bench - (SegmentTrie/split "/user/1234/profile/compact"))) - -(comment - (let [router (r/router ["/user/:id/profile/:type"])] - (cc/quick-bench - (r/match-by-path router "/user/1234/profile/compact")))) - -(let [lookup ^SegmentTrie$Matcher (SegmentTrie/sample)] - (SegmentTrie/lookup lookup "/user/1234/profile/compact") - #_(cc/quick-bench - (SegmentTrie/lookup lookup "/user/1234/profile/compact"))) - -(let [router (r/router [["/user/:id" ::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")) - (r/match-by-path router "/user/1234")) - -;; 281ns -(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")) - (r/match-by-path router "/user/1234/profile/compact")) - -(read-string - (str - (.matcher - (doto (SegmentTrie.) - (.add "/user" 1) - #_(.add "/user/id/permissions" 2) - (.add "/user/id/permissions2" 3))))) - -(SegmentTrie/lookup - (.matcher - (doto (SegmentTrie.) - (.add "/user/1" 1) - (.add "/user/1/permissions" 2))) - "/user/1") - -(.matcher - (doto (SegmentTrie.) - (.add "/user/1" 1) - (.add "/user/1/permissions" 2))) - -;; 137ns -(let [m (.matcher - (doto (SegmentTrie.) - (.add "/user/:id/profile/:type" 1)))] - #_(cc/quick-bench - (SegmentTrie/lookup m "/user/1234/profile/compact")) - (SegmentTrie/lookup m "/user/1234/profile/compact")) - -(comment - - (let [matcher ^SegmentTrie$Matcher (SegmentTrie/sample)] - (SegmentTrie/lookup matcher "/user/1234/profile/compact") - (cc/quick-bench - (SegmentTrie/lookup matcher "/user/1234/profile/compact"))) - - ;; 173ns - (let [lookup ^SegmentTrie$Matcher (SegmentTrie/tree2)] - (SegmentTrie/lookup lookup "/user/1234/profile/compact") - (cc/quick-bench - (SegmentTrie/lookup lookup "/user/1234/profile/compact"))) - - - ;; 140ns - (let [lookup ^SegmentTrie$Matcher (SegmentTrie/tree1)] - (SegmentTrie/lookup lookup "/user/1234/profile/compact") - (cc/quick-bench - (SegmentTrie/lookup lookup "/user/1234/profile/compact"))) - - ;; 849ns (clojure, original) - ;; 599ns (java, initial) - ;; 173ns (fast split) - (let [router (r/router ["/user/:id/profile/:type"])] - (r/match-by-path router "/user/1234/profile/compact") - (cc/quick-bench - (r/match-by-path router "/user/1234/profile/compact"))) - - ;; 849ns (clojure, original) - ;; 599ns (java, initial) - ;; 173ns (java, optimized) - (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"))) - - ;; 131ns - (let [route ["/user/" :id "/profile/" :type "/"]] - (cc/quick-bench - (Util/matchURI "/user/1234/profile/compact/" route))) - - ;; 728ns - (cc/quick-bench - (r/match-by-path ring/ROUTER (:uri ring/REQUEST)))) - -(set! *warn-on-reflection* true) + (fn [_] (hxx)) + {:inject-match? false, :inject-router? false})) (comment (let [request {:request-method :get :uri "/user/1234/profile/compact/"}] - (time - (dotimes [_ 1000] - (handler-reitit request))))) - -(comment - (Util/matchURI "/user/1234/profile/compact/" ["/user/" :id "/profile/" :type "/"]) - (cc/quick-bench - (Util/matchURI "/user/1234/profile/compact/" ["/user/" :id "/profile/" :type "/"])) - - (cc/quick-bench - (SegmentTrie/split "/user/1234/profile/compact/")) - - (cc/quick-bench - (.split "/user/1234/profile/compact/" "/" 666))) - -(import '[reitit Segment2]) - -(def paths ["kikka" "kukka" "kakka" "abba" "jabba" "1" "2" "3" "4"]) -(def a (Segment2/createArray paths)) -(def h (Segment2/createHash paths)) - -(set! *warn-on-reflection* true) - -(comment - (let [segment (segment/create - [["/user/:id/profile/:type/" 1] - ["/user/:id/permissions/" 2] - ["/company/:cid/dept/:did/" 3] - ["/this/is/a/static/route" 4]])] - (segment/lookup segment "/user/1/profile/compat/") - - ;; OLD: 602ns - ;; NEW: 472ns - (cc/quick-bench - (segment/lookup segment "/user/1/profile/compat/")) - - ;; OLD: 454ns - ;; NEW: 372ns - (cc/quick-bench - (segment/lookup segment "/user/1/permissions/")))) - -#_(cc/quick-bench - (SegmentTrie/split "/user/1/profile/compat")) - -#_(SegmentTrie/split "/user/1/profile/compat") - -#_(cc/quick-bench - (Segment2/hashLookup h "abba")) - + ;; OLD: 1338ns + ;; NEW: 981ns + ;; JAVA: 805ns + ;; NO-INJECT: 704ns + #_(cc/quick-bench + (handler-reitit request)) + (handler-reitit request))) (comment - (cc/quick-bench - (dotimes [_ 1000] - ;; 7ns - (Segment2/arrayLookup a "abba"))) - - (cc/quick-bench - (dotimes [_ 1000] - ;; 3ns - (Segment2/hashLookup h "abba")))) + ;; 281ns + (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")) + (r/match-by-path router "/user/1234/profile/compact"))) (comment - (time - (dotimes [_ 1000] - (Util/matchURI "/user/1234/profile/compact/" ["/user/" :id "/profile/" :type "/"]))) + (read-string + (str + (.matcher + (doto (SegmentTrie.) + (.add "/user" 1) + #_(.add "/user/id/permissions" 2) + (.add "/user/id/permissions2" 3)))))) - - (time - (let [s (s/create [["/user/:id/profile/:type/" 1]])] - (dotimes [_ 1000] - (s/lookup s "/user/1234/profile/compact/")))) - - (let [m {"/abba" 1}] - (time - (dotimes [_ 1000] - (get m "/abba")))) - - (time - (dotimes [_ 1000] - (Util/matchURI "/user/1234/profile/compact/" 0 ["/user/" :id "/profile/" :type "/"] false))) - - ;; 124ns - (cc/quick-bench - (Util/matchURI "/user/1234/profile/compact/" 0 ["/user/" :id "/profile/" :type "/"] false)) - - ;; 166ns - (cc/quick-bench - (impl/segments "/user/1234/profile/compact/")) - - ;; 597ns - (let [s (s/create [["/user/:id/profile/:type/" 1]])] - (cc/quick-bench - (s/lookup s "/user/1234/profile/compact/"))) - - (let [s (s/create [["/user/:id/profile/:type/" 1]])] - (s/lookup s "/user/1234/profile/compact/")))