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

From: Ihor Radchenko <[email-masked]>
Date: Tue, 09 May 2023 12:02:19 +0000

Mattias Engdegård <[email-masked]> writes:

> 8 maj 2023 kl. 21.38 skrev Ihor Radchenko <[email-masked]>:
>
>> Does this imply that simple regexps like
>> 
>> (looking-at-p (rx (seq bol (zero-or-more (any "\t ")) "#" (or " " eol))))
>> 
>> should better be implemented as the following?
>
> Obviously this isn't practical or beneficial for any but the simplest patterns. Benchmark if you are concerned.
> It is useful to know when (not) to use regexps.

I did some benchmarking and your code does provide >2x improvement. Most
of it is coming from using `forward-line' instead of
`beginning-of-line'.

I benchmarked different variations:

(defun yant/test1 ()
  (save-excursion
    (forward-line 0)   ; faster than beginning-of-line
    (skip-chars-forward "[:blank:]") ; faster than looking-at-p
    (not (eolp))))   ; very cheap

(defun yant/test2 ()
  (save-excursion
    (beginning-of-line 0)
    (not (looking-at-p "[[:blank:]]*$"))))

(defun yant/test3 ()
  (save-excursion
    (beginning-of-line 0)
    (skip-chars-forward "[:blank:]") ; faster than looking-at-p
    (not (eolp))))

(defun yant/test4 ()
  (save-excursion
    (forward-line 0)   ; faster than beginning-of-line
    (not (looking-at-p "[[:blank:]]*$"))))

- forward-line + skip-chars-forward      :: (2.980 2 0.648)
- beginning-of-line + looking-at-p       :: (7.189 2 0.653)
- beginning-of-line + skip-chars-forward :: (6.833 2 0.634)
- forward-line + looking-at-p            :: (3.180 2 0.663)

>> I really hope that I did not need to do all these workarounds specific to
>> current implementation pitfalls of Emacs regexp compiler.
>
> Some of them. We program for the system we have. Emacs is a very slowly moving target.

Will it make sense to use a combination of char-after and
skip-chars-forward every single time?

I am thinking about something among the lines of

(defconst org-fancy-re
  (propertize
   (rx bol (1+ "*") " ")
   'org-re-direct
   '(progn
      (and
       (bolp)
       (> (skip-chars-forward "*") 0)
       (prog1 (eq ?\s (char-after)) (forward-char))))))

(define-inline org-fancy-looking-at-p (regexp)
  "Like `looking-at-p', but try `org-re-direct' property."
  (let ((sexp (and (inline-const-p regexp)
		   (get-text-property
		    'org-re-direct
		    (inline-const-val regexp)))))
    (if sexp (inline-quote (save-excursion ,sexp))
      (inline-quote (looking-at-p ,regexp)))))

>>> I would like that too, but changing that isn't easy.
>> 
>> I am sure that it is easy.
>
> I didn't mean technically. Code is easy to write.

May you elaborate what is the blocker then?

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