Skip to content

Commit

Permalink
perf(rendering): cache results of whale-line--format-side
Browse files Browse the repository at this point in the history
  • Loading branch information
Walheimat committed Jan 7, 2024
1 parent 448a300 commit de78ac0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Keyword `:wraps` for augments. This is a shorthand to `:after-while`
and passing the name of the render function of another segment.
- Results `whale-line--format-side` are now cached until window
configuration changes.

## [0.8.3]

Expand Down
12 changes: 12 additions & 0 deletions test/whale-line-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,18 @@

(should-error (whale-line--symbol-for-type 'segment 'something)))

(ert-deftest whale-line--format-side--uses-cache ()
(let ((whale-line--render-cache (make-hash-table :test 'equal)))

(bydi ((:mock whale-line--render :return "hello")
(:mock format-mode-line :with bydi-rf))

(whale-line--format-side :left)

(should (equal (nth 0 (hash-table-keys whale-line--render-cache)) '(:left nil)))

(should (string= "hello" (whale-line--format-side :left))))))

;;; whale-line-test.el ends here

;; Local Variables:
Expand Down
10 changes: 8 additions & 2 deletions whale-line.el
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ logging."

(defvar whale-line--padded-cache (make-hash-table))
(defvar whale-line--space-cache (make-hash-table))
(defvar whale-line--render-cache (make-hash-table :test 'equal))

(defvar whale-line--stateful-timer nil)

Expand Down Expand Up @@ -189,7 +190,11 @@ formatter to call."
"Get the formatted SIDE.
Optionally FILTER out low priority segments."
(format-mode-line (whale-line--render side filter)))
(if-let* ((value (gethash (list side filter) whale-line--render-cache)))
value
(let ((render (format-mode-line (whale-line--render side filter))))

(puthash (list side filter) render whale-line--render-cache))))

;;;; Space calculation

Expand Down Expand Up @@ -270,7 +275,8 @@ This uses `string-pixel-width' for Emacs 29+, otherwise
(defun whale-line--clear-caches ()
"Clear all caches."
(clrhash whale-line--padded-cache)
(clrhash whale-line--space-cache))
(clrhash whale-line--space-cache)
(clrhash whale-line--render-cache))

;;;; Building segments

Expand Down

0 comments on commit de78ac0

Please sign in to comment.