On 04/05/2021 22:08, Ihor Radchenko wrote:
While testing another patch for agenda fontification, I noticed that
agenda can spend up to half!! time doing org-up-heading-safe. Mostly
inside queries for inherited tags and properties.
I managed to make org-up-heading-safe up to 50x faster using position
cache.
I have not tested the patch due to I do not use agenda. My interest was
stimulated solely by my attempts to make org-refile-get-targets faster.
I consider it as an improvement. I have noticed the only thing that must
be fixed: comments with old variant of the function. Reaction to my
other comments is optional.
In org-agenda.el org-up-heading-safe function is called only from
org-find-top-headline. So the purpose of the dance with backward
searches is to get top level headings. My expectation is that scan
through the whole buffer and storing result in a structure that allows
binary search of position (array, red-black tree, etc) may be even faster.
Alternatively lazily obtained position of top heading could be stored in
cache to reduce number of iterations on org-find-top-line.
+ (let ((level-cache (gethash (point) org--up-heading-cache)))
+ (if (and level-cache
+ (eq (buffer-chars-modified-tick) org--up-heading-cache-tick))
If buffer-chars-modified-tick is faster than gethash then reordering
might result in very slight improvement of performance.
+ ;; Parent is inside accessible part of the buffer.
+ (progn (goto-char level-cache)
+ (funcall outline-level)))
I do not see any reason why outline level can not be cached in pair with
position.
+ (let (result)
+ (setq result
I am not a lisp guru, so from my point of view this can be reduced to
(let ((result ...
+ (format "^\\*\\{1,%d\\} " level-up) nil t)
\t as an alternative to " " is used in org-refile.el,
e.g. "^\\*+[ \t]+". Unsure which variant is canonical one and I see that
regexp is taken from original function, so no regression is expected.
+;; (defun org-up-heading-safe ()
+;; "Move to the heading line of which the present line is a subheading.
Clean-up here is required, other notes may be ignored.