Skip to content

Commit

Permalink
Crude memory profiling in REPL (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski authored Aug 21, 2024
1 parent 1ee2fe2 commit 484ef9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/macaw/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
;; This may result in duplicate components, which are left to the caller to deduplicate.
;; In Metabase's case, this is done during the stage where the database metadata is queried.
(->> (collect/query->components statement (merge {:preserve-identifiers? true} opts))
(walk/prewalk (fn [x]
(if (string? x)
(unescape-keywords x (:non-reserved-words opts))
x)))))
(walk/postwalk (fn [x]
(if (string? x)
(unescape-keywords x (:non-reserved-words opts))
x)))))

(defn replace-names
"Given an SQL query, apply the given table, column, and schema renames.
Expand All @@ -86,11 +86,11 @@
;; If we decide that it's OK to normalize whitespace etc. during replacement, then we can use the same helper.
(let [sql' (escape-keywords (str/replace sql #"(?m)^\n" " \n") (:non-reserved-words opts))
opts' (select-keys opts [:case-insensitive :quotes-preserve-case? :allow-unused?])
renames' (walk/prewalk (fn [x]
(if (string? x)
(escape-keywords x (:non-reserved-words opts))
x))
renames)
renames' (walk/postwalk (fn [x]
(if (string? x)
(escape-keywords x (:non-reserved-words opts))
x))
renames)
parsed (parsed-query sql' opts)]
(-> (rewrite/replace-names sql' parsed renames' opts')
(str/replace #"(?m)^ \n" "\n")
Expand Down
32 changes: 23 additions & 9 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,29 @@ from foo")
raw-components)))))

(comment
(require 'hashp.core)
(require 'virgil)
(require 'clojure.tools.namespace.repl)
(virgil/watch-and-recompile ["java"] :post-hook clojure.tools.namespace.repl/refresh-all)
(require 'user) ;; kondo, really
(require '[clj-async-profiler.core :as prof])
(prof/serve-ui 8080)

(source-columns "WITH cte AS (SELECT x FROM t1) SELECT x, y FROM t2")
(defn- simple-benchmark []
(source-columns "SELECT x FROM t"))

(anonymize-query "SELECT x FROM a")
(anonymize-fixture :snowflake)
(anonymize-fixture :snowflakelet)
(defn- complex-benchmark []
(count
(source-columns
(query-fixture :snowflake))))

)
(user/time+ (simple-benchmark))
(prof/profile {:event :alloc}
(dotimes [_ 1000] (simple-benchmark)))

(user/time+ (complex-benchmark))
(prof/profile {:event :alloc}
(dotimes [_ 100] (complex-benchmark)))

(anonymize-query "SELECT x FROM a")
(anonymize-fixture :snowflakelet)

(require 'virgil)
(require 'clojure.tools.namespace.repl)
(virgil/watch-and-recompile ["java"] :post-hook clojure.tools.namespace.repl/refresh-all))

0 comments on commit 484ef9e

Please sign in to comment.