(performance): avoid path expansion by referencing cache obj (#184)

Prevent needless repeated calls to org-roam-directory-normalized by having a
reference to the cache object that matches the local buffer.

Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
Herbert Jones
2020-02-25 10:15:38 -06:00
committed by GitHub
parent 19f16e9c64
commit a8d696e6e8
2 changed files with 34 additions and 22 deletions

View File

@ -183,6 +183,13 @@ If called interactively, then PARENTS is non-nil."
(defvar org-roam--cache nil (defvar org-roam--cache nil
"The list of cache separated by directory.") "The list of cache separated by directory.")
(defvar-local org-roam--local-cache-ref nil
"Local reference of the buffer's cache object.")
(defvar-local org-roam--local-cache-id nil
"Local reference of the buffer's cache object id, which is
comparable by \"eq\".")
;;; Utilities ;;; Utilities
(defun org-roam-directory-normalized () (defun org-roam-directory-normalized ()
"Get the org-roam-directory normalized so that it can be used "Get the org-roam-directory normalized so that it can be used
@ -200,17 +207,18 @@ as a unique key."
(defun org-roam--get-directory-cache () (defun org-roam--get-directory-cache ()
"Get the cache object for the current org-roam-directory." "Get the cache object for the current org-roam-directory."
(let* ((cache (org-roam--get-local org-roam--cache))) (unless (eq org-roam--local-cache-id org-roam-directory)
(if cache ;; Prevent needless repeated calls to org-roam-directory-normalized by
cache ;; having a reference to the cache object that matches the local buffer.
(let ((new-cache (org-roam--default-cache))) (setq org-roam--local-cache-ref
(org-roam--set-local org-roam--cache new-cache) (let* ((cache (org-roam--get-local org-roam--cache)))
new-cache)))) (if cache
cache
(defun org-roam--set-directory-cache (data) (let ((new-cache (org-roam--default-cache)))
"Set the cache object for the current org-roam-directory." (org-roam--set-local org-roam--cache new-cache)
(setf (alist-get (org-roam-directory-normalized) new-cache))))
org-roam--cache nil nil #'equal) data)) (setq org-roam--local-cache-id org-roam-directory))
org-roam--local-cache-ref)
(defun org-roam--cache-initialized-p () (defun org-roam--cache-initialized-p ()
"Is cache valid?" "Is cache valid?"
@ -447,11 +455,11 @@ If PREFIX, downcase the title before insertion."
(org-roam--build-cache org-roam-directory)) (org-roam--build-cache org-roam-directory))
(lambda (cache) (lambda (cache)
(let ((org-roam-directory (plist-get cache :directory))) (let ((org-roam-directory (plist-get cache :directory)))
(org-roam--set-directory-cache (let ((obj (org-roam--get-directory-cache)))
(org-roam-cache :initialized t (oset obj initialized t)
:forward-links (plist-get cache :forward) (oset obj forward-links (plist-get cache :forward))
:backward-links (plist-get cache :backward) (oset obj backward-links (plist-get cache :backward))
:titles (plist-get cache :titles))) (oset obj titles (plist-get cache :titles)))
(unless org-roam-mute-cache-build (unless org-roam-mute-cache-build
(message "Org-roam cache built!")) (message "Org-roam cache built!"))
(when on-success (when on-success
@ -460,7 +468,11 @@ If PREFIX, downcase the title before insertion."
(defun org-roam--clear-cache () (defun org-roam--clear-cache ()
"Clears all entries in the caches." "Clears all entries in the caches."
(interactive) (interactive)
(org-roam--set-directory-cache (org-roam--default-cache))) (let ((cache (org-roam--get-directory-cache)))
(oset cache initialized nil)
(oset cache forward-links (make-hash-table :test #'equal))
(oset cache backward-links (make-hash-table :test #'equal))
(oset cache titles (make-hash-table :test #'equal))))
(defun org-roam--default-cache () (defun org-roam--default-cache ()
"A default, uninitialized cache object." "A default, uninitialized cache object."

View File

@ -68,11 +68,11 @@
(defun org-roam--test-build-cache () (defun org-roam--test-build-cache ()
"Builds the caches synchronously." "Builds the caches synchronously."
(let ((cache (org-roam--build-cache org-roam-directory))) (let ((cache (org-roam--build-cache org-roam-directory)))
(org-roam--set-directory-cache (let ((obj (org-roam--get-directory-cache)))
(org-roam-cache :initialized t (oset obj initialized t)
:forward-links (plist-get cache :forward) (oset obj forward-links (plist-get cache :forward))
:backward-links (plist-get cache :backward) (oset obj backward-links (plist-get cache :backward))
:titles (plist-get cache :titles))))) (oset obj titles (plist-get cache :titles)))))
;;; Tests ;;; Tests
(describe "org-roam--build-cache-async" (describe "org-roam--build-cache-async"