Slow org-table-align

What happened?

Emacs freezes for 2-3 seconds if I call org-table-align on my table (I’ll share it below) in an org-file. I did profiling:

Memory profile result:

21,764,188  87% - command-execute
15,120,755  60%  - funcall-interactively
13,383,452  53%   - execute-extended-command
13,382,276  53%    - command-execute
13,382,260  53%     - funcall-interactively
12,843,676  51%      - org-table-align
 5,249,111  21%       - cl-mapcar
 5,242,775  21%        - cl--mapcar-many
 5,175,575  20%         - org-table--align-field
 4,983,812  20%          - org-string-width
 4,322,972  17%           - set-window-buffer
 4,322,972  17%            - apply
 4,322,972  17%             - ad-Advice-set-window-buffer
 4,115,744  16%              - evil-local-mode
 3,902,432  15%               + evil-initialize-state
    33,792   0%               + evil-disabled-buffer-p
    28,512   0%                 evil--jumps-install-or-uninstall
    19,008   0%               + called-interactively-p
    12,672   0%               + evil-concat-lists
     4,224   0%               + evil-collection-magit-remove-visual-activate-hook
    12,672   0%              + #<subr set-window-buffer>
   457,236   1%           + #<compiled -0x1ddf345b607e5ed7>
   177,408   0%             version<
 2,817,536  11%       + org-fold-core--fix-folded-region
 2,447,428   9%       + org-string-width
 1,036,461   4%       + org-element--cache-after-change
   240,084   0%       + flycheck-handle-change
   234,952   0%       + org-indent-refresh-maybe
   219,168   0%       + jit-lock-after-change
    90,437   0%       + #<compiled -0x222b0cf8be8fd9c>
    20,064   0%         ws-butler-after-change
     2,208   0%       + org-indent-notify-modified-headline
     1,024   0%       + org-table-current-column
     1,024   0%         #<compiled -0x2220abd0b792394>
       560   0%         org-table--list-shrunk-columns
        80   0%         org-table-expand
     1,176   0%    + run-at-time
 1,737,303   6%   + doom/toggle-profiler
 6,643,433  26%  - byte-code
 6,643,433  26%   - read-extended-command
 6,643,433  26%    - completing-read-default
 6,643,433  26%     - apply
 6,643,433  26%      + vertico--advice
 3,140,361  12% + timer-event-handler
     1,296   0% + gcmh-register-idle-gc
     1,056   0%   winner-save-old-configurations
     1,024   0% + evil-escape-pre-command-hook
        80   0% + redisplay_internal (C function)
        80   0%   flycheck-maybe-display-error-at-point-soon
        24   0% + eldoc-schedule-timer
         0   0%   ...


CPU profile results:

2858  88% - command-execute
2711  84%  - funcall-interactively
2704  83%   - execute-extended-command
2704  83%    - command-execute
2704  83%     - funcall-interactively
2694  83%      - org-table-align
1346  41%       - org-string-width
1315  40%        - set-window-buffer
1314  40%         - apply
1312  40%          - ad-Advice-set-window-buffer
1289  39%           - evil-local-mode
 810  25%            + add-hook
 414  12%            + evil--jumps-install-or-uninstall
  58   1%            + evil-initialize-state
   3   0%            + evil-concat-lists
   2   0%            + #<lambda 0x1b3abd0a45dd8dbd>
  19   0%           + #<subr set-window-buffer>
  24   0%        + #<compiled -0x1ddf3440f15cacd7>
   7   0%        + version<
1276  39%       - cl-mapcar
1276  39%        - cl--mapcar-many
1275  39%         - org-table--align-field
1266  39%          - org-string-width
1230  38%           - set-window-buffer
1230  38%            - apply
1229  38%             - ad-Advice-set-window-buffer
1207  37%              - evil-local-mode
 760  23%               - add-hook
 760  23%                - #<compiled 0x19ac8867e04861cd>
  49   1%                   alist-get
 380  11%               - evil--jumps-install-or-uninstall
 380  11%                - add-hook
 380  11%                 - #<compiled 0x19ac8867e04861cd>
  26   0%                    alist-get
  56   1%               + evil-initialize-state
   2   0%                 evil-disabled-buffer-p
   2   0%               + evil-concat-lists
   1   0%               + #<lambda 0x1b3abd0a45dd8dbd>
  17   0%              + #<subr set-window-buffer>
  28   0%           + #<compiled -0x1ddf3489663744d7>
   5   0%           + version<
  28   0%       + org-fold-core--fix-folded-region
  13   0%       + org-element--cache-after-change
   5   0%         org-indent-notify-modified-headline
   4   0%       + org-indent-refresh-maybe
   3   0%       + flycheck-handle-change
   2   0%       + #<compiled -0x222b0cf8be8fd9c>
   1   0%       + jit-lock-after-change
   6   0%   + doom/toggle-profiler
 147   4%  + byte-code
 249   7% + ...
 120   3% + timer-event-handler

From these results you can see that most of the CPU is utilized by evil-local-mode. I see that there is the open issue in the project of evil-mode Advice of `set-window-buffer` unconditionally enables `evil-local-mode` · Issue #1561 · emacs-evil/evil · GitHub, so I believe that it can be fixed somehow in Doom Emacs, but I think someone else is experiencing the slow performance to dealing with it somehow, and hopefully the person could share a workaround to mitigate the experience.

What did you expect to happen?

Instant alignment of the table

| Name                    | value      | Tesco | Dunnes |  Lidl | Centra |
|-------------------------+------------+-------+--------+-------+--------|
| Cleaning cloth          | 6 pieces   |     2 |        |       |        |
| KM Scourers             | 2 pieces   |  1.15 |        |       |        |
| Sugar                   | 1 kg       |  1.39 |        |       |        |
| Saxa salt               | 1 kg       |  2.39 |        |       |        |
| Tesco peppers           | 2 pieces   |  1.49 |        |       |        |
| Pasta                   | 1 kg       |     1 |        |       |        |
| Black pepper            | 1 piece    |  1.00 |        |       |        |
| Basmati rice            | 1 kg       |   1.2 |        |       |        |
| Tuna chunks             | 1 kg       |   5.5 |        |       |        |
| Salad tomatoes          | 1 kg       |  3.03 |        |       |        |
| Carrot                  | 1 kg       |  1.38 |        |       |        |
| Spices/Herbs            |            |  0.60 |        |       |        |
| Ho paprika              |            |  0.75 |        |       |        |
| Light Milk 3L           | 1L         |  1.07 |        |       |        |
| Pepperoni Pizza 2Pk     | 1 piece    |       |        |  1.95 |        |
| Apple juice             | 1L         |       |        |  1.49 |        |
| Orange juice            | 1L         |       |        |  1.49 |        |
| Irish Whiskey Cream     | 1 piece    |       |        |  2.79 |        |
| Frankfurters 10 pieces  | 1kg        |       |        |  4.33 |        |
| Organic Squeezy (honey) | 350g       |       |        |  2.99 |        |
| Toasts (bread)          | 800g       |  1.19 |   0.99 |  2.19 |        |
| Cotton buds (sticks)    | 100 pieces |       |        |  0.99 |        |
| Jagermeister (0.7)      | 1L         |       |        | 37.14 |        |
| Bags for plastic        | 6 pieces   |       |        |       |     11 |
| Kitchen towel (paper)   | 2 pieces   |       |        |  1.29 |        |
| Cucumber                | 1 piece    |       |        |       |   0.69 |
| Pork chops (523g)       | 1 kg       |  5.73 |        |       |        |
| Pizza (deep baked)      | 1 piece    |  3.00 |        |       |        |
| T F/Range (12pk) eggs   | 10 pieces  |  3.17 |        |       |        |
|-------------------------+------------+-------+--------+-------+--------|
| Bed linen               | 1 piece    |       |     30 |       |        |
| Pillow cas              | 2 pieces   |       |      8 |       |        |
| Pillow                  | 2 pieces   |       |     15 |       |        |
| Duvet                   | 1 piece    |       |     45 |       |        |

Steps to reproduce

  1. Be in the evil-mode.
  2. Create an org file, put the table in it.
  3. Edit it so, the table will be realigned.
  4. Press ESC or invoke org-table-align.

System information


Loading data dump...

CPU: 2.9 GHz (AMD Ryzen 7 4800H) RAM: 64Gb

1 Like

Evil is wrong here. Advising such primitives is a bad idea to start with.