Skip to content

Commit b061e7b

Browse files
committed
org-cite-list-citations: Cache footnote-definition searches
* lisp/oc.el (org-cite-list-citations): Avoid quadratic complexity. Pre-calculate list of all footnote definitions and cache the footnote label search hits. Do not make `org-element-map' accumulate unused result.
1 parent 37a447a commit b061e7b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

lisp/oc.el

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ INFO is the export communication channel, as a property list."
854854
(or (plist-get info :citations)
855855
(letrec ((cites nil)
856856
(tree (plist-get info :parse-tree))
857+
(definition-cache (make-hash-table :test #'equal))
858+
(definition-list nil)
857859
(find-definition
858860
;; Find definition for standard reference LABEL. At
859861
;; this point, it is impossible to rely on
@@ -862,11 +864,21 @@ INFO is the export communication channel, as a property list."
862864
;; un-processed citation objects. So we use
863865
;; a simplified version of the function above.
864866
(lambda (label)
865-
(org-element-map tree 'footnote-definition
866-
(lambda (d)
867-
(and (equal label (org-element-property :label d))
868-
(or (org-element-contents d) "")))
869-
info t)))
867+
(or (gethash label definition-cache)
868+
(org-element-map
869+
(or definition-list
870+
(setq definition-list
871+
(org-element-map
872+
tree
873+
'footnote-definition
874+
#'identity info)))
875+
'footnote-definition
876+
(lambda (d)
877+
(and (equal label (org-element-property :label d))
878+
(puthash label
879+
(or (org-element-contents d) "")
880+
definition-cache)))
881+
info t))))
870882
(search-cites
871883
(lambda (data)
872884
(org-element-map data '(citation footnote-reference)
@@ -880,7 +892,8 @@ INFO is the export communication channel, as a property list."
880892
(_
881893
(let ((label (org-element-property :label datum)))
882894
(funcall search-cites
883-
(funcall find-definition label))))))
895+
(funcall find-definition label)))))
896+
nil)
884897
info nil 'footnote-definition t))))
885898
(funcall search-cites tree)
886899
(let ((result (nreverse cites)))

0 commit comments

Comments
 (0)