From c7f3683a9bf772e63be2e0a2b699dc15e2646ca6 Mon Sep 17 00:00:00 2001 From: Guillaume Buisson Date: Mon, 11 Aug 2025 16:20:02 +0200 Subject: [PATCH 1/2] Optimize memory usage for 2 core function --- src/scopula/core.cljc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/scopula/core.cljc b/src/scopula/core.cljc index 1518896..77c5d16 100644 --- a/src/scopula/core.cljc +++ b/src/scopula/core.cljc @@ -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? From f321eae53f4433e8a6a6203641863c5fc6c37d8d Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Fri, 29 Aug 2025 16:46:55 +0200 Subject: [PATCH 2/2] Added benchmarks --- benchmarks/scopula/core_bench.clj | 81 +++++++++++++++++++++++++++++++ project.clj | 11 +++-- 2 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 benchmarks/scopula/core_bench.clj diff --git a/benchmarks/scopula/core_bench.clj b/benchmarks/scopula/core_bench.clj new file mode 100644 index 0000000..6c8e74e --- /dev/null +++ b/benchmarks/scopula/core_bench.clj @@ -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)) diff --git a/project.clj b/project.clj index 98c58fc..31205dd 100644 --- a/project.clj +++ b/project.clj @@ -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}})