(internal): increase performance of post-command-hook (#122)

file-truename can be an expensive function on a slow filesystem like NFS. I Removed a lot of the unneeded code and refactored to improve performance. In my testing it took the execution time from 13ms per call to 2µs; over a 1000x speedup. This is important since post-command-hook is called with every character you type.
This commit is contained in:
Troy Hinckley
2020-02-18 11:39:22 -07:00
committed by GitHub
parent e84ab1de9a
commit 570467b34b

View File

@ -149,7 +149,7 @@ If called interactively, then PARENTS is non-nil."
(defvar org-roam-titles-cache (make-hash-table :test #'equal) (defvar org-roam-titles-cache (make-hash-table :test #'equal)
"Cache containing titles for org-roam files.") "Cache containing titles for org-roam files.")
(defvar org-roam-current-file nil (defvar org-roam--current-buffer nil
"Currently displayed file in `org-roam' buffer.") "Currently displayed file in `org-roam' buffer.")
(defvar org-roam-last-window nil (defvar org-roam-last-window nil
@ -463,8 +463,7 @@ Bindings:
(insert (format "%s \n\n" content))))) (insert (format "%s \n\n" content)))))
backlinks)) backlinks))
(insert "\n\n* No backlinks!"))) (insert "\n\n* No backlinks!")))
(read-only-mode 1))) (read-only-mode 1))))
(setq org-roam-current-file file-path))
;;; Show/hide the org-roam buffer ;;; Show/hide the org-roam buffer
(define-inline org-roam--current-visibility () (define-inline org-roam--current-visibility ()
@ -529,21 +528,20 @@ Valid states are 'visible, 'exists and 'none."
"Disable org-roam updating for file. "Disable org-roam updating for file.
1. Remove hooks for updating the cache, and the org-roam buffer." 1. Remove hooks for updating the cache, and the org-roam buffer."
(remove-hook 'post-command-hook #'org-roam--maybe-update-buffer) (remove-hook 'post-command-hook #'org-roam--maybe-update-buffer t)
(remove-hook 'after-save-hook #'org-roam--update-cache)) (remove-hook 'after-save-hook #'org-roam--update-cache t))
(cl-defun org-roam--maybe-update-buffer (&key redisplay) (cl-defun org-roam--maybe-update-buffer (&key redisplay)
"Update `org-roam-buffer' with the necessary information. "Update `org-roam-buffer' with the necessary information.
This needs to be quick/infrequent, because this is run at This needs to be quick/infrequent, because this is run at
`post-command-hook'." `post-command-hook'."
(with-current-buffer (window-buffer) (let ((buffer (window-buffer)))
(when (and (get-buffer org-roam-buffer) (when (and (or redisplay
(buffer-file-name (current-buffer)) (not (eq org-roam--current-buffer buffer)))
(file-exists-p (file-truename (buffer-file-name (current-buffer)))) (eq 'visible (org-roam--current-visibility)))
(or redisplay (setq org-roam--current-buffer buffer)
(not (string= org-roam-current-file (org-roam-update (expand-file-name
(file-truename (buffer-file-name (current-buffer))))))) (buffer-local-value 'buffer-file-truename buffer))))))
(org-roam-update (file-truename (buffer-file-name (window-buffer)))))))
(define-minor-mode org-roam-mode (define-minor-mode org-roam-mode
"Global minor mode to automatically update the org-roam buffer." "Global minor mode to automatically update the org-roam buffer."