zk-phi / magic-latex-buffer

Magical syntax highlighting for LaTeX-mode buffers
152 stars 13 forks source link

Slowdown even when magic-latex-enable-block-align is nil #25

Closed antoine-levitt closed 7 years ago

antoine-levitt commented 7 years ago

Hi,

This is a profile trace of scrolling a whole latex buffer with (setq magic-latex-enable-block-align nil) (setq magic-latex-enable-block-highlight nil)

- command-execute                                                3630  89%
 - call-interactively                                            3630  89%
  - funcall-interactively                                        3630  89%
   - scroll-up-command                                           3118  76%
    - scroll-up                                                  3053  75%
     - jit-lock-function                                         3042  74%
      - jit-lock-fontify-now                                     3042  74%
       - apply                                                   3042  74%
        - ad-Advice-jit-lock-fontify-now                         3042  74%
         - #<compiled 0x26a485>                                  3042  74%
          - jit-lock--run-functions                              3038  74%
           - run-hook-wrapped                                    3038  74%
            - #<compiled 0x1334d27>                              3038  74%
             + ml/jit-prettifier                                 1201  29%
             + ml/jit-block-highlighter                           921  22%
             + font-lock-fontify-region                           460  11%
             + ml/jit-block-aligner                               436  10%

As you can see, a sizable portion of the time is spent in ml/jit-block-highlighter and ml/jit-block-aligner, despite the fact that these functions do nothing. This is because of (condition-case nil (progn (ml/skip-blocks 1 nil t) (point)) (error (goto-char 1)))

Wrapping the two functions in (when)s works for me:

diff --git a/magic-latex-buffer.el b/magic-latex-buffer.el
index c6a1b69..9daa3bd 100644
--- a/magic-latex-buffer.el
+++ b/magic-latex-buffer.el
@@ -547,11 +547,11 @@ BEG END."
       (delete-overlay ov))))

 (defun ml/jit-block-highlighter (beg end)
-  (condition-case nil
-      (progn (ml/skip-blocks 1 nil t) (point))
-    (error (goto-char 1)))
-  (ml/remove-block-overlays (point) end)
   (when magic-latex-enable-block-highlight
+    (condition-case nil
+        (progn (ml/skip-blocks 1 nil t) (point))
+      (error (goto-char 1)))
+    (ml/remove-block-overlays (point) end)
     (dolist (command ml/block-commands)
       (save-excursion
         (while (funcall (car command) end)
@@ -635,11 +635,11 @@ between BEG and END."
       (delete-overlay ov))))

 (defun ml/jit-block-aligner (beg end)
-  (condition-case nil
-      (progn (ml/skip-blocks 1 nil t) (point))
-    (error (goto-char 1)))
-  (ml/remove-align-overlays (point) end)
   (when magic-latex-enable-block-align
+    (condition-case nil
+        (progn (ml/skip-blocks 1 nil t) (point))
+      (error (goto-char 1)))
+    (ml/remove-align-overlays (point) end)
     (dolist (command ml/align-commands)
       (save-excursion
         (while (funcall (car command) end)
zk-phi commented 7 years ago

This may be intended to clear block highlights, when block-highlight is disabled after some blocks got highlighted. But it seems a rare-case and we should better do nothing when block-highlight is disabled, as you pointed out. Thanks!

zk-phi commented 7 years ago

Fixed :)