From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: Exposing buffer text modifications to Lisp Date: Sat, 18 Jun 2022 11:47:57 +0300 Message-ID: <83fsk2nyrm.fsf@gnu.org> References: <2c2746e5f2558a87e8eab6f0914264a020173a9d.camel@pm.me> <27630AA3-8026-4E24-8852-ACCD9325B99D@gmail.com> <0E9E702B-B07C-4794-8498-29B9320E14CC@gmail.com> <871qvorqvv.fsf@localhost> <83tu8jq2vl.fsf@gnu.org> <87sfo37etn.fsf@localhost> <834k0jplcm.fsf@gnu.org> <878rpuwm9w.fsf@localhost> <83mteao3oj.fsf@gnu.org> <87edzmv3i0.fsf@localhost> <83k09eo1p5.fsf@gnu.org> <878rpuv17q.fsf@localhost> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1315"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.com, emacs-devel@gnu.org To: Ihor Radchenko Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jun 18 10:53:22 2022 Return-path: Envelope-to: ged-emacs-devel@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 1o2UCo-0000BP-6G for ged-emacs-devel@m.gmane-mx.org; Sat, 18 Jun 2022 10:53:22 +0200 Original-Received: from localhost ([::1]:52360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2UCm-0001SX-Dh for ged-emacs-devel@m.gmane-mx.org; Sat, 18 Jun 2022 04:53:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40222) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2U7g-00007V-8u for emacs-devel@gnu.org; Sat, 18 Jun 2022 04:48:04 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:60444) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2U7f-0007Xk-L9; Sat, 18 Jun 2022 04:48:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=htx3ZMS42K4IZITSpRRnJH6DCCr/iZB69pc89z0zc9o=; b=WWASJjTghpcb 7AarF8tZjDtLc1t1nyrmxOCeYDDsL8iVFWxjGlLYuxsjwyu6GXk+crU9ztP0EBGj36RG8H+6lkp6R 4htF8ympNUUXpu8UclbOdlefkVS3ePk+sk9drYm22Z2ZQYeLsJ26p1hHfhyMtKQU6UaC3+NV2DvM/ fZY/zYg12mViqkCRtjeHMrEnpZfKpgc8hNqW+44BLbQEibBOFYKrN6L80lRpjmeRlK2W9qJjLQ0zD xrbmZ0iRMk3xXsAkxBRsdAVu6lknVECEp6MnV0+Be6eSs0PIq2jqv0dJTWbjA9UQ/jifqPiCzNu1i 5V/Ir8xp6pB7+o9D/IxA8A==; Original-Received: from [87.69.77.57] (port=1224 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2U7f-0005NM-4L; Sat, 18 Jun 2022 04:48:03 -0400 In-Reply-To: <878rpuv17q.fsf@localhost> (message from Ihor Radchenko on Sat, 18 Jun 2022 16:13:13 +0800) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:291349 Archived-At: > From: Ihor Radchenko > Cc: casouri@gmail.com, emacs-devel@gnu.org > Date: Sat, 18 Jun 2022 16:13:13 +0800 > > > AFAIU, the right fix for this is to fix performance degradation when a > > buffer has many markers, not avoiding the use of markers. > > > > Here's one conclusion from this discussion that indicates changes > > required to be done in core (other than a low-level modification hook > > for buffer text) to take care of your AST implementation. > > > > We already have a TODO item for making markers more efficient; any > > takers? > > This is trickier than it may appear. > Each element in Org AST has 3-7 markers. > My real-life large org buffer contains ~200k Org syntax elements > (actually more, but not all the elements are ever queried). > So, we are talking about 600k-1.4M markers in buffer if Org AST were to > use markers. > > Now, imagine an edit somewhere near the beginning of Org buffer. Such > edit means that Emacs will have to shift positions of nearly all the > markers in the buffer. All the >1M markers. On every > self-insert-command. The inner loop of adjust_markers_for_insert is just 40 machine instructions. (This is in unoptimized code; it could be fewer instruction in an optimized build.) Assuming a 3GHz CPU clock, 40 instructions should take just 13 nsec, and 1 million of these should take 13 milliseconds -- a very short time indeed. I expect that to be between 5 and 7 msec in an optimized build. (Compare that with inserting the characters itself: the first insertion could potentially mean moving the gap, which in a large buffer means moving megabytes of bytes -- not a negligible feat.) So I don't think the performance degradation due to markers is because the insert/delete operations on buffer text need to update many markers. I think the real slowdown comes from the functions which convert character positions to byte positions and vice versa: these use markers. There are a lot of such calls all over our code, and that's where the current linear-linked-list implementation of markers slows us down. Of course, the right method to show the bottleneck(s) is to profile the code with a tool like 'prof', and take it from there. So here's one more interesting job for someone to volunteer. > Org parser goes around this issue by updating AST positions on idle and > maintaining asynchronous request queue. This works relatively well > because AST queries are skewed to be near the buffer region being > edited. I am not sure if similar approach (not trivial to start with) > can be efficiently utilized by Emacs. IDK the typical marker access > pattern in Emacs core. If you already have a workaround for marker-related problems, then why do you need to hook into insertion and deletion on the lowest level? > >> The situation is third-party code doing bloody murder with > >> > >> (with-silent-modifications > >> (insert "Some text not triggering modification hooks)) > >> > >> Another scenario is modifying text in indirect buffers created with > >> make-indirect-buffer. (where there is no chance to install > >> before/after-change-functions via clone-indirect-buffer-hook). > > > > In at least the latter case the idea for a proper solution was > > outlined by Stefan. > > I haven't read through his email carefully yet. A quick response is that > I have seen a lot of code in the wild that simply uses > make-indirect-buffer. Expecting compliance is unreliable in practice. (I > may need to think more about this though) If this is a frequent problem, perhaps we should introduce a special variant of add-hook which would cater to the indirect-buffer case. Discussion of several such cases could point to that conclusion, or it can point to something else. And that is my long-standing gripe aimed at developers of 3rd party packages: they should come here (or bug-gnu-emacs@gnu.org) and present the cases where they needed some missing infrastructure, instead of trying to jump through hoops to work around what they perceive as Emacs restrictions that (they think) cannot be possibly lifted. Doing the former will have at least two benefits: (a) it will facilitate Emacs development into a better platform, and (b) it will avoid giving birth to some of the horrible kludges out there, which eventually don't work well enough, and thus make Emacs seem less professional than it should be. And if that is my expectation from developers of 3rd party packages, I definitely expect that from packages that are bundled, such as Org. Since Org is basically part of the core Emacs, it makes little sense to me to realize that it goes to such lengths trying to work around the limitations, instead of asking the core team to improve the existing implementation or add some missing ones. I could perhaps understand if the request existed, but no one volunteered to work on it, but not having the requests in the first place I cannot understand.