Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlays make emacs really slow

I use hide-show to collapse certain parts of my text and I use the code below to show the number of hidden lines.

However, when a file is large enough (e.g. C++ or LaTeX) and I collapse all regions (thus creating tens of overlays), Emacs becomes really slow to the point of being unusable. Even moving the marker from one line to another takes half a second or so.

Is there a way of resolving this?

(defun display-code-line-counts (ov)
    (overlay-put ov 'display
                 (format "...%d..."
                         (count-lines (overlay-start ov)
                                      (overlay-end ov))
                         ))
    (overlay-put ov 'face '(:foreground "red" :box (:line-width 1 :style none)))
  )

(setq hs-set-up-overlay 'display-code-line-counts)

EDIT: Turns out the reason emacs becomes very slow is because of the linum minor mode which creates thousands of (hidden) overlays that are collapsed with hide-show. Is there a way to fix this? Or a better line number mode?

like image 259
Tohiko Avatar asked Oct 17 '22 17:10

Tohiko


1 Answers

As you said in your title, overlays can make Emacs slow.

One thing you can do, which can help sometimes, is to recenter the set of overlays around the current curson position (point). You do that using function overlay-recenter.

As the Elisp manual, node Managing Overlays tells you:

This function recenters the overlays of the current buffer around position POS. That makes overlay lookup faster for positions near POS, but slower for positions far away from POS.

A loop that scans the buffer forwards, creating overlays, can run faster if you do (overlay-recenter (point-max)) first.

like image 97
Drew Avatar answered Oct 21 '22 05:10

Drew