Karthik Chikmagalur <karthikchikmaga...@gmail.com> writes:

>> What if you try the attached diff?
>
> Two cases, profiles for both are attached:
>
> 1. When running `save-buffers-kill-emacs', the time is down from several
> minutes to about 10 seconds -- still undesirably long but not egregious.
> This is the profile org-persist-delay-kill-emacs.eld.

Ok. What about write time distribution now?

> 2. When opening an Org file and running (org-latex-preview '(16)): it
> still takes about 30 seconds to render ~350 LaTeX fragments, during
> which Emacs is unresponsive.  Previously this number was about 1-1.5
> seconds, during which Emacs was mostly responsive.  This continues to be
> because of `org-persist-write-elisp-file', which is called via
> `org-persist-register' when we cache LaTeX previews on generation.  This
> is the profile org-persist-delay-latex-preview.eld.

Expected. The problem appears with updating the index file every single
time a new latex preview is created. In your case, index is not small
and time required to write it is significantly larger compared to the
time needed to generate the preview images.

The reason I added force-updates to the index was fixing an elusive bug
with index associating cache with certain buffer string hash but actual
cached data representing a different buffer string.

    ;; Every time we write cache data, make sure that index is up to
    ;; date. This prevents situation when two Emacs sessions are writing
    ;; different data under the same cache key, but do not update the
    ;; index metadata about the cache data written (e.g. hash).
    (when (or inhibit-writing-index (org-persist--save-index))

Rather then synchronizing index on every write, I can try an alternative
approach and merge in-memory index with disk index on every read.

May you try the attached untested diff? (instead of previous)

diff --git a/lisp/org-persist.el b/lisp/org-persist.el
index a639699d93..ab290bbc26 100644
--- a/lisp/org-persist.el
+++ b/lisp/org-persist.el
@@ -492,7 +492,8 @@ (defun org-persist--write-elisp-file
     ;; date. This prevents situation when two Emacs sessions are writing
     ;; different data under the same cache key, but do not update the
     ;; index metadata about the cache data written (e.g. hash).
-    (when (or inhibit-writing-index (org-persist--save-index))
+    (when t
+      ;; (or inhibit-writing-index (org-persist--save-index))
       (unless (file-exists-p (file-name-directory file))
         (make-directory (file-name-directory file) t))
       ;; Do not write to FILE directly.  Another Emacs instance may be
@@ -1007,6 +1008,7 @@ (cl-defun org-persist-register (container &optional associated &rest misc
 When WRITE-IMMEDIATELY is non-nil, the return value will be the same
 with `org-persist-write'."
   (unless org-persist--index (org-persist--load-index))
+  (org-persist--merge-index-with-disk)
   (setq container (org-persist--normalize-container container))
   (when inherit
     (setq inherit (org-persist--normalize-container inherit))
@@ -1034,6 +1036,7 @@ (cl-defun org-persist-unregister (container &optional associated &key remove-rel
 When REMOVE-RELATED is non-nil, remove all the containers stored with
 the CONTAINER as well."
   (unless org-persist--index (org-persist--load-index))
+  (org-persist--merge-index-with-disk)
   (setq container (org-persist--normalize-container container))
   (if (eq associated 'all)
       (mapc (lambda (collection)
@@ -1071,6 +1074,7 @@ (cl-defun org-persist-read (container &optional associated hash-must-match load
     (org-persist-read \"My data\" nil nil nil
                       :read-related t) ; => (\"My data\" \"test\")"
   (unless org-persist--index (org-persist--load-index))
+  (org-persist--merge-index-with-disk)
   (setq associated (org-persist--normalize-associated associated))
   (setq container (org-persist--normalize-container container))
   (let* ((collection (org-persist--find-index `(:container ,container :associated ,associated)))
@@ -1119,6 +1123,7 @@ (cl-defun org-persist-load (container &optional associated hash-must-match &key
 (defun org-persist-load-all (&optional associated)
   "Restore all the persistent data associated with ASSOCIATED."
   (unless org-persist--index (org-persist--load-index))
+  (org-persist--merge-index-with-disk)
   (setq associated (org-persist--normalize-associated associated))
   (let (all-containers)
     (dolist (collection org-persist--index)
@@ -1164,16 +1169,18 @@ (defun org-persist-write (container &optional associated ignore-return)
              (seq-find (lambda (v)
                          (run-hook-with-args-until-success 'org-persist-before-write-hook v associated))
                        (plist-get collection :container)))
-      (let ((file (org-file-name-concat org-persist-directory (plist-get collection :persist-file)))
-            (data (mapcar (lambda (c) (cons c (org-persist-write:generic c collection)))
-                          (plist-get collection :container))))
-        (org-persist--write-elisp-file file data)
-        (or ignore-return (org-persist-read container associated))))))
+      (when (or (file-exists-p org-persist-directory) (org-persist--save-index))
+        (let ((file (org-file-name-concat org-persist-directory (plist-get collection :persist-file)))
+              (data (mapcar (lambda (c) (cons c (org-persist-write:generic c collection)))
+                            (plist-get collection :container))))
+          (org-persist--write-elisp-file file data)
+          (or ignore-return (org-persist-read container associated)))))))
 
 (defun org-persist-write-all (&optional associated)
   "Save all the persistent data.
 When ASSOCIATED is non-nil, only save the matching data."
   (unless org-persist--index (org-persist--load-index))
+  (org-persist--merge-index-with-disk)
   (setq associated (org-persist--normalize-associated associated))
   (if
       (and (equal 1 (length org-persist--index))
-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to