The patch as provided applies cleanly and all tests pass on main (fd6546e1a9dddedd913a4adaf2ea76eea65c7f67). It makes sense, but as you point out I think we probably want to restructure to reduce the duplication.
Cheers, Derek On Mon, Nov 2, 2026 at 12:11 PM Marcel van der Boom <[email protected]> wrote: > Hi everyone, > > I noticed my emacs started to lag after a few days and it turned > out there were over 2000 `org-indent-initialize-agent' timers in > `timer-idle-list'. > That caused a delay of .45 secs on every .2s idle pause. > > The patch attached cancels the timer if the > `org-indent-agentized-buffers' list is empty and there is a timer. > The code used to take an empty list as meaning there was no agent > running, but the list empties as soon as a buffer finishes > initializing or turns the mode off, while the agent only cancels > itself on its next run. A buffer enabling the mode in between > starts a second timer and overwrites `org-indent-agent-timer'; the > old timer later cancels the new one through that variable and > keeps running, unreferenced. > > > Reproducing: > > #+begin_src emacs-lisp > (require 'org-indent) > (dotimes (_ 3) > (with-current-buffer (generate-new-buffer "x") > (insert "* H\nText\n") > (org-mode) > (org-indent-mode 1) > (org-indent-initialize-agent))) > (seq-count (lambda (tm) > (eq (timer--function tm) > #'org-indent-initialize-agent)) > timer-idle-list) > ;; => 3, expected 1 > #+end_src > > The patch could be better by moving the duplicated push above the `if', > with the test becoming whether > the list was empty before it: > > #+begin_src emacs-lisp > (push (current-buffer) org-indent-agentized-buffers) > (unless (cdr org-indent-agentized-buffers) > (when (timerp org-indent-agent-timer) > (cancel-timer org-indent-agent-timer)) > (setq org-indent-agent-timer > (run-with-idle-timer > 0.2 t #'org-indent-initialize-agent))) > #+end_src > > That reads cleaner, but makes the diff a restructure rather than > an addition, so I kept the patch minimal. > > Org main f46d8a23a, GNU Emacs 32.0.50. > > > Marcel > > -- +---------------------------------------------------------------+ | Derek Chen-Becker | | GPG Key available at https://keybase.io/dchenbecker and | | https://pgp.mit.edu/pks/lookup?search=derek%40chen-becker.org | | Fngrprnt: EB8A 6480 F0A3 C8EB C1E7 7F42 AFC5 AFEE 96E4 6ACC | +---------------------------------------------------------------+
