replace whiteboard printing with ugly print

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
This commit is contained in:
Timothy Pratley
2022-11-10 20:09:57 -08:00
committed by Tienson Qin
parent 03afc9023b
commit 3b6a89c975
6 changed files with 62 additions and 1 deletions

1
.gitignore vendored
View File

@@ -14,6 +14,7 @@ pom.xml.asc
node_modules/
static/
tmp
cljs-test-runner-out
.cpcache/
/src/gen

View File

@@ -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"]}}}

View File

@@ -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))))

View File

@@ -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)))

View File

@@ -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]
@@ -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]]

View File

@@ -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)))