Skip to content

Commit be6824e

Browse files
committed
Experiment to improve performance
1 parent 6e09af1 commit be6824e

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/clj_commons/ansi.clj

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,26 +172,25 @@
172172
{:keys [font width pad] :as span-decl} (extract-span-decl first-element)]
173173
(if width
174174
(let [;; Transform this span and everything below it into easily managed span vectors, starting
175-
;; with a reduced version of the span decl.
175+
;; with a version of this span decl.
176176
span-decl' (dissoc span-decl :width :pad)
177177
*length (volatile! 0)
178178
inputs' (into [span-decl'] (normalize-markup inputs *length))
179179
spaces (padding (- width @*length))
180-
;; Added the padding in the desired position; this ensures that the logic that generates
180+
;; Add the padding in the desired position; this ensures that the logic that generates
181181
;; ANSI escape codes occurs correctly, with the added spaces getting the font for this span.
182182
padded (if (= :right pad)
183183
(conj inputs' spaces)
184184
;; An "insert-at" for vectors would be nice
185185
(into [(first inputs') spaces] (next inputs')))]
186186
(recur state padded))
187187
;; Normal (no width tracking)
188-
(let [{:keys [current]} state
189-
state' (reduce collect-markup
190-
(-> state
191-
(update :current update-font-data-from-font-def font)
192-
(update :stack conj current))
193-
inputs)]
194-
(-> state'
188+
(let [{:keys [current]} state]
189+
(-> (reduce collect-markup
190+
(-> state
191+
(update :current update-font-data-from-font-def font)
192+
(update :stack conj current))
193+
inputs)
195194
(assoc :current current
196195
:tracking-width? false)
197196
(update :stack pop)))))
@@ -226,6 +225,7 @@
226225
Nested vectors represent _spans_, a sequence of values with a specific visual representation.
227226
The first element in a span vector declares the visual properties of the span: the color (including
228227
other characteristics such as bold or underline), and the width and padding (described later).
228+
Spans may be nested.
229229
230230
The declaration is usually a keyword, to define just the font.
231231
The font def contains one or more terms, separated by periods.
@@ -248,7 +248,7 @@
248248
=> ...
249249
```
250250
251-
The order of the terms does not matter. Behavior for conflicting terms (`:blue.green.black`)
251+
The order of the terms does not matter. Behavior for conflicting terms (e.g., `:blue.green.black`)
252252
is not defined.
253253
254254
@@ -263,8 +263,8 @@
263263
264264
The core colors are `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, and `white`.
265265
266-
When [[*color-enabled*]] is false, then any font defs are validated, then ignored (no ANSI codes
267-
will be included).
266+
When [[*color-enabled*]] is false, then any font defs are validated, but otherwise ignored (no ANSI codes
267+
will be included in the composed string).
268268
269269
The span's font declaration may also be a map with the following keys:
270270
@@ -292,7 +292,9 @@
292292
[{:font :red
293293
:width 20} message]
294294
295-
This will output the value of `message` in red text, padded with spaces on the left to be 20 characters."
295+
This will output the value of `message` in red text, padded with spaces on the left to be 20 characters.
296+
297+
compose does not truncate a span to a width, it only pads if the span in too short."
296298
{:added "1.4.0"}
297299
[& inputs]
298300
(let [initial-font {:foreground "39"

src/clj_commons/format/exceptions.clj

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,7 @@
560560
"nil"
561561
x))
562562

563-
(defn format-exception*
564-
"Contains the main logic for [[format-exception]], which simply expands
565-
the exception (via [[analyze-exception]]) before invoking this function."
566-
{:added "0.1.21"}
563+
(defn- render-exception
567564
[exception-stack options]
568565
(let [{show-properties? :properties
569566
:or {show-properties? true}} options
@@ -601,7 +598,7 @@
601598
(map exception-f (?reverse modern? exception-stack))
602599
"\n")
603600
root-stack-trace (-> exception-stack last :stack-trace)]
604-
(compose
601+
(list
605602
(when *traditional*
606603
exceptions)
607604

@@ -611,6 +608,14 @@
611608
(when modern?
612609
exceptions))))
613610

611+
(defn format-exception*
612+
"Contains the main logic for [[format-exception]], which simply expands
613+
the exception (via [[analyze-exception]]) before invoking this function."
614+
{:added "0.1.21"}
615+
[exception-stack options]
616+
(compose
617+
(render-exception exception-stack options)))
618+
614619
(defn format-exception
615620
"Formats an exception, returning a single large string.
616621

test/demo.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@
104104
(let [e (make-ex-info)]
105105
(c/bench (e/format-exception e)))
106106

107+
108+
;; 27 Jun 2023 - 767 µs - Clojure 1.11.1, Corretto 17.0.7, M1
109+
(let [e (make-ex-info)
110+
composed (#'e/render-exception (e/analyze-exception e nil) nil)]
111+
(c/bench (compose composed)))
112+
113+
;; 27 Jun 2023 - 182 µs - Clojure 1.11.1, Corretto 17.0.7, M1
114+
(let [e (make-ex-info)]
115+
(c/bench (#'e/render-exception (e/analyze-exception e nil) nil)))
116+
107117
;; 11 Feb 2016 - 213 µs (4 µs std dev) - Clojure 1.8
108118
;; 28 Sep 2018 - 237 µs (8 µs std dev) - Clojure 1.9
109119

0 commit comments

Comments
 (0)