Skip to content

Commit

Permalink
Experiment to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hlship committed Jun 27, 2023
1 parent 6e09af1 commit be6824e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
28 changes: 15 additions & 13 deletions src/clj_commons/ansi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,25 @@
{:keys [font width pad] :as span-decl} (extract-span-decl first-element)]
(if width
(let [;; Transform this span and everything below it into easily managed span vectors, starting
;; with a reduced version of the span decl.
;; with a version of this span decl.
span-decl' (dissoc span-decl :width :pad)
*length (volatile! 0)
inputs' (into [span-decl'] (normalize-markup inputs *length))
spaces (padding (- width @*length))
;; Added the padding in the desired position; this ensures that the logic that generates
;; Add the padding in the desired position; this ensures that the logic that generates
;; ANSI escape codes occurs correctly, with the added spaces getting the font for this span.
padded (if (= :right pad)
(conj inputs' spaces)
;; An "insert-at" for vectors would be nice
(into [(first inputs') spaces] (next inputs')))]
(recur state padded))
;; Normal (no width tracking)
(let [{:keys [current]} state
state' (reduce collect-markup
(-> state
(update :current update-font-data-from-font-def font)
(update :stack conj current))
inputs)]
(-> state'
(let [{:keys [current]} state]
(-> (reduce collect-markup
(-> state
(update :current update-font-data-from-font-def font)
(update :stack conj current))
inputs)
(assoc :current current
:tracking-width? false)
(update :stack pop)))))
Expand Down Expand Up @@ -226,6 +225,7 @@
Nested vectors represent _spans_, a sequence of values with a specific visual representation.
The first element in a span vector declares the visual properties of the span: the color (including
other characteristics such as bold or underline), and the width and padding (described later).
Spans may be nested.
The declaration is usually a keyword, to define just the font.
The font def contains one or more terms, separated by periods.
Expand All @@ -248,7 +248,7 @@
=> ...
```
The order of the terms does not matter. Behavior for conflicting terms (`:blue.green.black`)
The order of the terms does not matter. Behavior for conflicting terms (e.g., `:blue.green.black`)
is not defined.
Expand All @@ -263,8 +263,8 @@
The core colors are `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, and `white`.
When [[*color-enabled*]] is false, then any font defs are validated, then ignored (no ANSI codes
will be included).
When [[*color-enabled*]] is false, then any font defs are validated, but otherwise ignored (no ANSI codes
will be included in the composed string).
The span's font declaration may also be a map with the following keys:
Expand Down Expand Up @@ -292,7 +292,9 @@
[{:font :red
:width 20} message]
This will output the value of `message` in red text, padded with spaces on the left to be 20 characters."
This will output the value of `message` in red text, padded with spaces on the left to be 20 characters.
compose does not truncate a span to a width, it only pads if the span in too short."
{:added "1.4.0"}
[& inputs]
(let [initial-font {:foreground "39"
Expand Down
15 changes: 10 additions & 5 deletions src/clj_commons/format/exceptions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,7 @@
"nil"
x))

(defn format-exception*
"Contains the main logic for [[format-exception]], which simply expands
the exception (via [[analyze-exception]]) before invoking this function."
{:added "0.1.21"}
(defn- render-exception
[exception-stack options]
(let [{show-properties? :properties
:or {show-properties? true}} options
Expand Down Expand Up @@ -601,7 +598,7 @@
(map exception-f (?reverse modern? exception-stack))
"\n")
root-stack-trace (-> exception-stack last :stack-trace)]
(compose
(list
(when *traditional*
exceptions)

Expand All @@ -611,6 +608,14 @@
(when modern?
exceptions))))

(defn format-exception*
"Contains the main logic for [[format-exception]], which simply expands
the exception (via [[analyze-exception]]) before invoking this function."
{:added "0.1.21"}
[exception-stack options]
(compose
(render-exception exception-stack options)))

(defn format-exception
"Formats an exception, returning a single large string.
Expand Down
10 changes: 10 additions & 0 deletions test/demo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
(let [e (make-ex-info)]
(c/bench (e/format-exception e)))


;; 27 Jun 2023 - 767 µs - Clojure 1.11.1, Corretto 17.0.7, M1
(let [e (make-ex-info)
composed (#'e/render-exception (e/analyze-exception e nil) nil)]
(c/bench (compose composed)))

;; 27 Jun 2023 - 182 µs - Clojure 1.11.1, Corretto 17.0.7, M1
(let [e (make-ex-info)]
(c/bench (#'e/render-exception (e/analyze-exception e nil) nil)))

;; 11 Feb 2016 - 213 µs (4 µs std dev) - Clojure 1.8
;; 28 Sep 2018 - 237 µs (8 µs std dev) - Clojure 1.9

Expand Down

0 comments on commit be6824e

Please sign in to comment.