unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Avoiding 'with-temp-buffer' in tight loops
@ 2024-08-09  8:30 martin rudalics
  2024-08-09 10:36 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: martin rudalics @ 2024-08-09  8:30 UTC (permalink / raw)
  To: emacs-devel

Ever since 'substitute-command-keys' has been transferred to help.el,
putting initial breakpoints in Fkill_buffer has become virtually
impossible.

Could we pretty please replace the

     (let ((keymap overriding-local-map)
           (inhibit-modification-hooks t)
           (orig-buf (current-buffer)))
       (with-temp-buffer

with something like

     (let* ((keymap overriding-local-map)
            (inhibit-modification-hooks t)
            (orig-buf (current-buffer))
	   (buffer-name " *substitute-command-keys*")
	   (buffer (or (get-buffer buffer-name)
		       (generate-new-buffer buffer-name t))))
       (with-current-buffer buffer
	(erase-buffer)

This would eliminate the overhead associated with continuously creating
and killing a buffer and make debugging amenable.  Maybe we could also
add a note somewhere in 'with-temp-buffer' so people avoid calling it in
tight loops.  Then we could also do something similar for
'image-type-from-file-header', possibly erasing the contents of its
buffer after being done with it.

Thanks, martin



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Avoiding 'with-temp-buffer' in tight loops
  2024-08-09  8:30 Avoiding 'with-temp-buffer' in tight loops martin rudalics
@ 2024-08-09 10:36 ` Eli Zaretskii
  2024-08-10 13:59   ` martin rudalics
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2024-08-09 10:36 UTC (permalink / raw)
  To: martin rudalics; +Cc: emacs-devel

> Date: Fri, 9 Aug 2024 10:30:42 +0200
> From: martin rudalics <rudalics@gmx.at>
> 
> Ever since 'substitute-command-keys' has been transferred to help.el,
> putting initial breakpoints in Fkill_buffer has become virtually
> impossible.

It should be possible to make the breakpoint conditional on the first
character of a temporary buffer not being SPC.

> Could we pretty please replace the
> 
>      (let ((keymap overriding-local-map)
>            (inhibit-modification-hooks t)
>            (orig-buf (current-buffer)))
>        (with-temp-buffer
> 
> with something like
> 
>      (let* ((keymap overriding-local-map)
>             (inhibit-modification-hooks t)
>             (orig-buf (current-buffer))
> 	   (buffer-name " *substitute-command-keys*")
> 	   (buffer (or (get-buffer buffer-name)
> 		       (generate-new-buffer buffer-name t))))
>        (with-current-buffer buffer
> 	(erase-buffer)

This needs to be amended to support several threads calling
substitute-command-keys, I think.

An alternative for the above is to have substitute-command-keys bind
some variable non-nil, and have a breakpoint be conditional on that
variable's value.  If this works, I think it's simpler and cleaner.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Avoiding 'with-temp-buffer' in tight loops
  2024-08-09 10:36 ` Eli Zaretskii
@ 2024-08-10 13:59   ` martin rudalics
  2024-08-10 14:11     ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: martin rudalics @ 2024-08-10 13:59 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

 >> Ever since 'substitute-command-keys' has been transferred to help.el,
 >> putting initial breakpoints in Fkill_buffer has become virtually
 >> impossible.
 >
 > It should be possible to make the breakpoint conditional on the first
 > character of a temporary buffer not being SPC.

This fails when investigating a general misbehavior of 'kill-buffer'
that might also happen when the buffer name starts with a space.  And it
won't catch the case where such a buffer is killed inadvertently as
"current buffer".

 > This needs to be amended to support several threads calling
 > substitute-command-keys, I think.

Protecting access to any such buffer with the help of a mutex should be
trivial.  But IIUC we don't do such a thing anywhere and it's certainly
not done with Vprin1_to_string_buffer which was used earlier for this
purpose.  So my faith in the correct resolution of locks is limited.

 > An alternative for the above is to have substitute-command-keys bind
 > some variable non-nil, and have a breakpoint be conditional on that
 > variable's value.  If this works, I think it's simpler and cleaner.

This means that every tight loop using 'with-temp-buffer' would have to
set up and clear that variable.  It would fail to catch all cases where
killing a buffer associated with that variable is the cause of the
problem to fix.

martin



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Avoiding 'with-temp-buffer' in tight loops
  2024-08-10 13:59   ` martin rudalics
@ 2024-08-10 14:11     ` Eli Zaretskii
  2024-08-12  7:51       ` martin rudalics
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2024-08-10 14:11 UTC (permalink / raw)
  To: martin rudalics; +Cc: emacs-devel

> Date: Sat, 10 Aug 2024 15:59:29 +0200
> Cc: emacs-devel@gnu.org
> From: martin rudalics <rudalics@gmx.at>
> 
>  >> Ever since 'substitute-command-keys' has been transferred to help.el,
>  >> putting initial breakpoints in Fkill_buffer has become virtually
>  >> impossible.
>  >
>  > It should be possible to make the breakpoint conditional on the first
>  > character of a temporary buffer not being SPC.
> 
> This fails when investigating a general misbehavior of 'kill-buffer'
> that might also happen when the buffer name starts with a space.  And it
> won't catch the case where such a buffer is killed inadvertently as
> "current buffer".
> 
>  > This needs to be amended to support several threads calling
>  > substitute-command-keys, I think.
> 
> Protecting access to any such buffer with the help of a mutex should be
> trivial.  But IIUC we don't do such a thing anywhere and it's certainly
> not done with Vprin1_to_string_buffer which was used earlier for this
> purpose.  So my faith in the correct resolution of locks is limited.
> 
>  > An alternative for the above is to have substitute-command-keys bind
>  > some variable non-nil, and have a breakpoint be conditional on that
>  > variable's value.  If this works, I think it's simpler and cleaner.
> 
> This means that every tight loop using 'with-temp-buffer' would have to
> set up and clear that variable.  It would fail to catch all cases where
> killing a buffer associated with that variable is the cause of the
> problem to fix.

With respect, if you reject every solution, don't expect this to be
solved any time soon.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Avoiding 'with-temp-buffer' in tight loops
  2024-08-10 14:11     ` Eli Zaretskii
@ 2024-08-12  7:51       ` martin rudalics
  2024-08-12 10:55         ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: martin rudalics @ 2024-08-12  7:51 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

 > With respect, if you reject every solution,

I did not reject any solution.  I'm using conditional breakpoints when
debugging 'kill-buffer' all the time, in particular to trace only those
cases where the buffer to be killed is shown in some window.

But here I'm investigating a problem where Emacs hangs and kill -TSTP
doesn't get me anything useful.  I suspect that 'kill-buffer' might be
involved but have no real clue yet.  So the buffer may be any buffer
including those created by 'with-temp-buffer' and not tracing those
buffers with the debugger would be of no help.

 > don't expect this to be
 > solved any time soon.

Don't worry.  I fixed this locally so it won't bother me any more here.

Apologies for wasting your time.  It won't happen again.

martin



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Avoiding 'with-temp-buffer' in tight loops
  2024-08-12  7:51       ` martin rudalics
@ 2024-08-12 10:55         ` Eli Zaretskii
  0 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2024-08-12 10:55 UTC (permalink / raw)
  To: martin rudalics; +Cc: emacs-devel

> Date: Mon, 12 Aug 2024 09:51:36 +0200
> Cc: emacs-devel@gnu.org
> From: martin rudalics <rudalics@gmx.at>
> 
>  > With respect, if you reject every solution,
> 
> I did not reject any solution.  I'm using conditional breakpoints when
> debugging 'kill-buffer' all the time, in particular to trace only those
> cases where the buffer to be killed is shown in some window.
> 
> But here I'm investigating a problem where Emacs hangs and kill -TSTP
> doesn't get me anything useful.  I suspect that 'kill-buffer' might be
> involved but have no real clue yet.  So the buffer may be any buffer
> including those created by 'with-temp-buffer' and not tracing those
> buffers with the debugger would be of no help.
> 
>  > don't expect this to be
>  > solved any time soon.
> 
> Don't worry.  I fixed this locally so it won't bother me any more here.
> 
> Apologies for wasting your time.  It won't happen again.

You didn't waste my time, I was trying to help you solve your problem,
given what I understood from your descriptions.  But if no proposal
satisfies your needs, I guess there's no solution, at least not one
that you haven't tried already.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-08-12 10:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-09  8:30 Avoiding 'with-temp-buffer' in tight loops martin rudalics
2024-08-09 10:36 ` Eli Zaretskii
2024-08-10 13:59   ` martin rudalics
2024-08-10 14:11     ` Eli Zaretskii
2024-08-12  7:51       ` martin rudalics
2024-08-12 10:55         ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).