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

From: Ihor Radchenko <[email-masked]>
Date: Mon, 01 Jan 2024 18:02:30 +0000

Gerd Möllmann <[email-masked]> writes:

>> (defvar-local let-tests-buffer-local-var 'value)
>> (with-temp-buffer
>>   (setq-local let-tests-buffer-local-var 'baz)
>>   (let ((let-tests-buffer-local-var 'bar))
>>     ;; This is failing.
>>     (should (eq 'bar (default-value 'let-tests-buffer-local-var)))
>
> C-h f default-value explains this:
>
>   Return SYMBOL’s default value.
>   This is the value that is seen in buffers that do not have their own values
>   for this variable.

Are you saying that `let' never changes `default-value' output?
Unfortunately, no.

I first expected (by reading the C sources) that the default value is
changed by `let', but it actually looks like `let' sometimes does change
the default value and sometimes does not.

See the following two almost identical snippets that yield completely
different results depending on the present of `setq-default':

(defvar-local let-tests-buffer-local-var 'value) ;; default value is 'value
(with-temp-buffer
  (let ((existing-buffer (current-buffer)))
    (with-temp-buffer
      (setq-local let-tests-buffer-local-var 'baz)
      (let ((let-tests-buffer-local-var 'bar))
	;; Let does not change the default value for variable.
	(should (eq 'value (default-value 'let-tests-buffer-local-var)))
	(should (eq 'bar let-tests-buffer-local-var))
	(with-temp-buffer
	  ;; We are in a new buffer - `let-tests-buffer-local-var' has its global default value.
	  (should (eq 'value (default-value 'let-tests-buffer-local-var)))
	  (should (eq 'value let-tests-buffer-local-var)))
        (with-current-buffer existing-buffer
          ;; The above let DOES NOT change the current value in all the buffer
          ;; _existing_ when `let' is invoked.
	  (should (eq 'value let-tests-buffer-local-var))
          ;; Global default value is still unchanged.
          (should (eq 'value (default-value 'let-tests-buffer-local-var))))))))

(with-temp-buffer
  (let ((existing-buffer (current-buffer)))
    (with-temp-buffer
      ;; !!! After commenting out this line, the behaviour changes.
      ;; (setq-local let-tests-buffer-local-var 'baz)
      (let ((let-tests-buffer-local-var 'bar))
	;; Now, let DOES change the default value for variable.
	(should (eq 'bar (default-value 'let-tests-buffer-local-var)))
	(should (eq 'bar let-tests-buffer-local-var))
	(with-temp-buffer
	  ;; We are in a new buffer - `let-tests-buffer-local-var' still has
	  ;; let-bound global and local values.
	  (should (eq 'bar (default-value 'let-tests-buffer-local-var)))
	  (should (eq 'bar let-tests-buffer-local-var)))
        (with-current-buffer existing-buffer
          ;; The above let DOES change the current value in all the buffer
          ;; _existing_ when `let' is invoked.
	  (should (eq 'bar let-tests-buffer-local-var))
          ;; Global default value is still let-bound.
          (should (eq 'bar (default-value 'let-tests-buffer-local-var))))))))

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