emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-refile: calculate file part of the outline path outside the loop for the headings
@ 2024-10-14 11:50 Sacha Chua
  2024-10-14 19:09 ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Sacha Chua @ 2024-10-14 11:50 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 647 bytes --]

Hello, all!

I noticed that org-refile-get-targets seems to recalculate the first
part of the outline path for each heading, which makes it slow on large
files when org-refile-use-outline-path is set to 'title. The following
patch let-binds that part to a variable that can be put outside the loop
for the headings, making it faster in the case where refile targets
include a few files with lots of headings. Does it make sense? I was
wondering if I was missing some important reason for it to be inside
that loop.

It feels like a pretty small change (just adding a let), but I 
have copyright assignment papers on file at the FSF just in case.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-refile-calculate-file-part-of-the-outline-path-o.patch --]
[-- Type: text/x-diff, Size: 4700 bytes --]

From 85a68b44cb3ddc5eda2dfe36614fdd6fb0755f6f Mon Sep 17 00:00:00 2001
From: Sacha Chua <sacha@sachachua.com>
Date: Sun, 13 Oct 2024 21:30:29 -0400
Subject: [PATCH] org-refile: calculate file part of the outline path once per
 file

* lisp/org-refile.el (org-refile-get-targets): Calculate the file part
of the outline path once per file, improving the performance when
org-refile-use-outline-path is set to 'title.
---
 lisp/org-refile.el | 89 +++++++++++++++++++++++-----------------------
 1 file changed, 44 insertions(+), 45 deletions(-)

diff --git a/lisp/org-refile.el b/lisp/org-refile.el
index 6da5f6482..7189f91a0 100644
--- a/lisp/org-refile.el
+++ b/lisp/org-refile.el
@@ -338,53 +338,52 @@ defun org-refile-get-targets
                                  (and f (file-name-nondirectory f)))
                              f nil nil)
                        tgs))
-	       (org-with-wide-buffer
-		(goto-char (point-min))
-		(setq org-outline-path-cache nil)
-		(while (re-search-forward descre nil t)
-		  (forward-line 0)
-		  (let ((case-fold-search nil))
-		    (looking-at org-complex-heading-regexp))
-		  (let ((begin (point))
-			(heading (match-string-no-properties 4)))
-		    (unless (or (and
-				 org-refile-target-verify-function
-				 (not
-				  (funcall org-refile-target-verify-function)))
-				(not heading))
-		      (let ((re (format org-complex-heading-regexp-format
-					(regexp-quote heading)))
-			    (target
-			     (if (not org-refile-use-outline-path) heading
-			       (mapconcat
-				#'identity
-				(append
-				 (pcase org-refile-use-outline-path
-				   (`file (list
+               (org-with-wide-buffer
+		(let ((base (pcase org-refile-use-outline-path
+			      (`file (list
+                                      (and (buffer-file-name (buffer-base-buffer))
+                                           (file-name-nondirectory
+                                            (buffer-file-name (buffer-base-buffer))))))
+                              (`title (list
+                                       (or (org-get-title)
                                            (and (buffer-file-name (buffer-base-buffer))
                                                 (file-name-nondirectory
-                                                 (buffer-file-name (buffer-base-buffer))))))
-                                   (`title (list
-                                            (or (org-get-title)
-                                                (and (buffer-file-name (buffer-base-buffer))
-                                                     (file-name-nondirectory
-                                                      (buffer-file-name (buffer-base-buffer)))))))
-                                   (`full-file-path
-				    (list (buffer-file-name
-					   (buffer-base-buffer))))
-				   (`buffer-name
-				    (list (buffer-name
-					   (buffer-base-buffer))))
-				   (_ nil))
-				 (mapcar (lambda (s) (replace-regexp-in-string
-						 "/" "\\/" s nil t))
-					 (org-get-outline-path t t)))
-				"/"))))
-			(push (list target f re (org-refile-marker (point)))
-			      tgs)))
-		    (when (= (point) begin)
-		      ;; Verification function has not moved point.
-		      (end-of-line)))))))
+                                                 (buffer-file-name (buffer-base-buffer)))))))
+                              (`full-file-path
+			       (list (buffer-file-name
+				      (buffer-base-buffer))))
+			      (`buffer-name
+			       (list (buffer-name
+				      (buffer-base-buffer))))
+			      (_ nil))))
+		  (while (re-search-forward descre nil t)
+		    (forward-line 0)
+		    (let ((case-fold-search nil))
+		      (looking-at org-complex-heading-regexp))
+		    (let ((begin (point))
+			  (heading (match-string-no-properties 4)))
+		      (unless (or (and
+				   org-refile-target-verify-function
+				   (not
+				    (funcall org-refile-target-verify-function)))
+				  (not heading))
+		        (let ((re (format org-complex-heading-regexp-format
+					  (regexp-quote heading)))
+			      (target
+			       (if (not org-refile-use-outline-path) heading
+			         (mapconcat
+				  #'identity
+				  (append
+				   base
+				   (mapcar (lambda (s) (replace-regexp-in-string
+						        "/" "\\/" s nil t))
+					   (org-get-outline-path t t)))
+				  "/"))))
+			  (push (list target f re (org-refile-marker (point)))
+			        tgs)))
+		      (when (= (point) begin)
+		        ;; Verification function has not moved point.
+		        (end-of-line))))))))
 	    (when org-refile-use-cache
 	      (org-refile-cache-put tgs (buffer-file-name) descre))
 	    (setq targets (append tgs targets))))))
-- 
2.43.0


[-- Attachment #3: Type: text/plain, Size: 22 bytes --]


Best regards,

Sacha

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] org-refile: calculate file part of the outline path outside the loop for the headings
  2024-10-14 11:50 [PATCH] org-refile: calculate file part of the outline path outside the loop for the headings Sacha Chua
@ 2024-10-14 19:09 ` Ihor Radchenko
  2024-10-14 20:47   ` Sacha Chua
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2024-10-14 19:09 UTC (permalink / raw)
  To: Sacha Chua; +Cc: emacs-orgmode

Sacha Chua <sacha@sachachua.com> writes:

> I noticed that org-refile-get-targets seems to recalculate the first
> part of the outline path for each heading, which makes it slow on large
> files when org-refile-use-outline-path is set to 'title. The following
> patch let-binds that part to a variable that can be put outside the loop
> for the headings, making it faster in the case where refile targets
> include a few files with lots of headings. Does it make sense? I was
> wondering if I was missing some important reason for it to be inside
> that loop.

Makes sense.

> It feels like a pretty small change (just adding a let), but I 
> have copyright assignment papers on file at the FSF just in case.

Agree. Yet, the tests are failing :)

1 unexpected results:
   FAILED  test-org/refile-get-targets  ((should (equal '("H1\\/foo") (org-test-with-temp-text "* H1/foo" (let (... ...) (mapcar ... ...))))) :form (equal ("H1\\/foo") ("H1")) :value nil :explanation (list-elt 0 (arrays-of-different-length 7 2 "H1\\/foo" "H1" first-mismatch-at 2)))

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] org-refile: calculate file part of the outline path outside the loop for the headings
  2024-10-14 19:09 ` Ihor Radchenko
@ 2024-10-14 20:47   ` Sacha Chua
  2024-10-15 18:52     ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Sacha Chua @ 2024-10-14 20:47 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 960 bytes --]

Ihor Radchenko <yantar92@posteo.net> writes:

Hello, Ihor!

>> I noticed that org-refile-get-targets seems to recalculate the first
>> part of the outline path for each heading, which makes it slow on large
>> It feels like a pretty small change (just adding a let), but I 
>> have copyright assignment papers on file at the FSF just in case.
> Agree. Yet, the tests are failing :)
> 1 unexpected results:
>    FAILED  test-org/refile-get-targets  ((should (equal '("H1\\/foo") (org-test-with-temp-text "* H1/foo" (let (... ...) (mapcar ... ...))))) :form (equal ("H1\\/foo") ("H1")) :value nil :explanation (list-elt 0 (arrays-of-different-length 7 2 "H1\\/foo" "H1" first-mismatch-at 2)))

Whoops, sorry, I forgot to run the tests. =) Now that I have done so, I
realized that I accidentally deleted the
  (goto-char (point-min))
  (setq org-outline-path-cache nil)
before the loop. This patch should make that test run again. Once more
with feeling!

Sacha


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-refile-calculate-file-part-of-the-outline-path-o.patch --]
[-- Type: text/x-diff, Size: 4521 bytes --]

From ef843a24848203adc58aa3cfbafcb50639126921 Mon Sep 17 00:00:00 2001
From: Sacha Chua <sacha@sachachua.com>
Date: Sun, 13 Oct 2024 21:30:29 -0400
Subject: [PATCH] org-refile: calculate file part of the outline path once per
 file

* lisp/org-refile.el (org-refile-get-targets): Calculate the file part
of the outline path once per file, improving the performance when
org-refile-use-outline-path is set to 'title.
---
 lisp/org-refile.el | 85 +++++++++++++++++++++++-----------------------
 1 file changed, 43 insertions(+), 42 deletions(-)

diff --git a/lisp/org-refile.el b/lisp/org-refile.el
index 6da5f6482..474706fe3 100644
--- a/lisp/org-refile.el
+++ b/lisp/org-refile.el
@@ -341,50 +341,51 @@ defun org-refile-get-targets
 	       (org-with-wide-buffer
 		(goto-char (point-min))
 		(setq org-outline-path-cache nil)
-		(while (re-search-forward descre nil t)
-		  (forward-line 0)
-		  (let ((case-fold-search nil))
-		    (looking-at org-complex-heading-regexp))
-		  (let ((begin (point))
-			(heading (match-string-no-properties 4)))
-		    (unless (or (and
-				 org-refile-target-verify-function
-				 (not
-				  (funcall org-refile-target-verify-function)))
-				(not heading))
-		      (let ((re (format org-complex-heading-regexp-format
-					(regexp-quote heading)))
-			    (target
-			     (if (not org-refile-use-outline-path) heading
-			       (mapconcat
-				#'identity
-				(append
-				 (pcase org-refile-use-outline-path
-				   (`file (list
+		(let ((base (pcase org-refile-use-outline-path
+			      (`file (list
+                                      (and (buffer-file-name (buffer-base-buffer))
+                                           (file-name-nondirectory
+                                            (buffer-file-name (buffer-base-buffer))))))
+                              (`title (list
+                                       (or (org-get-title)
                                            (and (buffer-file-name (buffer-base-buffer))
                                                 (file-name-nondirectory
-                                                 (buffer-file-name (buffer-base-buffer))))))
-                                   (`title (list
-                                            (or (org-get-title)
-                                                (and (buffer-file-name (buffer-base-buffer))
-                                                     (file-name-nondirectory
-                                                      (buffer-file-name (buffer-base-buffer)))))))
-                                   (`full-file-path
-				    (list (buffer-file-name
-					   (buffer-base-buffer))))
-				   (`buffer-name
-				    (list (buffer-name
-					   (buffer-base-buffer))))
-				   (_ nil))
-				 (mapcar (lambda (s) (replace-regexp-in-string
-						 "/" "\\/" s nil t))
-					 (org-get-outline-path t t)))
-				"/"))))
-			(push (list target f re (org-refile-marker (point)))
-			      tgs)))
-		    (when (= (point) begin)
-		      ;; Verification function has not moved point.
-		      (end-of-line)))))))
+                                                 (buffer-file-name (buffer-base-buffer)))))))
+                              (`full-file-path
+			       (list (buffer-file-name
+				      (buffer-base-buffer))))
+			      (`buffer-name
+			       (list (buffer-name
+				      (buffer-base-buffer))))
+			      (_ nil))))
+		  (while (re-search-forward descre nil t)
+		    (forward-line 0)
+		    (let ((case-fold-search nil))
+		      (looking-at org-complex-heading-regexp))
+		    (let ((begin (point))
+			  (heading (match-string-no-properties 4)))
+		      (unless (or (and
+				   org-refile-target-verify-function
+				   (not
+				    (funcall org-refile-target-verify-function)))
+				  (not heading))
+		        (let ((re (format org-complex-heading-regexp-format
+					  (regexp-quote heading)))
+			      (target
+			       (if (not org-refile-use-outline-path) heading
+			         (mapconcat
+				  #'identity
+				  (append
+				   base
+				   (mapcar (lambda (s) (replace-regexp-in-string
+						        "/" "\\/" s nil t))
+					   (org-get-outline-path t t)))
+				  "/"))))
+			  (push (list target f re (org-refile-marker (point)))
+			        tgs)))
+		      (when (= (point) begin)
+		        ;; Verification function has not moved point.
+		        (end-of-line))))))))
 	    (when org-refile-use-cache
 	      (org-refile-cache-put tgs (buffer-file-name) descre))
 	    (setq targets (append tgs targets))))))
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] org-refile: calculate file part of the outline path outside the loop for the headings
  2024-10-14 20:47   ` Sacha Chua
@ 2024-10-15 18:52     ` Ihor Radchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Ihor Radchenko @ 2024-10-15 18:52 UTC (permalink / raw)
  To: Sacha Chua; +Cc: emacs-orgmode

Sacha Chua <sacha@sachachua.com> writes:

> Whoops, sorry, I forgot to run the tests. =) Now that I have done so, I
> realized that I accidentally deleted the
>   (goto-char (point-min))
>   (setq org-outline-path-cache nil)
> before the loop. This patch should make that test run again. Once more
> with feeling!

Thanks!
Applied, onto main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=566c341155

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-15 19:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-14 11:50 [PATCH] org-refile: calculate file part of the outline path outside the loop for the headings Sacha Chua
2024-10-14 19:09 ` Ihor Radchenko
2024-10-14 20:47   ` Sacha Chua
2024-10-15 18:52     ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).