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

From: Ihor Radchenko <[email-masked]>
Date: Tue, 12 Dec 2023 19:18:19 +0000

Eli Zaretskii <[email-masked]> writes:

>> > So for starters, we make each BVAR more expensive, i.e. make Emacs
>> > uniformly slower (because we call BVAR all over the place).
>> 
>> Yes, although I do not believe that it will have large impact in
>> practice. It is just an extra == comparison.
>
> We've seen how similar extra comparisons slow down Emacs when we
> introduced symbols-with-location.  IMO, BVAR might be even more
> expensive, since it is used much more frequently and widely.

It might. Do you know a good way to test such thing? Because I do not
think that a mere suspicion that this change might make things slower
should stop the discussion.

>> > ... And then
>> > I'm not sure I understand how buffer_overrides will get the values of
>> > those temporary bindings without having to use the same loops when we
>> > have let-binding within another one.  Also, what about threads?
>> 
>> Just as any other global state variable - via `specbind'. Entering inner
>> let will push the previous value into specpdl and recover it upon
>> exiting.
>
> And that will not require looping similar to what we have now?

No.
AFAIK, the way let-binding for dynamic scoping works is:
1. push existing variable value to specpdl
2. assign a new value
3. when leaving let scope, pop the record from specpdl and assign the
   old value

What I propose is changing the way value is assigned. Now, the assignment of
buffer-local variable that is recorded into buffer object slot involves
doing it in all the live buffers. I propose to do it in a special buffer
object instead, which is strictly faster.

>> Threads work similarly, maintaining their own local specpdl queue heads
>> that make sure that global state is recorded and rewinded as needed.
>
> I don't think I follow you here.

See `rebind_for_thread_switch' and `unbind_for_thread_switch'.
The workhorse is `specpdl_unrewind', which see.

-- 
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>