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

From: Mattias Engdegård <[email-masked]>
Date: Tue, 2 May 2023 23:00:29 +0200

2 maj 2023 kl. 18.14 skrev Ihor Radchenko <[email-masked]>:

> |   Cache size |     Hit |    Miss | % miss from total | ~org-element-parse-buffer~ time   |
> |--------------+---------+---------+-------------------+---------------------------------|
> | 20 (default) | 3219470 | 1491165 |             31.66 | 21.035765s (1.091127s in 2 GCs) |
> |           40 | 4418377 |  293805 |              6.24 | 18.294018s (1.123854s in 2 GCs) |
> |           42 | 4550483 |  161820 |              3.43 | 17.946184s (1.073528s in 2 GCs) |
> |           45 | 4636222 |   76582 |              1.62 | 18.410150s (1.078844s in 2 GCs) |

Good, this quite solidly puts the working set size at 40-odd elements.

> The Org parser is basically a giant `cond' of a number of regexp
> matches. See `org-element--current-element'.

A common way to handle this is to build a big regexp to match many cases at the same time, essentially transforming

(cond ((looking-at RE1) ...)
      ((looking-at RE2) ...)
      ...)

to

    (looking-at (rx (or (group RE1) (group RE2) ...)))
    (cond ((match-beginning 1) ...)
          ((match-beginning 2) ...)
          ...)

This reduces the number of regexps used and is also typically faster.
(Essentially this is what `syntax-propertize-rules` does but in a more specialised context.)

Using tree-sitter for this could very well be even faster but it's not guaranteed to be available.

Otherwise it's very much a matter of optimisation of everything, including regexps. Minimise backtracking.
If you want to match five or more dashes, use "------*" instead of "-\\{5,\\}". And so on.
It's also obviously a good idea not to generate regexps dynamically each time if you can help it, and minimise consing in general.

>> Introducing regexp objects that could store compiled regexps and be
>> used instead of strings would be quite some work but probably
>> worthwhile.
> 
> What exactly needs to be done? Assuming that regexp objects are not
> going to be readable, for simplicity.

A proper design, for starters. For example, we probably want them to be usable in customised variables which calls for them to be readable.

> If this is something to be used in practice, it will be more convenient
> to provide a macro like (with-regexp-cache-size N <body>).

Maybe, we'll see if it's something we need to add.