From 1931fc213139b9f679390e5c90cbb65bf1451441 Mon Sep 17 00:00:00 2001 From: TEC Date: Tue, 3 Jan 2023 19:28:30 +0800 Subject: [PATCH] org-persist: Allow inhibiting of normalisation * lisp/org-persist.el (org-persist-register, org-persist--normalize-container, org-persist--inhibit-container-normalization): Since container normalisation is applied frequently (via `org-persist--normalize-container'), when registering many new containers the cost can quickly add up. To avoid redundant overhead, after normalising the container initially in `org-persist-register' we bind a new variable `org-persist--inhibit-container-normalization' to t and adjust `org-persist--normalize-container' to do nothing when this new variable is non-nil. --- lisp/org-persist.el | 70 ++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/lisp/org-persist.el b/lisp/org-persist.el index 0df345536..ae8483158 100644 --- a/lisp/org-persist.el +++ b/lisp/org-persist.el @@ -602,22 +602,27 @@ MISC, if non-nil will be appended to the collection. It must be a plist." ;;;; Reading container data. +(defvar org-persist--inhibit-container-normalization nil + "Prevent `org-persist--normalize-container' from doing anything.") + (defun org-persist--normalize-container (container &optional inner) "Normalize CONTAINER representation into (type . settings). When INNER is non-nil, do not try to match as list of containers." - (pcase container - ((or `elisp `elisp-data `version `file `index `url) - `(,container nil)) - ((or (pred keywordp) (pred stringp) `(quote . ,_)) - `(elisp-data ,container)) - ((pred symbolp) - `(elisp ,container)) - (`(,(or `elisp `elisp-data `version `file `index `url) . ,_) - container) - ((and (pred listp) (guard (not inner))) - (mapcar (lambda (c) (org-persist--normalize-container c 'inner)) container)) - (_ (error "org-persist: Unknown container type: %S" container)))) + (if org-persist--inhibit-container-normalization + container + (pcase container + ((or `elisp `elisp-data `version `file `index `url) + `(,container nil)) + ((or (pred keywordp) (pred stringp) `(quote . ,_)) + `(elisp-data ,container)) + ((pred symbolp) + `(elisp ,container)) + (`(,(or `elisp `elisp-data `version `file `index `url) . ,_) + container) + ((and (pred listp) (guard (not inner))) + (mapcar (lambda (c) (org-persist--normalize-container c 'inner)) container)) + (_ (error "org-persist: Unknown container type: %S" container))))) (defvar org-persist--associated-buffer-cache (make-hash-table :weakness 'key) "Buffer hash cache.") @@ -950,26 +955,27 @@ VALUE pairs. 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)) - (setq container (org-persist--normalize-container container)) - (when inherit - (setq inherit (org-persist--normalize-container inherit)) - (let ((inherited-collection (org-persist--get-collection inherit associated)) - new-collection) - (unless (member container (plist-get inherited-collection :container)) - (setq new-collection - (plist-put (copy-sequence inherited-collection) :container - (cons container (plist-get inherited-collection :container)))) - (org-persist--remove-from-index inherited-collection) - (org-persist--add-to-index new-collection)))) - (let ((collection (org-persist--get-collection container associated misc))) - (when (and expiry (not inherit)) - (when expiry (plist-put collection :expiry expiry)))) - (when (or (bufferp associated) (bufferp (plist-get associated :buffer))) - (with-current-buffer (if (bufferp associated) - associated - (plist-get associated :buffer)) - (add-hook 'kill-buffer-hook #'org-persist-write-all-buffer nil 'local))) - (when write-immediately (org-persist-write container associated))) + (let ((container (org-persist--normalize-container container)) + (inherit (and inherit (org-persist--normalize-container inherit))) + (org-persist--inhibit-container-normalization t)) + (when inherit + (let ((inherited-collection (org-persist--get-collection inherit associated)) + new-collection) + (unless (member container (plist-get inherited-collection :container)) + (setq new-collection + (plist-put (copy-sequence inherited-collection) :container + (cons container (plist-get inherited-collection :container)))) + (org-persist--remove-from-index inherited-collection) + (org-persist--add-to-index new-collection)))) + (let ((collection (org-persist--get-collection container associated misc))) + (when (and expiry (not inherit)) + (when expiry (plist-put collection :expiry expiry)))) + (when (or (bufferp associated) (bufferp (plist-get associated :buffer))) + (with-current-buffer (if (bufferp associated) + associated + (plist-get associated :buffer)) + (add-hook 'kill-buffer-hook #'org-persist-write-all-buffer nil 'local))) + (when write-immediately (org-persist-write container associated)))) (cl-defun org-persist-unregister (container &optional associated &key remove-related) "Unregister CONTAINER in ASSOCIATED to be persistent.