Subject: bug#66117: 30.0.50; `find-buffer-visiting' is slow when opening large number of buffers

From: Ihor Radchenko <[email-masked]>
Date: Sat, 23 Sep 2023 08:22:17 +0000

Eli Zaretskii <[email-masked]> writes:

>> Because `buffer-file-name' can be modified from Lisp (via
>> `set-visited-file-name' or directly). Same for `buffer-file-truename'
>> and `buffer-file-number'.
>
> You could update the cache in set-visited-file-name, and ignore
> direct changes.

I have eyeballed Emacs sources, and it looks like a huge number of
libraries sets `buffer-file-name' directly. Also, even
`find-file-noselect' does not use `set-visited-file-name'.

- Functions setting ~buffer-file-name~ manually (except those setting it to nil):
  - tests
  - ~vc-find-revision-no-save~
  - ~url-insert-buffer-contents~
  - ~plstore-open~
  - ~protect-innocence-hook~ (really?)
  - ~tramp-handle-insert-file-contents~
  - ~tramp-archive-handle-insert-file-contents~
  - ~mailcap-view-file~
  - ~ange-ftp-parse-netrc~
  - ~ange-ftp-write-region~
  - ~ange-ftp-insert-file-contents~
  - ~mh-display-msg~
  - ~mh-make-folder~
  - ~mh-read-draft~
  - ~feedmail-vm-mail-mode~
  - ~feedmail-send-it~
  - ~jka-compr-write-region~
  - ~jka-compr-insert-file-contents~
  - ~image-dired-write-tags~
  - ~image-dired-remove-tag~
  - ~image-dired-write-comments~
  - ~hfy-buffer~
  - ~nndraft-request-associate-buffer~
  - ~nndraft-auto-save-file-name~
  - ~nnbabyl-create-mbox~
  - ~mm-display-inline-fontify~
  - ~mm-url-insert-file-contents~
  - ~mm-extern-url~
  - ~message-send-mail-with-mh~
  - ~message-set-auto-save-file-name~
  - ~gnus-dribble-read-file~
  - ~gnus-save-newsrc-file~
  - ~gnus-gnus-to-newsrc-format~
  - ~gnus-mime-copy-part~

  - ~find-alternate-file~
  - ~find-file-noselect-1~ (but not by default?)
  - ~set-visited-file-name~
  - ~file-name-non-special~

  - ~erc-dcc-find-file~
  - ~epa-file-insert-file-contents~
  - ~epa-file-write-region~
  - ~save-completions-to-file~
  - ~load-completions-from-file~
  - ~archive-extract~

- Functions setting ~buffer-file-truename~ manually (except those setting it to nil):
  - ~tar-extract~
  - ~find-alternate-file~
  - ~find-file-noselect-1~
  - ~set-visited-file-name~
  - ~revert-buffer--default~
  - ~archive-extract~

- Functions setting ~buffer-file-number~ manually (except those setting it to nil):
  - ~find-alternate-file~
  - ~find-file-noselect-1~
  - ~set-visited-file-name~
  - ~basic-save-buffer~

>> Or should we just assume that these variables remain unchanged other
>> than by primitives?
>
> Programs that make these changes are asking for trouble, IMO.  AFAICT,
> find-buffer-visiting will never find such buffers anyway.

It would, in its current form. Because it calls `get-file-buffer' that
loops over all the buffers and checks their buffer-local
`buffer-file-name' value, including values set via `setq' in Elisp.

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