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 16:33:05 +0000

Eli Zaretskii <[email-masked]> writes:

>> What do you think about introducing something like
>> 
>> struct buffer buffer_overrides;
>> 
>> It will be similar to buffer_defaults  in buffer.c, but will hold the
>> overriding values of buffer-local variables that are to be used instead
>> of proper buffer-local variables. That way, we can speed up let-binding
>> of case-fold-search.
>
> I'm sorry, I don't think I understand how this will work and avoid the
> problems we have with let-binding.  So I added Stefan, in the hope
> that he might understand what I am evidently missing.

Let me elaborate.

- In eval.c, `Flet' always uses `specbind' to bind `case-fold-search'.
  Because `case-fold-search' is declared special (u.s.declared_special =
  true).

- For `case-fold-search', `specbind' does

  	    /* If SYMBOL is a per-buffer variable which doesn't have a
	       buffer-local value here, make the `let' change the global
	       value by changing the value of SYMBOL in all buffers not
	       having their own value.  This is consistent with what
	       happens with other buffer-local variables.  */
	    if (NILP (Flocal_variable_p (symbol, Qnil)))
	      specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;

- Later, `specbind' calls `do_specbind' where SPECPDL_LET_DEFAULT causes
  a call to `set_default_internal'.

- Then, `set_default_internal', for any variable with positive
  buffer_local_flags loops over all the live buffers to overwrite the
  buffer-local value.

  This loop is the bottleneck, making let-binding
  of `case-fold-search' (and any other variable marked in buffer.c:4691
  block) scale with the number of open buffers.

- What I propose is to avoid this loop in `do_specbind' altogether.
  Instead of having to loop through all the buffers to set temporary
  buffer-local value, I propose to introduce special buffer object
  `buffer_overrides' that will hold such temporary bindings.
  Then, we can change BVAR to something like

  #define BVAR(buf, field) (buffer_overrides->field ## _ == UNSET ?\
                              (buf)->field ## _ :\
                             buffer_overrides->field ## _)

  and replace the loop with simply setting buffer_overrides slot.

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