Bug: performance issue in org-font-lock-add-priority-faces related to org-priority-regexp [9.4 (9.4-7-g3eccc5-elpaplus @ /home/standard/.emacs.d/elpa/26.3/develop/org-plus-contrib-20200921/)]

2020-10-07 Thread Tim Frana
Apologies, the extreme slowdown for the regex in my setup is mostly 
credited to a very long line in my org file (~100k characters). I 
inserted a snippet from a log file to a quote block, without realizing 
that a large base64 string was hiding at the end of one line.
The old regex could exclude that line early on, because it wasn't a 
header line, so I didn't notice a performance hit back then.


With that line deleted (org file shrinks to ~300KB), I measured 
performance with the new and old regex (new on 1st line vs old on 2nd line):


(elp-instrument-function 'org-font-lock-add-priority-faces)
(elp-reset-all)
;; open org file
(elp-results)


 Function Name Call Count  Elapsed Time Average 
Time

new: org-font-lock-add-priority-faces  28  0.0228787000 0.0008170964
old: org-font-lock-add-priority-faces  28  0.0007937999 2.834...e-05

So the new one is still considerably slower, but probably not noticeable 
unless the file is really large and/or it contains long lines.



One last remark, my workaround from the last mail is flawed, e.g. it 
broke priority sorting in my agendas. This one worked better:


(defun adv--org-font-lock-add-priority-faces (wrapped-func  args)
  (let ((org-priority-regexp "^\\*+ .*?\\(\\[#\\([A-Z0-9]+\\)\\] ?\\)"))
    (apply wrapped-func args)))
(advice-add 'org-font-lock-add-priority-faces :around 
#'adv--org-font-lock-add-priority-faces)





Bug: performance issue in org-font-lock-add-priority-faces related to org-priority-regexp [9.4 (9.4-7-g3eccc5-elpaplus @ /home/standard/.emacs.d/elpa/26.3/develop/org-plus-contrib-20200921/)]

2020-09-24 Thread Tim Frana

Hello,

After updating from org-plus-contrib-20200810 to 
org-plus-contrib-20200921, opening org files has become extremely slow 
for me. As a rough estimate, a 400 KB org file of mine used to take 5 
seconds on first load in a session, and after the update needed more 
than 2 minutes. This is the case both in Emacs 27.1 in Windows, and 
Emacs 26.3 in Windows WSL1 Ubuntu 20.04 LTS, using spacemacs develop.


I can narrow it down to this change:

org-font-lock-add-priority-faces used to search for this regexp: "^\\*+ 
.*?\\(\\[#\\(.\\)\\]\\)"
Now it searches for org-priority-regexp, which by default resolves to 
".*?\\(\\[#\\([A-Z0-9]+\\)\\] ?\\)".


This is much slower, and also matches more than I expected. In the 
following example, I expect only [#A], but it will match [#B] and [#C] 
too. Is this intended?


** TODO [#A] Headline [#B]
this is matched despite not being a heading: [#C] more text

It seems plausible that the slowdown comes from the regex engine now 
doing elaborate matching on all lines, instead of fast skipping over 
non-heading lines.


This version seems to resolve my problem, where the beginning "^\\*+ " 
should narrow matches to heading lines only:

(setq org-priority-regexp "^\\*+ .*?\\(\\[#\\([A-Z0-9]+\\)\\] ?\\)")

Profiler excerpt (the other 51% were mostly eaten by spell checking 
during idling, as I got distracted waiting for the loading to finish):


- set-auto-mode 5498  49%
 - set-auto-mode-0  5498  49%
  - org-mode    5498  49%
   - run-mode-hooks 5498  49%
    - apply 5497  49%
 - run-hooks    5497  49%
  - org-superstar-mode  5493  49%
   - org-superstar--fontify-buffer  5493  49%
    - font-lock-ensure  5493  49%
 - jit-lock-fontify-now 5493  49%
  - jit-lock--run-functions 5493  49%
   - run-hook-wrapped   5493  49%
    - # 5391 48%
 - font-lock-fontify-region 5391  48%
  - font-lock-default-fontify-region    5391  48%
   - font-lock-fontify-keywords-region  5391  48%
  org-font-lock-add-priority-faces  5391  48%

Best regards,
Tim






Emacs  : GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30)
 of 2019-09-16
Package: Org mode version 9.4 (9.4-7-g3eccc5-elpaplus @ 
/home/standard/.emacs.d/elpa/26.3/develop/org-plus-contrib-20200921/)


current state:
==
(setq
 org-src-mode-hook '(org-src-babel-configure-edit-buffer 
org-src-mode-configure-edit-buffer)

 org-after-todo-state-change-hook '(org-expiry-insert-created)
 org-link-shell-confirm-function 'yes-or-no-p
 org-blank-before-new-entry '((heading . t) (plain-list-item))
 org-babel-after-execute-hook '(spacemacs/ob-fix-inline-images)
 org-metadown-hook '(org-babel-pop-to-session-maybe)
 org-agenda-tags-todo-honor-ignore-options t
 org-export-with-sub-superscripts '{}
 org-clock-out-hook '(org-clock-remove-empty-clock-drawer)
 org-refile-targets '((nil :maxlevel . 3) (org-agenda-files :maxlevel . 3))
 org-html-format-inlinetask-function 
'org-html-format-inlinetask-default-function

 org-enforce-todo-dependencies t
 org-edit-src-content-indentation 0
 org-odt-format-headline-function 'org-odt-format-headline-default-function
 org-imenu-depth 8
 org-download-annotate-function 'org-download-annotate-default
 org-ascii-format-inlinetask-function 'org-ascii-format-inlinetask-default
 org-clock-report-include-clocking-task t
 org-modules '(org-habit ol-w3m ol-bbdb ol-bibtex ol-docview ol-gnus 
ol-info ol-irc ol-mhe ol-rmail ol-eww)
 org-blocker-hook '(org-block-todo-from-children-or-siblings-or-parent 
org-depend-block-todo)

 org-archive-location "%s_archive.org::"
 org-id-link-to-org-use-id 'create-if-interactive
 org-mode-hook '((lambda nil (setq paragraph-start "\f\\|[ ]*$" 
paragraph-separate "[     \f]*$")) turn-on-auto-fill org-tempo-setup 
org-clock-load
 #[0 "\300\301\302\303\304$\207" [add-hook 
change-major-mode-hook org-show-all append local] 5] flyspell-mode 
spacemacs/org-setup-evil-surround spacemacs/load-yasnippet
 #[0 "\301\211\207" [imenu-create-index-function 
org-imenu-get-tree] 2] toc-org-enable org-superstar-mode 
org-download-enable dotspacemacs//prettify-spacemacs-docs 
spacemacs//org-babel-do-load-languages
 #[0 "\300\301\302\303\304$\207" [add-hook 
change-major-mode-hook org-babel-show-result-all append local] 5] 
org-babel-result-hide-spec org-babel-hide-all-hashes 
spacemacs//evil-org-mode org-eldoc-load

 spacemacs//init-company-org-mode company-mode)
 org-clock-persist t