From 570467b34b3dcb8f23cd3411b63c2ee2f40bf6cf Mon Sep 17 00:00:00 2001 From: Troy Hinckley Date: Tue, 18 Feb 2020 11:39:22 -0700 Subject: [PATCH] (internal): increase performance of post-command-hook (#122) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- org-roam.el | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/org-roam.el b/org-roam.el index b4f0685..b492d2b 100644 --- a/org-roam.el +++ b/org-roam.el @@ -149,7 +149,7 @@ If called interactively, then PARENTS is non-nil." (defvar org-roam-titles-cache (make-hash-table :test #'equal) "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.") (defvar org-roam-last-window nil @@ -463,8 +463,7 @@ Bindings: (insert (format "%s \n\n" content))))) backlinks)) (insert "\n\n* No backlinks!"))) - (read-only-mode 1))) - (setq org-roam-current-file file-path)) + (read-only-mode 1)))) ;;; Show/hide the org-roam buffer (define-inline org-roam--current-visibility () @@ -529,21 +528,20 @@ Valid states are 'visible, 'exists and 'none." "Disable org-roam updating for file. 1. Remove hooks for updating the cache, and the org-roam buffer." - (remove-hook 'post-command-hook #'org-roam--maybe-update-buffer) - (remove-hook 'after-save-hook #'org-roam--update-cache)) + (remove-hook 'post-command-hook #'org-roam--maybe-update-buffer t) + (remove-hook 'after-save-hook #'org-roam--update-cache t)) (cl-defun org-roam--maybe-update-buffer (&key redisplay) "Update `org-roam-buffer' with the necessary information. This needs to be quick/infrequent, because this is run at `post-command-hook'." - (with-current-buffer (window-buffer) - (when (and (get-buffer org-roam-buffer) - (buffer-file-name (current-buffer)) - (file-exists-p (file-truename (buffer-file-name (current-buffer)))) - (or redisplay - (not (string= org-roam-current-file - (file-truename (buffer-file-name (current-buffer))))))) - (org-roam-update (file-truename (buffer-file-name (window-buffer))))))) + (let ((buffer (window-buffer))) + (when (and (or redisplay + (not (eq org-roam--current-buffer buffer))) + (eq 'visible (org-roam--current-visibility))) + (setq org-roam--current-buffer buffer) + (org-roam-update (expand-file-name + (buffer-local-value 'buffer-file-truename buffer)))))) (define-minor-mode org-roam-mode "Global minor mode to automatically update the org-roam buffer."