patch updated and rebased on main > 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?
Adding back standard properties to the loop reduces performance by 5-20% depending on the element (see attached), but that's taking off of an already 5-10x speedup (see attached pdf). However, if we added affiliated keywords to standard properties we could easily access them without touching anything else since it would be an array lookup. I'm not sure why we would need to loop over the entire array. >From 8df3f542ba3bffbb48f87964ce8f7529d140fb71 Mon Sep 17 00:00:00 2001 From: ndwarshuis <nd...@yavin4.ch> Subject: [PATCH] org-element.el: Make affiliated keyword interpreter faster * lisp/org-element.el (org-element--interpret-affiliated-keywords): Optimize performance by bypassing unnecessary types and reducing loop complexity. Added new constant `org-element-elements-no-affiliated` which stores the types to be bypassed. This function was doing redundant work on several levels which dramatically reduced performance of interpreting element nodes relative to object nodes. First, all types were interpreted regardless of if they could possibly contain affiliated keywords. Skipping these types dramatically speeds up typical use cases since many of these skipped types are common (headline, item, etc). Second, the loop was much more complex than needed. The loop included :standard-properties which should not be necessary here. It also duplicated some work between calls to `org-element--properties-mapc` and `mapconcat` (the code was moved entirely under the former). The result should be faster and more readable. TINYCHANGE --- lisp/org-element.el | 85 ++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 5a7435581..8469a2d56 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -335,6 +335,12 @@ specially in `org-element--object-lex'.") (append org-element-recursive-objects '(paragraph table-row verse-block)) "List of object or element types that can directly contain objects.") +(defconst org-element-elements-no-affiliated + '(org-data comment clock headline inlinetask item + node-property planning property-drawer + 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" "RESULTS" "SOURCE" "SRCNAME" "TBLNAME") @@ -5522,49 +5528,48 @@ to interpret. Return Org syntax as a string." (make-string blank ?\n))))))))) (funcall fun data nil))) +(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))) + (concat "#+" (downcase key) + (and dual + (format "[%s]" (org-element-interpret-data dual))) + ": " + (if (member key org-element-parsed-keywords) + (org-element-interpret-data value) + value) + "\n"))) + (defun org-element--interpret-affiliated-keywords (element) "Return ELEMENT's affiliated keywords as Org syntax. If there is no affiliated keyword, return the empty string." - (let ((keyword-to-org - (lambda (key value) - (let (dual) - (when (member key org-element-dual-keywords) - (setq dual (cdr value) value (car value))) - (concat "#+" (downcase key) - (and dual - (format "[%s]" (org-element-interpret-data dual))) - ": " - (if (member key org-element-parsed-keywords) - (org-element-interpret-data value) - value) - "\n"))))) - (mapconcat - (lambda (prop) - (let ((value (org-element-property prop element)) - (keyword (upcase (substring (symbol-name prop) 1)))) - (when value - (if (or (member keyword org-element-multiple-keywords) - ;; All attribute keywords can have multiple lines. - (string-match-p "^ATTR_" keyword)) - (mapconcat (lambda (line) (funcall keyword-to-org keyword line)) - value "") - (funcall keyword-to-org keyword value))))) - ;; List all ELEMENT's properties matching an attribute line or an - ;; affiliated keyword, but ignore translated keywords since they - ;; cannot belong to the property list. - (let (acc) - (org-element-properties-mapc - (lambda (prop _ __) - (let ((keyword (upcase (substring (symbol-name prop) 1)))) - (when (or (string-match-p "^ATTR_" keyword) - (and - (member keyword org-element-affiliated-keywords) - (not (assoc keyword - org-element-keyword-translation-alist)))) - (push prop acc)))) - element t) - (nreverse acc)) - ""))) + ;; 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) + "" + (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))) + (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 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))))) ;; Because interpretation of the parse tree must return the same ;; number of blank lines between elements and the same number of white -- 2.48.1 ________________________________ From: Ihor Radchenko <yanta...@posteo.net> Sent: Wed, 25 Dec 2024 12:20:33 +0000 (6 weeks, 1 day, 12 hours ago) To: Dwarshuis, Nathan J Subject: [PATCH] org-element.el; significant optimizations for org-element--interpret-affiliated-keywords "Dwarshuis, Nathan J" <nd...@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? >From 0301efb86b994e2c79a37c21f17c664c1193d4c0 Mon Sep 17 00:00:00 2001 Message-ID: <0301efb86b994e2c79a37c21f17c664c1193d4c0.1735129004.git.yanta...@posteo.net> From: Ihor Radchenko <yanta...@posteo.net> 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
20240202_org_element-std.pdf
Description: Adobe PDF document