Fixing Disproportionately Slow grepping in Emacs
For years, I’ve noticed that
grep-like commands in Emacs, including the far superior
would become unusable when the search results included particularly long lines (on the order of
thousands of characters, I believe, but I’ve never measured it). The results would begin
displaying as they came in, but Emacs would slow down within a second or two and then lock up. I
would have to either kill the grep process and wait for a few minutes to kill the buffer or else
restart Emacs entirely. I kept running into this problem at the most inopportune moments, when I was
thinking about something else and absent-mindedly hit the ripgrep shortcut.
Fed up with having my work interrupted and not being able to use basic search commands in certain repositories, I delved into the details a couple of weeks ago. The same commands in the same repositories exhibited normal behaviour (a touch of delay with long lines, but no locking up) when I ran emacs -Q and installed only the relevant packages, so my configuration had to be responsible. I spent an hour haphazardly disabling modes and refreshing the results buffer to see whether something I enabled was responsible, but I couldn’t identify the culprit.
I changed tack, incrementally adding bits of my configuration to emacs -Q, and eventually found
the root cause: I was setting
t so Emacs would scroll the output
as results came in. I
don’t remember adding it and in fact dislike the behaviour, so I was more than happy to remove it.
The problem immediately vanished. Reading the docstring carefully afterwards led me to set the
first-error instead, which is more desirable behaviour.
Apart from that, since I use ripgrep, I added a .rgignore file to one particular repository where this occurred time and again, and I added .rgignore to my local Git ignore file to maintain proper hygiene. That helped clean up the results so I wouldn’t encounter the problematic scenario in the first place.
- This tends to happen with source repositories that accumulate artefacts such as source maps and minified files—inadvisable but sadly common.↩