Subject: Re: bug#76964: 31.0.50; completion-preview-mode uses a lot of CPU

From: Eshel Yaron <[email-masked]>
Date: Wed, 12 Mar 2025 07:48:38 +0100

Hi Geza,

Geza Herman <[email-masked]> writes:

> When moving around a buffer with completion-preview-mode enabled, CPU
> usage becomes very high.
>
> Profiling the problem reveals that with completion-preview-mode,
> redisplay_internal takes much more CPU.
>
> The problem is in completion-preview--post-command, which calls
> (completion-preview-active-mode -1). For some reason, even though this
> should be a nop when just moving around a buffer, it forces emacs to
> do some more thorough redisplay. If I change this code to "(when
> completion-preview-active-mode (completion-preview-active-mode -1))",
> the problem goes away.
>
> I think the problem is inside the define-minor-mode macro, because if
> I change completion-preview-active-mode to an empty minor mode
> definition:
>
> (define-minor-mode completion-preview-active-mode
>   "Mode for when the completion preview is shown."
>   :interactive nil)
>
> the problem still happens. Note: looking inside the define-minor-mode
> macro, I see a (force-mode-line-update) call. I'm not sure it's
> related, but it has a chance that it is.

Thanks for investigating and reporting.  I think that conditioning the
(completion-preview-active-mode -1) in completion-preview--post-command
as you suggest makes sense.  Would you like to provide a simple patch
with that change?

Best,

Eshel