Subject: bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in search.c)

From: Mattias Engdegård <[email-masked]>
Date: Tue, 2 May 2023 16:33:58 +0200

> I was able to get rid of the regex compilation-related slowdown simply
> by increasing REGEXP_CACHE_SIZE 10x (see the attached patch).

Indeed it sounds like you are suffering from regexp cache thrashing. I'm attaching two patches: one to measure the cache miss rate, and one that allows the regexp cache size to be changed at run time.

That should let you find the working set size for your application, and ideally come up with a way to reduce it. Perhaps you could give us an idea of what these regexps look like and how they are used?

> Does anyone know if there are potential side effects of this increase if
> applied across Emacs? Or, alternatively, may Emacs provide an ability to
> store compiled regexp patterns from Elisp (similar to what
> `treesit-query-compile' does)?

I don't think it's necessarily a good idea to increase the size to 200 right away because of the linear cache lookup mechanism. Allowing the size to be changed at run time is probably less controversial (but arguably just as much of a crutch).

Introducing regexp objects that could store compiled regexps and be used instead of strings would be quite some work but probably worthwhile.