Org roam db optimisation

Hello Org Roam community, I have been working on an advice function to enable not wasting Cpu cycle on updating the db file unnecessarily, the db file is stored on your disk and unless modifications are made it is not prudent to waste cpu cycle on syncing it esp on every Emacs restart.

I realised that org-roam doesn’t sync the database immediately when files are deleted/added but waits for a restart to do it, this is a design choice that probably helps in giving the illusion that the db is doing delta updates and it is really a good one at that.

Nevertheless, I managed to reduce startup time significantly if the user has not added/deleted files since last restart or did a manual db-sync.

Please give feedback how this could be improved even more, if. My startup time is now 2seconds rather than 3 (Thats 33% improvement :smirk_cat: ) without any loss.

If you use it and notice any issues, please rectify the error and post solutions.


Also forgot to mention, do not delete files manually outside Emacs through your file manager - if you do it, manually do a db-sync! or wait for the next time you add/del some file - upon restart the db will sync again!


Load the code, before (org-roam-db-autosync-mode)!
use (setq org-roam-db-last-update-file "/path/to/file") to set the location of the file which keeps track when the last db update was done. Defaults to ~/.emacs/org-roam-db-last-update-time this is a plain txt file which just keeps the time.

;;; org-roam-db-sync-optimization.el

;;; Commentary:
;; This Emacs Lisp file optimizes the Org-roam database synchronization process by introducing
;; a mechanism to skip unnecessary synchronization when the database file has not been modified
;; since the last update. The timestamp of the last Org-roam database update is stored in an
;; external file.

;;; Code:

(defcustom org-roam-db-last-update-file "~/.emacs.d/org-roam-db-last-update-time"
  "File to store the timestamp of the last Org-roam database update."
  :type 'file
  :group 'org-roam)

(defvar org-roam-db-last-update-time nil
  "Timestamp of the last Org-roam database update.")

(defun org-roam-db-load-last-update-time ()
  "Load the timestamp of the last Org-roam database update from file.
If the file is not readable or does not exist, the timestamp remains nil."
  (when (file-readable-p org-roam-db-last-update-file)
    (setq org-roam-db-last-update-time
            (insert-file-contents org-roam-db-last-update-file)
            (read (current-buffer))))))

(defun org-roam-db-save-last-update-time ()
  "Save the timestamp of the last Org-roam database update to file."
    (prin1 org-roam-db-last-update-time (current-buffer))
    (write-region (point-min) (point-max) org-roam-db-last-update-file)))

(defun org-roam-db-update-time ()
  "Update the timestamp of the last Org-roam database update.
This function sets the timestamp to the current time and saves it to the external file."
  (setq org-roam-db-last-update-time (current-time))

(defun org-roam-db-sync-advice (orig-fun &rest args)
  "Advice function for org-roam-db-sync to check if syncing is necessary.
This advice checks whether the Org-roam database file has been modified since the last update.
If the file has been modified or the last update time is nil, it calls the original function (`org-roam-db-sync`),
updates the timestamp, and saves it to the external file."
  (let ((db-file-modified-time (nth 5 (file-attributes org-roam-db-location))))
    (when (or (null org-roam-db-last-update-time)
              (time-less-p org-roam-db-last-update-time db-file-modified-time))
      ;; Call the original function to perform synchronization
      (apply orig-fun args)
      ;; Update and save the timestamp

;;; Initialization:

;; Load the last update time when Emacs starts

;; Advising org-roam-db-sync
(advice-add 'org-roam-db-sync :around #'org-roam-db-sync-advice)

;; Save the last update time when Emacs is about to exit
(add-hook 'kill-emacs-hook 'org-roam-db-save-last-update-time)

(provide 'org-roam-db-sync-optimization)

;;; org-roam-db-sync-optimization.el ends here

Update 01

I have been running this for sometime without any problem. I created a function to force db-update on demand to circumvent the advice

(defun custom/org-roam-db-sync (&optional force)
  "Temporarily remove the sync advice and do org-roam-db-sync.
If FORCE is non-nil, force a rebuild of the cache from scratch."
  (interactive "P")
  (advice-remove 'org-roam-db-sync #'org-roam-db-sync-advice)
  (org-roam-db-sync force)
  (advice-add 'org-roam-db-sync :around #'org-roam-db-sync-advice))

Further - timestamps can be used to create sync between roam instances since they give information about recency – as described here: roam-sync