diff -u /tmp/nm-btree-1.org /tmp/nm-btree.org --- /tmp/nm-btree-1.org 2021-05-10 23:04:21.286395360 +0800 +++ /tmp/nm-btree.org 2021-05-10 23:02:17.586396326 +0800 @@ -1,3 +1,6 @@ +:PROPERTIES: +:ID: 0aba93db-491d-46f7-b952-e138ba179a12 +:END: #+begin_src elisp :results none @@ -38,6 +41,26 @@ (push props parents)))) (and headings (cons headings count))))))) +(defun nm-buffer-headings-reversed-cache (buffer) + (with-current-buffer buffer + (save-restriction + (save-excursion + (widen) + (goto-char (point-min)) + (let ((count 0) + (headings ()) + (parents ())) + (while (re-search-forward org-outline-regexp-bol nil t) + (let ((cached (org-element--cache-find (line-beginning-position)))) + (if (eq (org-element-property :begin cached) (line-beginning-position)) + (push cached headings) + (push (org-element-at-point) headings))) + + ;; (save-excursion + ;; (beginning-of-line) + ;; (org-element-at-point 'cached)) + )))))) + ;; binary search tree (defun nm-btree-new-node () @@ -103,6 +126,7 @@ #+begin_src elisp (byte-compile #'nm-buffer-headings-reversed) +(byte-compile #'nm-buffer-headings-reversed-cache) (byte-compile #'nm-btree-from-reversed) (byte-compile #'nm-btree-find-left) @@ -125,13 +149,44 @@ (let* ((scan-result1 (nm-buffer-headings-reversed buffer)) (tree1 (nm-btree-from-reversed scan-result1))) tree1))) + (append '("scan using org-element-cache x10") + (progn + (let ((org-element-cache-sync-duration 1000.0)) + (org-element-cache-reset) + (org-element--cache-buffer-stealthly)) + (benchmark-run 10 + (nm-buffer-headings-reversed-cache buffer)))) + (append '("populating the cache x1") + (benchmark-run 1 + (with-current-buffer buffer + ;; Disable async processing. + (let ((org-element-cache-sync-duration 1000.0)) + (org-element-cache-reset) + (org-element--cache-buffer-stealthly))))) (append '("find random x10 000") (benchmark-run 10000 (nm-btree-find-left tree (random lim)))) + (append '("find random in org-element-cache x10 000") + (benchmark-run 10000 + (org-element-lineage + (org-element--cache-find (random lim)) + '(headlines) t))) (list "nodes" (cdr scan-result) "" ""))) #+end_src #+RESULTS: +| scan x10 | 1.693757321 | 0 | 0.0 | +| btree x10 | 0.552682783 | 1 | 0.43690057000000593 | +| scan+btree x10 | 2.3174109880000002 | 0 | 0.0 | +| scan using org-element-cache x10 | 2.9140550789999997 | 0 | 0.0 | +| populating the cache x1 | 10.83575134 | 9 | 3.9872376689999953 | +| find random x10 000 | 0.061988949999999994 | 0 | 0.0 | +| find random in org-element-cache x10 000 | 0.003262685 | 0 | 0.0 | +| nodes | 16641 | | | + + + + | scan x10 | 0.8611382689999999 | 0 | 0.0 | | btree x10 | 0.07705962400000001 | 1 | 0.05648322199999978 | | scan+btree x10 | 0.940467238 | 1 | 0.05685373699999996 | Diff finished. Mon May 10 23:04:27 2021