(perf)db: require optional libraries only once per function (#1944)

Require optional libraries 'org-ref and 'oc only once per function run:
org-roam-db-update-file and org-roam-db-sync will call these requires at
the top level instead of within the link mapper.
This commit is contained in:
Jethro Kuan
2021-11-06 15:03:04 +08:00
committed by GitHub
parent 3e47f198c7
commit 84f58cbc12
2 changed files with 18 additions and 6 deletions

View File

@ -489,8 +489,7 @@ INFO is the org-element parsed buffer."
(org-get-outline-path 'with-self 'use-cache))))) (org-get-outline-path 'with-self 'use-cache)))))
;; For Org-ref links, we need to split the path into the cite keys ;; For Org-ref links, we need to split the path into the cite keys
(when (and source path) (when (and source path)
(if (and (require 'org-ref nil 'noerror) (if (and (boundp 'org-ref-cite-types)
(boundp 'org-ref-cite-types)
(fboundp 'org-ref-split-and-strip-string) (fboundp 'org-ref-split-and-strip-string)
(member type org-ref-cite-types)) (member type org-ref-cite-types))
(progn (progn
@ -499,7 +498,6 @@ INFO is the org-element parsed buffer."
[:insert :into citations [:insert :into citations
:values $v1] :values $v1]
(mapcar (lambda (p) (vector source p (point) properties)) path))) (mapcar (lambda (p) (vector source p (point) properties)) path)))
(org-roam-db-query (org-roam-db-query
[:insert :into links [:insert :into links
:values $v1] :values $v1]
@ -544,16 +542,24 @@ INFO is the org-element parsed buffer."
(secure-hash 'sha1 (current-buffer))))) (secure-hash 'sha1 (current-buffer)))))
;;;; Synchronization ;;;; Synchronization
(defun org-roam-db-update-file (&optional file-path) (defun org-roam-db-update-file (&optional file-path no-require)
"Update Org-roam cache for FILE-PATH. "Update Org-roam cache for FILE-PATH.
If the file does not exist anymore, remove it from the cache. If the file does not exist anymore, remove it from the cache.
If the file exists, update the cache with information."
If the file exists, update the cache with information.
If NO-REQUIRE, don't require optional libraries. Set NO-REQUIRE
when the libraries are already required at some toplevel, e.g.
in `org-roam-db-sync'."
(setq file-path (or file-path (buffer-file-name (buffer-base-buffer)))) (setq file-path (or file-path (buffer-file-name (buffer-base-buffer))))
(let ((content-hash (org-roam-db--file-hash file-path)) (let ((content-hash (org-roam-db--file-hash file-path))
(db-hash (caar (org-roam-db-query [:select hash :from files (db-hash (caar (org-roam-db-query [:select hash :from files
:where (= file $s1)] file-path))) :where (= file $s1)] file-path)))
info) info)
(unless (string= content-hash db-hash) (unless (string= content-hash db-hash)
(unless no-require
(org-roam-require '(org-ref oc)))
(org-roam-with-file file-path nil (org-roam-with-file file-path nil
(emacsql-with-transaction (org-roam-db) (emacsql-with-transaction (org-roam-db)
(save-excursion (save-excursion
@ -585,6 +591,7 @@ If FORCE, force a rebuild of the cache from scratch."
(org-roam-db--close) ;; Force a reconnect (org-roam-db--close) ;; Force a reconnect
(when force (delete-file org-roam-db-location)) (when force (delete-file org-roam-db-location))
(org-roam-db) ;; To initialize the database, no-op if already initialized (org-roam-db) ;; To initialize the database, no-op if already initialized
(org-roam-require '(org-ref oc))
(let* ((gc-cons-threshold org-roam-db-gc-threshold) (let* ((gc-cons-threshold org-roam-db-gc-threshold)
(org-agenda-files nil) (org-agenda-files nil)
(org-roam-files (org-roam-list-files)) (org-roam-files (org-roam-list-files))
@ -606,7 +613,7 @@ If FORCE, force a rebuild of the cache from scratch."
(if (fboundp 'dolist-with-progress-reporter) (if (fboundp 'dolist-with-progress-reporter)
(dolist-with-progress-reporter (file modified-files) (dolist-with-progress-reporter (file modified-files)
"Processing modified files..." "Processing modified files..."
(org-roam-db-update-file file)) (org-roam-db-update-file file 'no-require))
(dolist (file modified-files) (dolist (file modified-files)
(org-roam-db-update-file file)))))) (org-roam-db-update-file file))))))

View File

@ -34,6 +34,11 @@
(require 'org-roam) (require 'org-roam)
(defun org-roam-require (libs)
"Require LIBS."
(dolist (lib libs)
(require lib nil 'noerror)))
;;; String utilities ;;; String utilities
;; TODO Refactor this. ;; TODO Refactor this.
(defun org-roam-replace-string (old new s) (defun org-roam-replace-string (old new s)