Skip to content

Commit

Permalink
org-cite-list-citations: Cache footnote-definition searches
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Ihor Radchenko committed Jun 16, 2022
1 parent 37a447a commit b061e7b
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lisp/oc.el
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,8 @@ INFO is the export communication channel, as a property list."
(or (plist-get info :citations)
(letrec ((cites nil)
(tree (plist-get info :parse-tree))
(definition-cache (make-hash-table :test #'equal))
(definition-list nil)
(find-definition
;; Find definition for standard reference LABEL. At
;; this point, it is impossible to rely on
Expand All @@ -862,11 +864,21 @@ INFO is the export communication channel, as a property list."
;; un-processed citation objects. So we use
;; a simplified version of the function above.
(lambda (label)
(org-element-map tree 'footnote-definition
(lambda (d)
(and (equal label (org-element-property :label d))
(or (org-element-contents d) "")))
info t)))
(or (gethash label definition-cache)
(org-element-map
(or definition-list
(setq definition-list
(org-element-map
tree
'footnote-definition
#'identity info)))
'footnote-definition
(lambda (d)
(and (equal label (org-element-property :label d))
(puthash label
(or (org-element-contents d) "")
definition-cache)))
info t))))
(search-cites
(lambda (data)
(org-element-map data '(citation footnote-reference)
Expand All @@ -880,7 +892,8 @@ INFO is the export communication channel, as a property list."
(_
(let ((label (org-element-property :label datum)))
(funcall search-cites
(funcall find-definition label))))))
(funcall find-definition label)))))
nil)
info nil 'footnote-definition t))))
(funcall search-cites tree)
(let ((result (nreverse cites)))
Expand Down

0 comments on commit b061e7b

Please sign in to comment.