Subject: Re: bug#73046: 29.4; Emacs 100% CPU usage for several seconds when opening dired buffer over TRAMP

From: Suhail Singh <[email-masked]>
Date: Fri, 06 Sep 2024 21:39:12 -0400

Suhail Singh <[email-masked]> writes:

>>>> It seems to be related to font-locking, indeed. See variable
>>>> `dired-font-lock-keywords'. It specifies face recognition running basic
>>>> file oprtations. For example, ";; Broken Symbolic link" calls
>>>> `file-truename' and `file-exists-p', while "Symbolic link to a directory"
>>>> and ";; Symbolic link to a non-directory" invoke `file-truename' and
>>>> `file-directory-p'.
> ...
>>>> I believe it would be helpful to suppress these checks via a user
>>>> option.

For the benefit of others, absent such a user option, the below code
works around the issue:

#+begin_src emacs-lisp
  (add-hook 'dired-mode-hook
  	  (lambda ()
  	    (setq-local dired-font-lock-keywords
  			(cons
  			 (list dired-re-sym
  			       '(".+" (dired-move-to-filename) nil (0 dired-symlink-face)))
  			 (seq-remove (lambda (x)
  				       (string= (car x) dired-re-sym))
  				     dired-font-lock-keywords)))))
#+end_src

I believe that the above isn't a fix, but simply a workaround since
TRAMP awaiting a response over a slow connection shouldn't result in
100% CPU usage.  Functions such as `tramp-wait-for-regexp' should not be
using excess CPU when they are simply awaiting a response.

>>>> And no, the checks shouldn't be suppressed for remote directories
>>>> in general, on a fast connection they are valuable.

For this reason, those interested in working around the issue in a
similar manner as above, may want to add additional guards to the hook
to ensure that the informative font-locking is suppressed (by modifying
dired-font-lock-keywords) only when necessary.

-- 
Suhail