Subject: Re: bug#23327: 25.0.92; show-trailing-whitespace uses too much cpu

From: Eli Zaretskii <[email-masked]>
Date: Thu, 21 Apr 2016 17:16:08 +0300

> Date: Thu, 21 Apr 2016 11:33:55 +0530
> From: Mohammed Sadik <[email-masked]>
> 
> Enabling show-trailing-whitespace and navigating through buffer uses too
> much CPU.  An increase of about 4-6% of CPU on my 2.4 GHz Quad-core
> system.
> 
> How to reproduce:
> 
> 1. Open a large source code file (I opened cc-engine.el from Emacs source).
> 2. Enable show-trailing-whitespace (setq show-trailing-whitespace t)
> 3. Press and hold the arrow keys to navigate. See the processor usage
>    (use top/htop or similar program).
> 4. Now disable show-trailing-whitespace (setq show-trailing-whitespace
>    nil)
> 5. Repeat 3
> 
> This also happens when pressing any other key (eg:while typing).

The Emacs display engine employs several aggressive optimizations for
simple operations like cursor motion and inserting a single
character.  When you turn on show-trailing-whitespace, most of these
optimizations are disabled, so it's very small wonder that redisplay
becomes more expensive.

The reason that these optimizations are disable is this feature of
show-trailing-whitespace:

     This feature does not apply when point is at the end of the line
  containing the whitespace.  Strictly speaking, that is trailing
  whitespace nonetheless, but displaying it specially in that case looks
  ugly while you are typing in new text.  In this special case, the
  location of point is enough to show you that the spaces are present.

Because of this, when you move the cursor, the display engine cannot
assume that the display remains unchanged, because it needs to see if
the cursor happens to be after a stretch of trailing whitespace, in
which case, that whole stretch of whitespace needs to be redrawn as
well.  For the same reason, other optimizations, like those which
reuse portions of the previously displayed window, are abandoned.

I've looked at the optimizations we disable in this case, and I cannot
see anything wrong in those decisions, given the way this feature
works.

We could try adding an optional feature which doesn't turn off the
special display of trailing whitespace when the cursor is at the end
of a line that ends in whitespace, that might allow us to re-enable
these optimizations.  Would users like that?

Other than that, I don't see how this could be helped, except by some
thorough redesign of how this feature works.  I don't immediately see
how to do that, but maybe someone else does, and is motivated to work
on this.

> Also this creates minor glitches in displaying text on buffer
> (sudden disappearance and appearence of some text) when CPU is already
> enough busy.

If your CPU is very busy, then adding a few percents to it might
indeed produce such jerky redisplay, I think.