vedang / pdf-tools

Emacs support library for PDF files.
https://pdftools.wiki
GNU General Public License v3.0
620 stars 90 forks source link

emacs stuck/extremely slow when ivy-switch-buffer from pdf-tools buffer #122

Open maxecharel opened 2 years ago

maxecharel commented 2 years ago

(pdf-tools: Melpa, 20220619.2227; Emacs: 28.1) When I try to switch buffer (using M-x ivy-switch-buffer) from an active pdf-tools buffer, the popup with the completion framework takes ages to open (same to select the buffer I want to switch to) and emacs freezes. Since I only encountered this problem with pdf-tools, I've considered that it was a pdf-tools rather than an ivy bug, but I may be wrong. Here are two profiler reports (one as text, other as screenshot):

     SAMPLES    %   FUNCTION
    2327  76% - command-execute
        2327  76%  - call-interactively
        2258  74%   - byte-code
        2258  74%    - read-extended-command
        2258  74%     - completing-read
        2258  74%      - ivy-completing-read
        2258  74%       - ivy-read
        2258  74%        - apply
        2258  74%         - #<compiled 0x1365326a527ac9ba>
        2156  71%          - read-from-minibuffer
        2104  69%           - ivy--queue-exhibit
        2104  69%            - ivy--exhibit
        2068  68%             - ivy--update-minibuffer
        1723  56%              - ivy--format
        1715  56%               - mapcar
        1651  54%                - ivy-rich--ivy-switch-buffer-transformer
        1651  54%                 - ivy-rich-format
        1651  54%                  - mapconcat
        1651  54%                   - #<compiled -0x1e155f64bb11af29>
        1651  54%                    - ivy-rich-format-column
        1592  52%                     - all-the-icons-ivy-rich-switch-buffer-major-mode
        1592  52%                      - format-mode-line
        1592  52%                       - eval
        1592  52%                        - save-excursion
        1592  52%                         - let
        1592  52%                          - while
        1592  52%                           - when
        1588  52%                            - if
        1588  52%                             - condition-case
        1588  52%                              - dired-get-filename
        1588  52%                               - let
        1588  52%                                - save-excursion
        1588  52%                                 - if
        1588  52%                                  - setq
        1588  52%                                   - dired-move-to-filename
        1588  52%                                    - let
        1588  52%                                       cond
          31   1%                     + ivy-rich-switch-buffer-project
          28   0%                     + ivy-rich-switch-buffer-path
          64   2%                + ivy-rich--execute-extended-command-transformer
           4   0%               + ivy--wnd-cands-to-str
         341  11%              + ivy--filter
          32   1%             + ivy--insert-minibuffer
           8   0%           + timer-event-handler
           4   0%           + internal-timer-start-idle
          96   3%          + ivy--reset-state
           6   0%          + ivy-call
          69   2%   + funcall-interactively
         693  22% - timer-event-handler
         693  22%  - apply
         685  22%   - #<compiled -0x28309e470a8676c>
         681  22%    - execute-extended-command--shorter
         681  22%     - completion-try-completion
         681  22%      - completion--nth-completion
         681  22%       - completion--some
         681  22%        - #<compiled -0xd2cdad5a035b862>
         437  14%         - completion-pcm-try-completion
         429  14%          - completion-pcm--find-all-completions
         429  14%             completion-pcm--all-completions
           8   0%          + completion-pcm--merge-try
         244   8%           completion-basic-try-completion
           7   0%   - pdf-cache--prefetch-start
           7   0%    - pdf-cache--prefetch-pages
           7   0%     + pdf-cache-lookup-image
           1   0%     #<compiled 0x12c5e7f522dda936>
           6   0% + ...
           5   0% + redisplay_internal (C function)

Screenshot_20220620_184906

(@vedang thx for continuing the work of politza)

vedang commented 2 years ago

I don't think this is a pdf-tools problem. This is not to say that pdf-tools doesn't leak / accumulate memory (it does), but I don't think that there is any reason it should affect switching buffers. Have you installed some advice around buffer switching as part of your configuration that might be causing this? Try and re-start your Emacs with only pdf-tools and ivy loaded to see if you still hit the problem.

I'll profile / perf-check this a bit when I get the chance.

bymoz089 commented 2 years ago

I can not confirm this issue, when using the same software versions and Linux.

What OS are you using?

maxecharel commented 2 years ago

I use ivy, counsel, ivy-prescient (to get most used suggested first) and ivy-rich (to provide additional info about items). OS: Kubuntu 22.04

maxecharel commented 2 years ago

I disabled ivy-prescient and ivy-rich and the performance issue seems to have disappeared. I will definitely confirm that later on (and will also try to identify which one of the two is critical here) but still I do not understand why I only encountered this issue with pdf-tools buffers.

Edit: the problem seems to come from ivy-rich, and apparently is particularly acute when called from a pdf-tools buffer, for an unknown reason. What I do not understand is the fact that ivy-rich appears nowhere in the profiler report.

maxecharel commented 2 years ago

I just realize I told complete crap, ivy-rich does appear in the first profiler report, but not in the second.