r/emacs•
fredmorcos

Emacs cursor navigation and insertion is really slow

Hello,

Navigating and editing buffers is really slow in c-mode, python-mode and probably others. It's become really frustrating. Is there a way I can profile navigation and editing only?

My primary suspect is post-command-hook, which is set to the following value in a c-mode buffer with about 800 lines of code, I'd like to find out which of those is the slowest.

(linum-schedule whitespace-post-command-hook
flycheck-hide-error-buffer flycheck-display-error-at-point-soon
flycheck-error-list-highlight-errors
flycheck-error-list-update-source
flycheck-perform-deferred-syntax-check company-post-command t)

The currently enabled minor modes are:

Auto-Composition  Auto-Compression Auto-Encryption  Column-Number
Company   Dynamic-Completion   Electric-Indent   File-Name-Shadow
Flycheck Flycheck-Pos-Tip  Font-Lock Global-Font-Lock Gud-Tooltip
Helm-Gtags   Line-Number  Linum   Menu-Bar  Mouse-Wheel   Recentf
Savehist   Shell-Dirtrack   Show-Paren  Size-Indication   Tooltip
Transient-Mark Whitespace

And here is my emacs init file.

emacs-version: GNU Emacs 24.5.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.20.6) of 2016-06-04 on juergen, using the gtk3 interface.

Thank you.

EDIT: Formatting.

EDIT 2: More formatting.

EDIT 3: Typo.

Update:

I ran profiler-start and navigated around, then profiler-report and here is the output.

- timer-event-handler                                            2645  53%
 - byte-code                                                     2629  53%
  - apply                                                        2629  53%
   - linum-update-current                                        2622  53%
    - linum-update                                               2618  53%
     - mapc                                                      2610  53%
      - linum-update-window                                      2551  51%
       - funcall                                                   64   1%
          #<compiled 0x7f475f>                                     58   1%
         line-number-at-pos                                        19   0%
   - show-paren-function                                            3   0%
      show-paren--default                                           3   0%
 + timer-activate-when-idle                                         7   0%
   cancel-timer-internal                                            3   0%
- redisplay_internal (C function)                                1499  30%
 - linum-after-scroll                                            1368  27%
  - linum-update                                                 1362  27%
   + mapc                                                        1360  27%
     get-buffer-window-list                                         2   0%
 + eval                                                            66   1%
 + jit-lock-function                                               54   1%
 + #<compiled 0x4132e5>                                             6   0%
   file-remote-p                                                    5   0%
+ command-execute                                                 477   9%
+ ...                                                             219   4%
+ linum-schedule                                                   31   0%
+ company-post-command                                             12   0%
+ completion-before-command                                         9   0%
+ flycheck-display-error-at-point-soon                              7   0%
  flycheck-hide-error-buffer                                        5   0%
  company-pre-command                                               3   0%
  flycheck-pos-tip-hide-messages                                    2   0%

So I disabled linum-mode and things are smoother but sometimes still sluggish especially when the buffer needs to move/scroll.

At this point I'm not sure how to fix linum-mode (linum-after-scroll, linum-update).

Another update: here is the profiler output without linum-mode:

- redisplay_internal (C function)                                1908  42%
 - jit-lock-function                                             1521  33%
  - jit-lock-fontify-now                                         1518  33%
   - funcall                                                     1507  33%
    + #<compiled 0xcb4723>                                       1502  33%
    + #<compiled 0xd2d64b>                                          5   0%
 - eval                                                           297   6%
    flycheck-mode-line-status-text                                160   3%
    if                                                             76   1%
    mode-line-eol-desc                                              7   0%
   file-remote-p                                                   41   0%
 + #<compiled 0x4132e5>                                            38   0%
- command-execute                                                1435  31%
 - call-interactively                                            1424  31%
  - previous-line                                                 723  15%
   + funcall                                                      714  15%
   + called-interactively-p                                         6   0%
  + next-line                                                     482  10%
  + byte-code                                                     103   2%
  + execute-extended-command                                       45   0%
  + newline                                                        19   0%
  + save-buffer                                                    14   0%
  + c-electric-backspace                                           11   0%
  + scroll-up-command                                               7   0%
  + self-insert-command                                             6   0%
  + completion-separator-self-insert-autofilling                    1   0%
+ timer-event-handler                                             551  12%
+ ...                                                             441   9%
+ flycheck-handle-signal                                           40   0%
+ company-post-command                                             22   0%
+ completion-before-command                                        21   0%
+ flycheck-display-error-at-point-soon                             21   0%
+ linum-schedule                                                   15   0%
  x-set-selection                                                  15   0%
+ whitespace-post-command-hook                                     13   0%
+ company-pre-command                                              12   0%
+ flycheck-hide-error-buffer                                       10   0%
  internal-timer-start-idle                                         7   0%
+ flycheck-pos-tip-hide-messages                                    4   0%
  tooltip-hide                                                      3   0%
+ flycheck-perform-deferred-syntax-check                            3   0%
+ deactivate-mark                                                   3   0%
+ #<compiled 0xcbb30b>                                              1   0%

Posted Sep 12, 2016, 10:15 AM UTC · edited

211
permafrosty•

If line numbers are important to you, you could try nlinum-mode instead of linum-mode (available from GNU ELPA). It doesn't update as often so it might be better.

As for the slowness without linum-mode, what do you see when you open up the funcall under previous-line:

- previous-line                                                 723  15%
 + funcall                                                      714  15%

Is it just some compiled code or do you get further functions? Same thing with next-line. Those two and jit-lock seem to be the ones where most time is spent. Especially for (previous|next)-line I find that somewhat surprising.

1
fredmorcosOP•

Thanks! The problem still persists to a lesser extent, but still noticeable, with linum-mode disabled. I'm currently looking into font-lock.

1
fredmorcosOP•
- command-execute                                                 130  56%
 - call-interactively                                             130  56%
  - previous-line                                                  60  26%
   - funcall                                                       60  26%
    - #<compiled 0x10c089d>                                        60  26%
     - line-move                                                   60  26%
        line-move-visual                                           42  18%
      - default-line-height                                         4   1%
         default-font-height                                        4   1%
  - next-line                                                      41  17%
   - funcall                                                       41  17%
    - #<compiled 0x1312aa9>                                        41  17%
     - line-move                                                   41  17%
        line-move-visual                                           27  11%
      - default-line-height                                        10   4%
         default-font-height                                        6   2%
  + minibuffer-complete                                            19   8%
  + execute-extended-command                                        6   2%
  + byte-code                                                       4   1%
+ redisplay_internal (C function)                                  86  37%
+ flycheck-display-error-at-point-soon                              5   2%
  completion-before-command                                         4   1%
+ flycheck-pos-tip-hide-messages                                    3   1%
  tramp-set-auto-save-file-modes                                    2   0%
+ ...                                                               0   0%
1
tuhdo•
  • Have you tried turning off flycheck and company?
  • Can you reproduce it with stock Emacs i.e. emacs -q? You should really try with stock Emacs. If stock Emacs does not have this problem, then this might be the activated packages in that mode.

Only 800 lines of code, even linum-mode should not be slow.

1
fredmorcosOP•

Thanks for your answer! Most of the time is spent on things like previous-line, next-line and jit-font-lock when linum-mode is disabled. The same holds with emacs -q.

https://www.reddit.com/r/emacs/comments/52dmv1/emacs_cursor_navigation_and_insertion_is_really/d7jif7e

1
RobThorpe•

The same holds with emacs -q.

Do you mean that it's slow using emacs -q ? That's surprising. If so, have you compiled Emacs yourself?

1
fredmorcosOP•

Sorry that I wasn't clear. By that I mean that these functions are the biggest offenders even with emacs -q. It's definitely more responsive, but you can still feel the difference between emacs and gvim (both gtk3).

I haven't compiled emacs myself, I'm using the version from the Archlinux repositories.

1
RobThorpe•

I see. On small files Emacs is usually very fast. It may be that the file in question has some strange features which cause font-lock to be much slower than normal. It may be because the Archlinux maintainers made a mistake when building it. Does it happen on all files or just one in particular?

There are several reasons why the main functions in the trace may be the same for customized Emacs and Emacs -q. One is that the customizations are affecting what's happening within those functions. Another is that with customizations Emacs's memory footprint will be larger, meaning less cache memory for the core functions and slower performance.

1
RobThorpe•

Also, you might want to try setting jit-lock-stealth-fontify, though I've never found that necessary for small files.

1
ws-ilazki•

Have you tried using emacs-lucid instead of the gtk one? I've noticed all sorts of wonkiness with emacs-gtk3, eventually got sick of it and went with lucid and coloured the widgets to match the rest of my emacs theme.

1
gottabeme•

You can probably find it fairly quickly by using the bug-hunter package to bisect your config.

1