Subject: bug#18696: Emacs freezes for a while visitin Conf (.ini)) buffers

From: Lars Ingebrigtsen <[email-masked]>
Date: Thu, 19 Aug 2021 16:55:12 +0200

Stefan Monnier <[email-masked]> writes:

> Actually IIUC which-function-mode shouldn't cause such a slow down.
> If it does, it's probably because the major mode doesn't have special
> support for which-function-mode, so it falls back on using imenu's
> support, i.e. it needs to scan the whole buffer.
>
> This said, scanning the whole buffer shouldn't be that sow, unless you
> really have very many sections.  So it might also simply be a bug in the
> way the major mode supports imenu (e.g. the regexp used is inefficient).

I can reproduce this problem in Emacs 28:

curl https://mirrors.kernel.org/sourceware/cygwin/x86_64/setup.ini > /tmp/setup.ini
./src/emacs -f which-function-mode -Q /tmp/setup.ini
y

Emacs hangs completely, and even `C-g' doesn't help.

The major mode here is conf-colon-mode, which does:

  (setq-local imenu-generic-expression
	      `(("Parameters" "^[ \t]*\\(.+?\\)[ \t]*:" 1)
	        ,@(cdr imenu-generic-expression))))

And, indeed, removing that make the file open in a couple of seconds
instead of taking forever (where "forever" is defined as "longer than I
had patience to wait").

Hm...  That regexp does have some backtracking in it, but this is
an 18MB file where 99.997% of the lines match the regexp:

sdesc: "Debug info for 2048-cli"
ldesc: "This package contains files necessary for debugging the
2048-cli package with gdb."
category: Debug
requires: cygwin-debuginfo
version: 0.9.1-1

etc.

So no matter how efficient the regexp is, it's gonna hang here.

Perhaps `imenu-default-create-index-function' should just give up after
spending more than x seconds?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no