From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#58558: 29.0.50; re-search-forward is slow in some buffers Date: Tue, 13 Dec 2022 12:38:13 -0500 Message-ID: References: <877d10r21x.fsf@localhost> <87zgdwyvkt.fsf@gnus.org> <87ilkk6ri5.fsf@localhost> <87v8okjei9.fsf@gnus.org> <87tu44jdce.fsf@localhost> <87czasjd9j.fsf@gnus.org> <87k050nio5.fsf@localhost> <87zgdwhw0z.fsf@gnus.org> <83sfjo3tfw.fsf@gnu.org> <878rlfjmjh.fsf@localhost> <87mt9tbbbp.fsf@gnus.org> <8335bl18lo.fsf@gnu.org> <87wn8x9eqb.fsf@gnus.org> <87tu1zd2c6.fsf@localhost> <83h6xzphxm.fsf@gnu.org> <87wn6vbfaa.fsf@localhost> <838rjbpecw.fsf@gnu.org> <87fsdjwb4e.fsf@localhost> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33610"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 58558@debbugs.gnu.org, Eli Zaretskii , larsi@gnus.org To: Ihor Radchenko Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Dec 13 18:39:23 2022 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 1p59FS-0008Th-0P for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 13 Dec 2022 18:39:22 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p59FB-00067k-Qa; Tue, 13 Dec 2022 12:39:05 -0500 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 1p59F8-00067R-Ta for bug-gnu-emacs@gnu.org; Tue, 13 Dec 2022 12:39:02 -0500 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 1p59F8-0000tD-M9 for bug-gnu-emacs@gnu.org; Tue, 13 Dec 2022 12:39:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p59F8-0006nb-Hl for bug-gnu-emacs@gnu.org; Tue, 13 Dec 2022 12:39:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 13 Dec 2022 17:39:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58558 X-GNU-PR-Package: emacs Original-Received: via spool by 58558-submit@debbugs.gnu.org id=B58558.167095310526122 (code B ref 58558); Tue, 13 Dec 2022 17:39:02 +0000 Original-Received: (at 58558) by debbugs.gnu.org; 13 Dec 2022 17:38:25 +0000 Original-Received: from localhost ([127.0.0.1]:33555 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p59EX-0006nG-AX for submit@debbugs.gnu.org; Tue, 13 Dec 2022 12:38:25 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:36670) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p59EU-0006nA-RX for 58558@debbugs.gnu.org; Tue, 13 Dec 2022 12:38:23 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 7376280853; Tue, 13 Dec 2022 12:38:17 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id B28C880323; Tue, 13 Dec 2022 12:38:15 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1670953095; bh=LuSLwFWlH4cw8QxkNfWqybRWu39xFWELx8oMzqaOawg=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=lK1gNCB5/Uv+oknPhQ3AKbyVW7p1hUQoAu4qOLdWE+RFjwbU20RcnB7TQ2F8Ir98W 91Qs9O+VZ1IR2oA2QZa+urWda2jVRgYiKR9po2uuG6PqcF3Rk6jRWmiLJnQQ3frOm9 iPZwghq/za+BKSqBjup5F0KSVP8zTjMhDBDOXGnuNkrh+0aEuzD/ioZLhLECL9LWJP ATbc1eXNBv0yciiPE9qzlJ4zpHcuDFkXQW3GP+3lWPxzUfuuWxfYi3897LHf3yyZnH NLTOg03Jdgda/xwYqJfjEAXCi4vr38lgnKGqVOxrkLroABAEobFZSUMun7A0vCJF/L OpPebpzW62ekA== Original-Received: from pastel (unknown [45.72.193.52]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 71B641201E4; Tue, 13 Dec 2022 12:38:15 -0500 (EST) In-Reply-To: <87fsdjwb4e.fsf@localhost> (Ihor Radchenko's message of "Tue, 13 Dec 2022 15:56:33 +0000") 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:250865 Archived-At: >>> I will look how to do it. Maybe perf probe. >>> I guess, it will be useful to compile Emacs with debug symbols at this >>> point. >> >> AFAIR, you can ask perf to profile a single function, and you can ask >> it to annotate the profile with the source code. > > I now compiled Emacs with debug symbols, waited enough to see observable > increase in the benchmark-run timing, and recorded the perf data. > > buf_bytepos_to_charpos is still on the top > > 78.06% emacs emacs [.] buf_bytepos_to_charpos > 3.00% emacs emacs [.] re_match_2_internal > 1.05% emacs emacs [.] find_interval > 1.04% emacs emacs [.] CHAR_TABLE_REF_ASCII > 0.85% emacs emacs [.] make_lisp_symbol > 0.80% emacs emacs [.] re_search_2 > 0.76% emacs emacs [.] builtin_lisp_symbol > 0.62% emacs emacs [.] PSEUDOVECTORP AFAIK the main places where we call `buf_bytepos_to_charpos` from `re_match_2_internal` is via the `SYNTAX_TABLE_BYTE_TO_CHAR` macro, used for regexp elements that depend on syntax tables (i.e. \<, \>, \_<, ...). But I'd expect those to be executed "frequently&closely" enough that the `cached_(byte|char)pos` data should almost always be nearby, making the call to `buf_bytepos_to_charpos` fairly cheap (more specifically the `for (tail = BUF_MARKERS (b);...` loop should not iterate many times, regardless how many markers there are). > My guess: number of markers is growing somehow? `buf_bytepos_to_charpos` itself creates markers (using them as a cache of previous conversions), so that might be why. But we only look at the first N markers where N*50 is the distance to the closest marker found so far. So growth is not sufficient (it's clearly a part of the reason, tho). Regarding growth: could you call `garbage-collect` between the calls to `re-search-forward` to see if that avoids the accumulation? [ I presume here that those markers are created/added by `buf_bytepos_to_charpos` itself, so they should be recovered by the GC because they're not referenced from anywhere else. ] I'd be interested to know how many iterations of the `for (tail = BUF_MARKERS (b);...` loop are executed on average during your `re-search-forward` (and how that average changes between runs of `re-search-forward`). Stefan PS: Of course, another approach would be to replace this code with something else. Using markers as a cache of bytepos/charpos conversions has been a source of a few performance issues over the year. Another approach could be to use a "vector with gap" managed alongside the actual buffer text. It could be indexed by "charpos divided by 1024", so conversion from charpos to bytepos could be a simple vector lookup followed by scanning at most 1kB, and conversion in the other direction would use a binary search in that same vector (or we could use 2 "vectors with gap", one per direction of conversion).