From: Ihor Radchenko <yantar92@posteo.net>
To: "Mattias Engdegård" <mattiase@acm.org>
Cc: 63225@debbugs.gnu.org
Subject: bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in search.c)
Date: Tue, 02 May 2023 16:14:40 +0000 [thread overview]
Message-ID: <874jou7lsf.fsf@localhost> (raw)
In-Reply-To: <63882A45-BD02-40D5-92FA-70175267BA3B@acm.org>
Mattias Engdegård <mattiase@acm.org> writes:
>> 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.
Here are the results:
Command: (benchmark-progn
(setq regexp-cache-hit 0
regexp-cache-miss 0)
(set-regexp-cache-size 42)
(org-element-parse-buffer)
nil)
Buffer size: 22Mb
| 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) |
| 50 | 4693497 | 44174 | 0.93 | 17.896177s (1.082944s in 2 GCs) |
| 60 | 4734712 | 10807 | 0.23 | 18.011224s (1.097961s in 2 GCs) |
| 80 | 4710155 | 1386 | 0.03 | 18.047544s (1.103518s in 2 GCs) |
| 100 | 4711821 | 399 | 0.01 | 17.880491s (1.102658s in 2 GCs) |
| 160 | 4711895 | 160 | 0.00 | 17.950772s (1.068975s in 2 GCs) |
| 320 | 4737968 | 393 | 0.01 | 17.773617s (1.089100s in 2 GCs) |
| 640 | 4737388 | 320 | 0.01 | 18.225701s (1.097688s in 2 GCs) |
| 1280 | 4711353 | 160 | 0.00 | 17.847522s (1.099575s in 2 GCs) |
| 2560 | 4711898 | 160 | 0.00 | 18.168488s (1.082394s in 2 GCs) |
| 5120 | 4711835 | 160 | 0.00 | 17.797036s (1.097445s in 2 GCs) |
#+TBLFM: $4=100*$3/($3+$2);%.2f
> 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?
The Org parser is basically a giant `cond' of a number of regexp
matches. See `org-element--current-element'. It is called repeatedly on
every syntax element in Org buffer (like heading, table, paragraph,
etc). Each clause in the `cond' additionally calls for more complex
series regexps to look into smaller components of the parsed syntax
elements. For example, see `org-element-keyword-parser'.
So, we are cycling across several dozens (more than regexp cache size)
of regexps repeatedly.
>> 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).
Fair point. Although overshooting within a single command does not
appear to do much as long as we really re-use these regexps - everything
gets cached.
> 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.
> +DEFUN ("set-regexp-cache-size", Fset_regexp_cache_size, Sset_regexp_cache_size,
> + 1, 1, 0,
> + doc: /* Set the regexp cache size to N elements. Internal use only. */)
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>).
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
next prev parent reply other threads:[~2023-05-02 16:14 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-02 7:37 bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in search.c) Ihor Radchenko
2023-05-02 14:33 ` Mattias Engdegård
2023-05-02 15:25 ` Eli Zaretskii
2023-05-02 15:28 ` Mattias Engdegård
2023-05-02 17:30 ` Eli Zaretskii
2023-05-02 17:58 ` Ihor Radchenko
2023-05-02 16:14 ` Ihor Radchenko [this message]
2023-05-02 21:00 ` Mattias Engdegård
2023-05-02 21:21 ` Ihor Radchenko
2023-05-03 8:39 ` Mattias Engdegård
2023-05-03 9:36 ` Ihor Radchenko
2023-05-03 13:59 ` Mattias Engdegård
2023-05-03 15:05 ` Ihor Radchenko
2023-05-03 15:20 ` Mattias Engdegård
2023-05-03 16:02 ` Ihor Radchenko
2023-05-04 9:24 ` Mattias Engdegård
2023-05-05 10:31 ` Ihor Radchenko
2023-05-05 16:26 ` Mattias Engdegård
2023-05-06 13:38 ` Ihor Radchenko
2023-05-07 10:32 ` Mattias Engdegård
2023-05-08 11:58 ` Ihor Radchenko
2023-05-08 18:21 ` Mattias Engdegård
2023-05-08 19:38 ` Ihor Radchenko
2023-05-08 19:53 ` Mattias Engdegård
2023-05-09 8:36 ` bug#63225: Using char table-based finite-state machines as a replacement for re-search-forward (was: bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in search.c)) Ihor Radchenko
2023-05-09 12:02 ` bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in search.c) Ihor Radchenko
2023-05-09 15:05 ` Mattias Engdegård
2023-05-09 15:56 ` Ihor Radchenko
2023-05-09 15:57 ` Mattias Engdegård
2023-05-07 12:45 ` Mattias Engdegård
2023-05-08 13:56 ` Ihor Radchenko
2023-05-08 19:32 ` Mattias Engdegård
2023-05-08 19:44 ` Ihor Radchenko
2023-05-04 12:58 ` Ihor Radchenko
2023-05-02 23:36 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874jou7lsf.fsf@localhost \
--to=yantar92@posteo.net \
--cc=63225@debbugs.gnu.org \
--cc=mattiase@acm.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).