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

From: Eshel Yaron <[email-masked]>
Date: Sun, 16 Mar 2025 10:17:23 +0100

Eshel Yaron <[email-masked]> writes:

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

I've now pushed such a change along with some additional commentary to
the emacs-30 branch in commit b6b4a080a3a.  It should make its way to
master soon enough.  Could you please check whether that improves
performance for you?

Thanks,

Eshel