emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: "Dwarshuis, Nathan J" <ndwar@yavin4.ch>
Cc: org-mode-email <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] org-element.el; significant optimizations for org-element--interpret-affiliated-keywords
Date: Wed, 25 Dec 2024 12:20:33 +0000	[thread overview]
Message-ID: <877c7o0x9q.fsf@localhost> (raw)
In-Reply-To: <87v7w5pswu.fsf@yavin4.ch>

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

"Dwarshuis, Nathan J" <ndwar@yavin4.ch> writes:

> I noticed that calling `org-element-interpret-data' on objects is
> generally 5-10x faster than when calling on elements. The reason seems
> to be that `org-element--interpret-affiliated-keywords' (which is only
> called on elements) does alot of unnecessary work. Namely, it runs on
> all elements (including those that should never have an affiliated
> keyword)
>
> The attached patch addresses this.

Thanks!
I am attaching some extra suggestions on top of the patch.

> ...  and also loops over :standard-properties which should not be
> relevant here.

There is nothing stopping us from adding some affiliated keywords to
standard properties in future. What happens if you drop this
optimization? Does the benchmark still show an improvement?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-suggestions.patch --]
[-- Type: text/x-patch, Size: 3776 bytes --]

From 0301efb86b994e2c79a37c21f17c664c1193d4c0 Mon Sep 17 00:00:00 2001
Message-ID: <0301efb86b994e2c79a37c21f17c664c1193d4c0.1735129004.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Wed, 25 Dec 2024 13:16:36 +0100
Subject: [PATCH] suggestions

---
 lisp/org-element.el | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/lisp/org-element.el b/lisp/org-element.el
index 3b90dce2a2..d386ee4184 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -338,7 +338,8 @@ (defconst org-element-object-containers
 (defconst org-element-elements-no-affiliated
   '(org-data comment clock headline inlinetask item
              node-property planning property-drawer
-             section table-row))
+             section table-row)
+  "List of paragraph-level node types that cannot have affiliated keywords.")
 
 (defconst org-element-affiliated-keywords
   '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
@@ -5522,7 +5523,8 @@ (defun org-element-interpret-data (data)
 			      (make-string blank ?\n)))))))))
     (funcall fun data nil)))
 
-(defun org-element--keyword-to-org (key value)
+(defun org-element--interpret-affiliated-keyword (key value)
+  "Interpret affiliated keyword with KEY and VALUE."
   (let (dual)
     (when (member key org-element-dual-keywords)
       (setq dual (cdr value) value (car value)))
@@ -5540,28 +5542,30 @@ (defun org-element--interpret-affiliated-keywords (element)
 If there is no affiliated keyword, return the empty string."
   ;; there are some elements that will never have affiliated keywords,
   ;; so do nothing for these
-  (if (member (org-element-type element) org-element-elements-no-affiliated)
+  (if (member (org-element-type element)
+              org-element-elements-no-affiliated)
       ""
     (let (acc)
       (org-element-properties-resolve element t)
       (org-element--properties-mapc
        (lambda (prop value)
          (when value
-           (let ((keyword (upcase (substring (symbol-name prop) 1))))
-             (when (or (string-match-p "^ATTR_" keyword)
+           (let* ((keyword (upcase (substring (symbol-name prop) 1)))
+                  (attrp (string-match-p "^ATTR_" keyword)))
+             (when (or attrp
                        (and
                         (member keyword org-element-affiliated-keywords)
                         (not (assoc keyword
-                                    org-element-keyword-translation-alist))))
-               (push (if (or (member keyword org-element-multiple-keywords)
-                             ;; All attribute keywords can have multiple lines.
-                             (string-match-p "^ATTR_" keyword))
-                         (mapconcat (lambda (line) (org-element--keyword-to-org keyword line))
-                                    value "")
-                       (org-element--keyword-to-org keyword value))
+                                  org-element-keyword-translation-alist))))
+               (push (if (or attrp ; All attribute keywords can have multiple lines.
+                             (member keyword org-element-multiple-keywords))
+                         (mapconcat
+                          (lambda (line) (org-element--interpret-affiliated-keyword keyword line))
+                          value "")
+                       (org-element--interpret-affiliated-keyword keyword value))
                      acc)))))
-      element nil t)
-    (apply #'concat (nreverse acc)))))
+       element nil t)
+      (apply #'concat (nreverse acc)))))
 
 ;; Because interpretation of the parse tree must return the same
 ;; number of blank lines between elements and the same number of white
-- 
2.47.1


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


-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
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>

      reply	other threads:[~2024-12-25 12:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-29 22:38 [PATCH] org-element.el; significant optimizations for org-element--interpret-affiliated-keywords Dwarshuis, Nathan J
2024-12-25 12:20 ` Ihor Radchenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877c7o0x9q.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=ndwar@yavin4.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).