Skip to content

Commit

Permalink
purpose-x-kill: Don't calculate 'other-buffers' unless necessary
Browse files Browse the repository at this point in the history
Fixes bmag#149
The problem is that 'gathering a list of other buffers with the same purpose' conses due to an inevitable maphash later on, and  even though it's not a ton, it adds up quick because this function is called on every kill-buffer call. Even for the tons and tons of background temp buffers used by company, helm, sly, and others.

The fix: We don't need to figure out the list of other buffers unless we actually come across a window that was displaying the killed buffer. So this fix just delays calculating it until we actually need it.
  • Loading branch information
Zulu-Inuoe committed Jun 17, 2019
1 parent eddfcea commit f42085c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions window-purpose-x.el
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,22 @@ This function removes the buffer denoted by BUFFER-OR-NAME from all
window-local buffer lists."
(interactive "bBuffer to replace: ")
(let* ((buffer (window-normalize-buffer buffer-or-name))
(purpose (purpose-buffer-purpose buffer))
(other-buffers (delete buffer (purpose-buffers-with-purpose purpose))))
;; Delay calculating other-buffers until we need it
;; This prevents unnecessary calculations on temporary
;; buffers created by `with-temp-buffer' and other likewise
;; non-displayed buffers
(have-other-buffers nil)
(other-buffers nil))
(dolist (window (window-list-1 nil nil t))
(if (eq (window-buffer window) buffer)
(unless (window--delete window t t)
(let ((dedicated (purpose-window-purpose-dedicated-p window))
(deletable (window-deletable-p window)))
(let* ((purpose (purpose-buffer-purpose buffer))
(other-buffers (if have-other-buffers
other-buffers
(setq have-other-buffers t
other-buffers (delete buffer (purpose-buffers-with-purpose purpose)))))
(dedicated (purpose-window-purpose-dedicated-p window))
(deletable (window-deletable-p window)))
(cond
((and dedicated other-buffers)
;; dedicated, so replace with a buffer with the same purpose
Expand Down

0 comments on commit f42085c

Please sign in to comment.