Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions benchmarks/scopula/core_bench.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(ns scopula.core-bench
(:require [scopula.core :as scopula]
[clj-memory-meter.core :as mm]
[clj-memory-meter.trace :as mmtrace]
[clojure.set :as set]
[criterium.core :as c]))

(def scope-1 "admin/integration/module-instance:read")
(def scope-2 "admin/integration/module-instance")

(def scope-repr-1 (scopula/to-scope-repr scope-1))
(def scope-repr-2 (scopula/to-scope-repr scope-2))

(def lst1 (:path scope-repr-1))
(def lst2 (:path scope-repr-2))

(defn is-sub-list-2 [lst super-lst]
(loop [l1 (seq lst)
l2 (seq super-lst)]
(cond
(nil? l1) true
(nil? l2) false
(= (first l1) (first l2)) (recur (next l1) (next l2))
:else false)))

(def lists
[["admin"]
["admin" "integration" "module-instance"]
["iroh-int"]
["iroh-int" "module-instance"]])

(defn t [f]
(f lst1 lst2))


(defn old-is-sub-list?
"Does `super-lst` begin with `lst`?"
[lst super-lst]
(let [n (count lst)]
(= (take n super-lst) lst)))


(defn check-is-sub-list []
(println "without loop")
(c/quick-bench (t old-is-sub-list?) :gc-summary true)

(println "with loop")
(c/quick-bench (t is-sub-list-2) :gc-summary true))

(defn check-ram-diff []
(mmtrace/trace-var #'t)
(println "Old Version: ")
(mmtrace/trace-var #'old-is-sub-list?)
(mmtrace/with-relative-usage
(t old-is-sub-list?))

(println "Current Branch Version: ")
(mmtrace/trace-var #'scopula/is-sub-list?)
(mmtrace/with-relative-usage
(t scopula/is-sub-list?) ))


(def accesses [#{}
#{:read}
#{:write}
#{:read :write}])

(defn check-access []
(println "with superset?")
(c/quick-bench
(for [ac1 accesses
ac2 accesses]
(set/superset? ac1 ac2))
:gc-summary true)

(println "with every?")
(c/quick-bench
(for [ac1 accesses
ac2 accesses]
(every? ac1 ac2))
:gc-summary true))
11 changes: 7 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
["snapshots" {:url "https://clojars.org/repo" :creds :gpg}]]
:repl-options {:init-ns scopula.core }
:plugins [[lein-cljsbuild "1.1.8"]]
:profiles {:benchmarks {:dependencies [[criterium "0.4.6"]
[com.clojure-goes-fast/clj-memory-meter "0.4.0"]]
:source-paths ["benchmark"]
:jvm-opts ["-Djdk.attach.allowAttachSelf"
"-XX:+EnableDynamicAgentLoading"
]}}
:cljsbuild {:source-paths ["src"]
:compiler {:optimizations :whitespace
:pretty-print false
}
}
)
:pretty-print false}})
11 changes: 8 additions & 3 deletions src/scopula/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@
(defn is-sub-list?
"Does `super-lst` begin with `lst`?"
[lst super-lst]
(let [n (count lst)]
(= (take n super-lst) lst)))
(loop [l1 (seq lst)
l2 (seq super-lst)]
(cond
(nil? l1) true
(nil? l2) false
(= (first l1) (first l2)) (recur (next l1) (next l2))
:else false)))

(defn repr-is-subscope?
"Returns whether the `scope-to-check` is a subscope of the `super-scope`"
[scope-to-check super-scope]
(and (set/superset? (:access super-scope) (:access scope-to-check))
(and (every? (:access super-scope) (:access scope-to-check))
(is-sub-list? (:path super-scope) (:path scope-to-check))))

(defn is-subscope?
Expand Down