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

From: Dmitry Gutov <[email-masked]>
Date: Fri, 22 Sep 2023 04:36:50 +0300

Hi Ihor,

On 20/09/2023 11:53, Ihor Radchenko wrote:
> In the attached profile, ~500 Org buffers are being opened and calls to
> `find-buffer-visiting' take 12.4 sec out of total 30.9 sec - 1/3 of all
> the execution time. After replacing some of the calls to
> `find-buffer-visiting' with `get-file-buffer', the total execution time
> decreased to ~20 sec, confirming that `find-buffer-visiting' is
> contributing significantly to slow performance.
> 
> Would it be possible to implement some kind of caching mechanism to be
> used by `find-buffer-visiting'?

I'm guessing you Cc'd me because of an existing comment inside xref.el? 
As you can see I decided not to use this function there, but even 
get-file-buffer wasn't as fast as I would've wanted, so there's a 
quick-and-dirty caching solution for sequential lookups (which assumes 
that the same file would be looked up multiple times in a row).

Putting aside the perspective of maintaining a cache in the core 
(hopefully this will be discussed later in this feature request), any 
chance you could initialize that cache yourself first (by iterating 
through buffers and building file-truename -> buffer map), before you 
are doing those many lookups? Somewhere in org-agenda-list, perhaps.

That should change the complexity from N*M to N+M, more or less.