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

From: Ihor Radchenko <[email-masked]>
Date: Tue, 02 May 2023 07:37:39 +0000

Tags: patch

Hello,

I am now studying the performance of Org mode parser on huge Org files.

I noticed that `org-element-parse-buffer' spends a significant (~10%)
fraction of CPU time simply compiling regexp patterns.

This happens because Org parser performs a huge number repeated regexp
searches as it incrementally parses the buffer. The searches happen on a
fixed set of regexp patterns (several dozens).

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

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 suspect that storing pre-compiled patterns may benefit a number of
major modes that have to perform complex regexp matching.

Best,
Ihor

In GNU Emacs 30.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version
 3.24.37, cairo version 1.17.8) of 2023-05-02 built on localhost
Repository revision: a0a71ca12d585bca5173775f08eabae553e15659
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101008
System Description: Gentoo Linux

Configured using:
 'configure --with-native-compilation'

>From f0d9c814ff0601fa3487ad3da1ae4dcdf511d850 Mon Sep 17 00:00:00 2001
Message-Id: <[email-masked]>
From: Ihor Radchenko <[email-masked]>
Date: Tue, 2 May 2023 09:28:21 +0200
Subject: [PATCH] * src/search.c (REGEXP_CACHE_SIZE): Increase to 200

---
 src/search.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/search.c b/src/search.c
index 0bb52c03eef..cfcce3c7293 100644
--- a/src/search.c
+++ b/src/search.c
@@ -34,7 +34,7 @@ Copyright (C) 1985-1987, 1993-1994, 1997-1999, 2001-2023 Free Software
 
 #include "regex-emacs.h"
 
-#define REGEXP_CACHE_SIZE 20
+#define REGEXP_CACHE_SIZE 200
 
 /* If the regexp is non-nil, then the buffer contains the compiled form
    of that regexp, suitable for searching.  */
-- 
2.40.0

-- 
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>