From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.bugs Subject: bug#63225: Compiling regexp patterns (and REGEXP_CACHE_SIZE in search.c) Date: Tue, 02 May 2023 16:14:40 +0000 Message-ID: <874jou7lsf.fsf@localhost> References: <63882A45-BD02-40D5-92FA-70175267BA3B@acm.org> 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="37229"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 63225@debbugs.gnu.org To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue May 02 18:12:34 2023 Return-path: 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 ) id 1ptscD-0009Vx-9c for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 02 May 2023 18:12:33 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ptsbn-0004mz-94; Tue, 02 May 2023 12:12:07 -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 ) id 1ptsbi-0004lS-V9 for bug-gnu-emacs@gnu.org; Tue, 02 May 2023 12:12: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 ) id 1ptsbi-0002CQ-MR for bug-gnu-emacs@gnu.org; Tue, 02 May 2023 12:12:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ptsbh-0000mi-Ro for bug-gnu-emacs@gnu.org; Tue, 02 May 2023 12:12:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ihor Radchenko Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 02 May 2023 16:12:01 +0000 Resent-Message-ID: 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.16830439163005 (code B ref 63225); Tue, 02 May 2023 16:12:01 +0000 Original-Received: (at 63225) by debbugs.gnu.org; 2 May 2023 16:11:56 +0000 Original-Received: from localhost ([127.0.0.1]:44886 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ptsbb-0000mO-Fk for submit@debbugs.gnu.org; Tue, 02 May 2023 12:11:55 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:38751) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ptsbW-0000m6-3H for 63225@debbugs.gnu.org; Tue, 02 May 2023 12:11:53 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 51F862402ED for <63225@debbugs.gnu.org>; Tue, 2 May 2023 18:11:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1683043903; bh=dE01Hu82ZWOzemRW381LBYLMMAQcI9s11VQoQTf6Ng0=; h=From:To:Cc:Subject:Date:From; b=Kz407mE2QwNKTBwX34w/QXT5ZUupcgovybHlMNQAnkxVKua0e7SU9Vxp4moMaZhWn la5EIOibmAO0XyudhwGXbfVprGuZt+dQc/WsaQv5YEiMw3wg1NqFrhbx+G1WcvREOh 1ZPGl48jBOi1lekS7kqWR3q8zdG+ZaHJ0MSQaVBVLGzeyvVO6RIjwWWjnbqSK2Umhb mvnFWXHJ/i0cPW9tSG1+fpLOlzv47OWMPdwmqvXw7yElLe6zN+69DcPr5TLMinrTX8 xZJLZ9ZnUzlYU5Kt4ya7PUsxJ2LPiE8NGHmTVVSZpZxiapLoemycT1yAf+eH3JVNUt hweT8bJ5q4QNQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Q9lSp2wVPz9rxb; Tue, 2 May 2023 18:11:38 +0200 (CEST) In-Reply-To: <63882A45-BD02-40D5-92FA-70175267BA3B@acm.org> 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" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-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:260944 Archived-At: Mattias Engdeg=C3=A5rd 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 all= ows 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.0911= 27s in 2 GCs) | | 40 | 4418377 | 293805 | 6.24 | 18.294018s (1.1238= 54s in 2 GCs) | | 42 | 4550483 | 161820 | 3.43 | 17.946184s (1.0735= 28s in 2 GCs) | | 45 | 4636222 | 76582 | 1.62 | 18.410150s (1.0788= 44s in 2 GCs) | | 50 | 4693497 | 44174 | 0.93 | 17.896177s (1.0829= 44s in 2 GCs) | | 60 | 4734712 | 10807 | 0.23 | 18.011224s (1.0979= 61s in 2 GCs) | | 80 | 4710155 | 1386 | 0.03 | 18.047544s (1.1035= 18s in 2 GCs) | | 100 | 4711821 | 399 | 0.01 | 17.880491s (1.1026= 58s in 2 GCs) | | 160 | 4711895 | 160 | 0.00 | 17.950772s (1.0689= 75s in 2 GCs) | | 320 | 4737968 | 393 | 0.01 | 17.773617s (1.0891= 00s in 2 GCs) | | 640 | 4737388 | 320 | 0.01 | 18.225701s (1.0976= 88s in 2 GCs) | | 1280 | 4711353 | 160 | 0.00 | 17.847522s (1.0995= 75s in 2 GCs) | | 2560 | 4711898 | 160 | 0.00 | 18.168488s (1.0823= 94s in 2 GCs) | | 5120 | 4711835 | 160 | 0.00 | 17.797036s (1.0974= 45s in 2 GCs) | #+TBLFM: $4=3D100*$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_cach= e_size, > + 1, 1, 0, > + doc: /* Set the regexp cache size to N elements. Internal use on= ly. */) If this is something to be used in practice, it will be more convenient to provide a macro like (with-regexp-cache-size N ). --=20 Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at