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

From: Michael Heerdegen <[email-masked]>
Date: Thu, 28 Sep 2023 01:30:37 +0200

Ihor Radchenko <[email-masked]> writes:

> So, using `with-current-buffer' when looping over all the buffers is
> certainly not optimal (maybe in other places as well).
>
> However, even `buffer-local-value' is still costly - it adds up over 50%
> run time.

OTOH - I tried to reproduce your experiments here locally -
`find-buffer-visiting' only takes 12% of the total run time here when
using the `buffer-local-value' tuned definition of `find-buffer-visiting'.

When we additionally merge the two loops in `find-buffer-visiting' into
one loop and avoid the duplicated calls of `buffer-local-value', don't we
reach a region where maintaining a separate cache doesn't make sense for
the additional gain?

And I wonder which part(s) of `buffer-local-value' is/are that costly,
and why.

Storing the same information twice - once in a buffer local variable,
and then again in a cache that needs to be updated separately doesn't
sound very reasonable.

Michael.