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#63040: 30.0.50; Performance of buf_bytepos_to_charpos when a buffer has large number of markers Date: Sun, 23 Apr 2023 19:41:40 +0000 Message-ID: <878reiwfm3.fsf@localhost> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34764"; mail-complaints-to="usenet@ciao.gmane.io" To: 63040@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 23 21:40:15 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 1pqfZG-0008sP-FL for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 23 Apr 2023 21:40:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pqfZ6-0005xT-Nd; Sun, 23 Apr 2023 15:40:04 -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 1pqfZ4-0005vZ-OF for bug-gnu-emacs@gnu.org; Sun, 23 Apr 2023 15:40:02 -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 1pqfZ4-0001wk-Et for bug-gnu-emacs@gnu.org; Sun, 23 Apr 2023 15:40:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pqfZ3-0007fY-U7 for bug-gnu-emacs@gnu.org; Sun, 23 Apr 2023 15:40: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: Sun, 23 Apr 2023 19:40:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 63040 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.168227874729375 (code B ref -1); Sun, 23 Apr 2023 19:40:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 23 Apr 2023 19:39:07 +0000 Original-Received: from localhost ([127.0.0.1]:47009 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pqfYA-0007di-Vw for submit@debbugs.gnu.org; Sun, 23 Apr 2023 15:39:07 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:43514) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pqfY8-0007dZ-TI for submit@debbugs.gnu.org; Sun, 23 Apr 2023 15:39:05 -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 1pqfY8-0005du-Ir for bug-gnu-emacs@gnu.org; Sun, 23 Apr 2023 15:39:04 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pqfY4-0001lE-GG for bug-gnu-emacs@gnu.org; Sun, 23 Apr 2023 15:39:03 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id A4540240170 for ; Sun, 23 Apr 2023 21:38:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1682278737; bh=WDvJvoFOGMe+96eBGGhd7uvnO1J4m45uPwnG/vE/I0U=; h=From:To:Subject:Date:From; b=HbeythQTKRxu18ay9A6R+d5uUZ7V0PSL/Dn3Mv/C/C/J3nklrlKNt7JArjRp9VKed XP4WZwEkXrdn7R/gf+ZGk1XHNRA9TfLuP/3axKyFazSy3A880VPp7ZCNsJhqH6s3i+ OegGEBugvheoDmcIQQ+xF2PCvUkfNRHv9g7UCSKSKgciRWFKZ9lJLwI+laaboCOr7q s/cFmYns1C9vTzVWDD8HUzb2SPDqSLLixty5PF8h5EAzosJkRQcwOw6zE3bvxPpGAt Q099LGJz6ufPlp+tJ1z7xzD5btUgkllMg0y3yhfRbV8W8uVPqy+K2t5DUHmQQsvGN5 jJlUOoRDb0JJw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Q4JV139Lyz9rxH for ; Sun, 23 Apr 2023 21:38:49 +0200 (CEST) Received-SPF: pass client-ip=185.67.36.66; envelope-from=yantar92@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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:260538 Archived-At: --=-=-= Content-Type: text/plain Hi, When investigating `re-search-forward' performance in https://debbugs.gnu.org/cgi/bugreport.cgi?bug=58558 (bug#58558), I noticed that buf_bytepos_to_charpos is taking most of the CPU time, according to perf stats. This was partially caused by `parse-sexp-lookup-properties', but even after working around the text property issue, buf_bytepos_to_charpos still shows up on top of the perf profile. Since one of the apparent bottlenecks in buf_bytepos_to_charpos is for (tail = BUF_MARKERS (b); tail; tail = tail->next) which obviously scales with the number of markers in buffer, I decided to add a cut-off parameter, as in the attached patch (number 50 has no particular motivation underneath). Surprisingly, this simple change reduced my Org agenda generation times from 20 seconds down to 3-4 seconds! I am sure that my dumb approach is not the best way to improve the performance, but this place in buf_bytepos_to_charpos is clearly something that can be optimized. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-src-marker.c-buf_bytepos_to_charpos-Limit-marker-sea.patch >From a6ff6268bdc42a7dfedc6729d4232a2ae149da56 Mon Sep 17 00:00:00 2001 Message-Id: From: Ihor Radchenko Date: Sun, 23 Apr 2023 21:31:46 +0200 Subject: [PATCH] * src/marker.c (buf_bytepos_to_charpos): Limit marker search Limit searching across buffer markers to first 50 markers and thus avoid performance scaling with the number of markers. I got 5x `re-search-forward' speed improvement in my setup with this dumb change. --- src/marker.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/marker.c b/src/marker.c index e42c49a5434..008a76c49e6 100644 --- a/src/marker.c +++ b/src/marker.c @@ -348,8 +348,10 @@ buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos) if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff) CONSIDER (cached_bytepos, cached_charpos); - for (tail = BUF_MARKERS (b); tail; tail = tail->next) + int i = 0; + for (tail = BUF_MARKERS (b); tail && i < 50; tail = tail->next) { + i++; CONSIDER (tail->bytepos, tail->charpos); /* If we are down to a range of 50 chars, -- 2.40.0 --=-=-= Content-Type: text/plain In GNU Emacs 30.0.50 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.37, cairo version 1.17.8) of 2023-04-23 built on localhost Repository revision: ca875e3947e29d222554a05583068c49a56ed8ca Repository branch: master Windowing system distributor 'The X.Org Foundation', version 11.0.12101008 System Description: Gentoo Linux Configured using: 'configure --with-native-compilation' -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--