Skip to content

Commit

Permalink
replace whiteboard printing with ugly print
Browse files Browse the repository at this point in the history
Prints fast with newlines

clojure -M:test:bench

Testing frontend.benchmark-test-runner
[], (with-out-str (pprint/pprint onboarding)), 10 runs, 2950 msecs
[], (with-out-str (fipp/pprint onboarding)), 10 runs, 2447 msecs
[], (up/ugly-pr-str onboarding), 10 runs, 94 msecs
[], (pr-str onboarding), 10 runs, 82 msecs
  • Loading branch information
timothypratley authored and tiensonqin committed Nov 14, 2022
1 parent 03afc90 commit 3b6a89c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pom.xml.asc
node_modules/
static/
tmp
cljs-test-runner-out

.cpcache/
/src/gen
Expand Down
5 changes: 5 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
org.clojars.knubie/cljs-run-test {:mvn/version "1.0.1"}}
:main-opts ["-m" "shadow.cljs.devtools.cli"]}

:bench {:extra-paths ["src/bench/"]
:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}
fipp/fipp {:mvn/version "0.6.26"}}
:main-opts ["-m" "cljs-test-runner.main" "-d" "src/bench" "-n" "frontend.benchmark-test-runner"]}

;; Use :replace-deps for tools. See https://github.com/clj-kondo/clj-kondo/issues/1536#issuecomment-1013006889
:clj-kondo {:replace-deps {clj-kondo/clj-kondo {:mvn/version "2022.10.14"}}
:main-opts ["-m" "clj-kondo.main"]}}}
28 changes: 28 additions & 0 deletions src/bench/frontend/benchmark_test_runner.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns frontend.benchmark-test-runner
"Runs a benchmark"
(:require [clojure.edn :as edn]
[frontend.macros :refer [slurped]]
[frontend.modules.file.uprint :as up]
[clojure.pprint :as pprint]
[clojure.test :refer [run-tests deftest testing]]
[fipp.edn :as fipp]))

(def onboarding
(edn/read-string (slurped "resources/whiteboard/onboarding.edn")))

(deftest test-pp-str
(testing "pp-str benchmark"
(simple-benchmark []
(with-out-str (pprint/pprint onboarding))
10)
(simple-benchmark []
(with-out-str (fipp/pprint onboarding))
10)
(simple-benchmark []
(up/ugly-pr-str onboarding)
10)
(simple-benchmark []
(pr-str onboarding)
10)
;; uncomment to see the output
#_(println (up/ugly-pr-str onboarding))))
9 changes: 9 additions & 0 deletions src/bench/frontend/macros.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns frontend.macros
#?(:cljs (:require-macros [frontend.macros])
:clj (:require [clojure.edn :as edn])))

#?(:clj
(defmacro slurped
"Like slurp, but at compile time"
[filename]
(slurp filename)))
3 changes: 2 additions & 1 deletion src/main/frontend/modules/file/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.utils :as db-utils]
[frontend.modules.file.uprint :as up]
[frontend.state :as state]
[frontend.util.property :as property]
[frontend.util.fs :as fs-util]
Expand Down Expand Up @@ -140,7 +141,7 @@
file-path (-> (db-utils/entity file-db-id) :file/path)]
(if (and (string? file-path) (not-empty file-path))
(let [new-content (if (= "whiteboard" (:block/type page-block))
(pr-str {:blocks tree
(up/ugly-pr-str {:blocks tree
:pages (list (remove-transit-ids page-block))})
(tree->file-content tree {:init-level init-level}))
files [[file-path new-content]]
Expand Down
17 changes: 17 additions & 0 deletions src/main/frontend/modules/file/uprint.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns frontend.modules.file.uprint)

(defn print-prefix-map* [prefix m print-one writer opts]
(pr-sequential-writer
writer
(fn [e w opts]
(do (print-one (key e) w opts)
(-write w \space)
(print-one (val e) w opts)))
(str prefix "{") \newline "}"
opts (seq m)))

(defn ugly-pr-str
"Ugly printing fast, with newline per block"
[x]
(with-redefs [print-prefix-map print-prefix-map*]
(pr-str x)))

0 comments on commit 3b6a89c

Please sign in to comment.