Subject: bug#46350: 28.0.50; touchpad-scrolling-eats-lots-of-cpu-samples

From: Andrey Orst <[email-masked]>
Date: Sat, 6 Feb 2021 20:33:56 +0300

Hello.  I'm experiencing major slowdown in Emacs -q fundamental mode
when setting `mouse-wheel-progressive-speed' to `nil'.  Here are
reproduction steps:

1. Run emacs -q
2. Open some big file, in my case I've opened lisp-mode.el
3. Turn on fundamental-mode (optional)
4. Go to the end of the buffer.
5. Rapidly produce scroll up events from touchpad or mouse wheel
   (although it is much harder to achieve with mouse) with significant
   scroll amount.  By significant scroll amount I mean continuous
   scrolling event from touchpad, which can be achieved by swiping over
   whole touchpad area very rapidly.
6. Observe that Emacs doesn't scroll almost at all and just waits until
   scroll events will stop.  Then it scrolls correct amount.

The delay I'm talking about is what bothers me.

First, there's no delay at all when scrolling with scrollbars
(provided by `scroll-bar-mode').  I can scroll with scrollbars as
rapidly as I want, and there is literally no lag whatsoever.

Second, when producing slow scrolling motion on touchpad or mouse
(e.g. one line at a time) scrolling is very responsive.  But I'm
scrolling one line at a time very rarely so this is useless to me.

Third, this bug also reproduces with default value of
`mouse-wheel-progressive-speed', although it is a bit more difficult
to catch this as it requires enormously big buffer for lag to build
up, as scrolling increases in speed (seemingly) exponentially and can
reach top of the buffer before lag builds up significantly.

Last but not least, this also happens when scrolling down (to buffer
end) but the lag seems to be smaller, yet it still noticeable.

I've did some profiling during irresponsibility the amount of CPU
samples is enormous, but down the tree, called functions don't consume
much.  Here's a fragment of report, and I've also attached profiler
exported report to the mail:

        4400  88% - command-execute
        4359  87%  - funcall-interactively
        3776  75%   - mwheel-scroll
          26   0%      mouse-wheel--get-scroll-window
          26   0%    - run-with-timer
          23   0%     - run-at-time
           4   0%        timer-set-time
           1   0%        timer-set-function
           1   0%        timer-activate
          23   0%    - scroll-up
          12   0%     - eval
           8   0%      - if
           1   0%         display-graphic-p
           1   0%        mode-line-eol-desc
          19   0%    - scroll-down
           9   0%     - eval
           7   0%      - if
           1   0%         frame-parameter
           1   0%        unless
          15   0%    - error-message-string
          11   0%     - substitute-command-keys
           7   0%      - #<compiled -0x1fc708cc6b8a9a73>
           4   0%       - kill-buffer
           2   0%        - replace-buffer-in-windows
           1   0%         - unrecord-window-buffer
           1   0%            assq-delete-all
           2   0%      #<compiled -0xd2dd66c27d231f8>
           1   0%      message
         288   5%   - scroll-bar-toolkit-scroll
         264   5%    - sit-for
          10   0%     - redisplay_internal (C function)
           2   0%      - eval
           1   0%         mode-line-eol-desc
           1   0%         if

My wild guess is that touchpad sends much more input to Emacs, compared
to mouse, and that's why scrolling function has to work with much more
inputs and hence the struggle.  I was under assumption that Emacs'
font-lock was the culprit here, as disabling it kinda helps, but then
I've managed to reproduce it without font locking and now I think that
there might be another issue.  Everything I've tested happened on GCC
Emacs branch, but also happens (and actually a bit more noticeable in
27.1 and master branches).

In GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.13, cairo version 1.16.0)
 of 2021-01-17 built on toolbox
Repository revision: 88100bed0af530f04cf56acca9f9d1bb12b45771
Repository branch: feature/native-comp
Windowing system distributor 'Fedora Project', version 11.0.12010000
System Description: Fedora 33 (Workstation Edition)

Configured using:
 'configure --with-nativecomp --with-mailutils
 --prefix=/home/andreyorst/.local/emacs
 --bindir=/home/andreyorst/.local/bin
 '--program-transform-name=s/^ctags$/ctags.emacs/''

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSELINUX LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY
INOTIFY PDUMPER PNG RSVG SOUND THREADS TIFF TOOLKIT_SCROLL_BARS X11 XDBE
XIM XPM GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
rfc822 mml mml-sec epa derived epg epg-config gnus-util rmail
rmail-loaddefs auth-source eieio eieio-core eieio-loaddefs
password-cache json map mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils jka-compr shortdoc text-property-search
cl-indent comp comp-cstr warnings rx cl-seq cl-macs cl-extra seq
byte-opt gv bytecomp byte-compile cconv inf-lisp comint ansi-color ring
thingatpt profiler help-fns radix-tree cl-print debug backtrace
help-mode find-func time-date subr-x vc-git diff-mode easymenu
easy-mmode cl-loaddefs cl-lib iso-transl tooltip eldoc electric uniquify
ediff-hook vc-hooks lisp-float-type mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame minibuffer cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite charscript charprop case-table epa-hook
jka-cmpr-hook help simple abbrev obarray cl-preloaded nadvice button
loaddefs faces cus-face pcase macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote threads dbusbind inotify lcms2
dynamic-setting system-font-setting font-render-setting cairo
move-toolbar gtk x-toolkit x multi-tty make-network-process nativecomp
emacs)

Memory information:
((conses 16 120435 14545)
 (symbols 48 9569 1)
 (strings 32 28965 2841)
 (string-bytes 1 952843)
 (vectors 16 38276)
 (vector-slots 8 804855 15451)
 (floats 8 80 204)
 (intervals 56 3638 189)
 (buffers 984 12))

-- 
Best regards,
Andrey Listopadov