From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Ihor Radchenko <yantar92@posteo.net>
Newsgroups: gmane.emacs.bugs
Subject: bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in
 search.c)
Date: Tue, 09 May 2023 12:02:19 +0000
Message-ID: <871qjp1zn8.fsf@localhost>
References: <63882A45-BD02-40D5-92FA-70175267BA3B@acm.org>
 <874jou7lsf.fsf@localhost>
 <37EED5F9-F1FE-46B6-B4FA-0B268B945123@gmail.com>
 <87wn1qqvj0.fsf@localhost>
 <34F4849A-CB39-4C96-9CC1-11ED723706DA@gmail.com>
 <87wn1psqny.fsf@localhost>
 <6DAF37F9-B236-4C33-8E30-0FCA47CCBCC5@gmail.com>
 <87zg6lfobh.fsf@localhost>
 <281B22C2-CD69-4495-A97C-E754446CA9A6@gmail.com>
 <87o7n1v1w3.fsf@localhost>
 <878E8D66-A548-42E6-B077-6068A8B131D8@gmail.com>
 <87ednvul22.fsf@localhost>
 <EFFDF31B-2B58-44E8-9B05-6039A98331D3@gmail.com>
 <87y1m1oa01.fsf@localhost>
 <74CD5EF4-5424-40BA-8F80-D0FD89CB890F@gmail.com>
 <87zg6fjar6.fsf@localhost>
 <C037659A-DBF7-4071-A54F-84CC1D7D18AF@gmail.com>
 <875y923964.fsf@localhost>
 <67AAB661-8B27-4D09-BF0D-6B76ABB54477@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="29493"; mail-complaints-to="usenet@ciao.gmane.io"
Cc: 63225@debbugs.gnu.org
To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= <mattias.engdegard@gmail.com>
Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue May 09 14:00:19 2023
Return-path: <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org>
Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org>)
	id 1pwM0x-0007X9-Ja
	for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 09 May 2023 14:00:19 +0200
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <bug-gnu-emacs-bounces@gnu.org>)
	id 1pwM0j-00089T-IV; Tue, 09 May 2023 08:00:06 -0400
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1pwM0g-00088l-TT
 for bug-gnu-emacs@gnu.org; Tue, 09 May 2023 08:00:03 -0400
Original-Received: from debbugs.gnu.org ([209.51.188.43])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1pwM0g-0005H5-H9
 for bug-gnu-emacs@gnu.org; Tue, 09 May 2023 08:00:02 -0400
Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1pwM0g-0006KN-C0
 for bug-gnu-emacs@gnu.org; Tue, 09 May 2023 08:00:02 -0400
X-Loop: help-debbugs@gnu.org
Resent-From: Ihor Radchenko <yantar92@posteo.net>
Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org>
Resent-CC: bug-gnu-emacs@gnu.org
Resent-Date: Tue, 09 May 2023 12:00:02 +0000
Resent-Message-ID: <handler.63225.B63225.168363355124228@debbugs.gnu.org>
Resent-Sender: help-debbugs@gnu.org
X-GNU-PR-Message: followup 63225
X-GNU-PR-Package: emacs
X-GNU-PR-Keywords: patch
Original-Received: via spool by 63225-submit@debbugs.gnu.org id=B63225.168363355124228
 (code B ref 63225); Tue, 09 May 2023 12:00:02 +0000
Original-Received: (at 63225) by debbugs.gnu.org; 9 May 2023 11:59:11 +0000
Original-Received: from localhost ([127.0.0.1]:42579 helo=debbugs.gnu.org)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <debbugs-submit-bounces@debbugs.gnu.org>)
 id 1pwLzq-0006Ih-IS
 for submit@debbugs.gnu.org; Tue, 09 May 2023 07:59:10 -0400
Original-Received: from mout01.posteo.de ([185.67.36.65]:50779)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <yantar92@posteo.net>) id 1pwLzo-0006IR-Mz
 for 63225@debbugs.gnu.org; Tue, 09 May 2023 07:59:09 -0400
Original-Received: from submission (posteo.de [185.67.36.169]) 
 by mout01.posteo.de (Postfix) with ESMTPS id 47882240069
 for <63225@debbugs.gnu.org>; Tue,  9 May 2023 13:59:02 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017;
 t=1683633542; bh=zh6PCkTSu/aGD4L6cgWpl4wuILXotDa4cV4/sddJiHM=;
 h=From:To:Cc:Subject:Date:From;
 b=VeEzdmoAyw4uMLUWCOOrWDRgWa1UfifO9n/fxZNzrBSHko50/zM07Govsak7seGzJ
 JSEIJ3mMaxF04xYXe78Bj6EafBUHEa1im2WR97FIdsCs1o6aWj8MP8NB7MoCrAb1kw
 rbCxELA617meQHdIDGV7Tsv3cmC+9ZhMFdmpJ+h6rsmYfffen0okzm7emh/TmOXMIa
 DOyveCuRU5gIY82ohdx4n0sqBhzeSToBQKkAARsFkFXpmT58Dcd25GAeZ9h15XW4YX
 Sa3jj8ffa2oodzsSfBRFjC3mvCnG8winFaR3cF2As4DDON2LTpIhHOwNR5ztXpDGCx
 LTwzf9vSXnd9A==
Original-Received: from customer (localhost [127.0.0.1])
 by submission (posteo.de) with ESMTPSA id 4QFxX557Q3z6tvr;
 Tue,  9 May 2023 13:59:01 +0200 (CEST)
In-Reply-To: <67AAB661-8B27-4D09-BF0D-6B76ABB54477@gmail.com>
X-BeenThere: debbugs-submit@debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
X-BeenThere: bug-gnu-emacs@gnu.org
List-Id: "Bug reports for GNU Emacs,
 the Swiss army knife of text editors" <bug-gnu-emacs.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/bug-gnu-emacs>
List-Post: <mailto:bug-gnu-emacs@gnu.org>
List-Help: <mailto:bug-gnu-emacs-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=subscribe>
Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org
Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.emacs.bugs:261405
Archived-At: <http://permalink.gmane.org/gmane.emacs.bugs/261405>

Mattias Engdeg=C3=A5rd <mattias.engdegard@gmail.com> writes:

> 8 maj 2023 kl. 21.38 skrev Ihor Radchenko <yantar92@posteo.net>:
>
>> Does this imply that simple regexps like
>>=20
>> (looking-at-p (rx (seq bol (zero-or-more (any "\t ")) "#" (or " " eol))))
>>=20
>> should better be implemented as the following?
>
> Obviously this isn't practical or beneficial for any but the simplest pat=
terns. 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 m=
oving 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.
>>=20
>> 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?

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