From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: gerd.moellmann@gmail.com, 58158@debbugs.gnu.org
Subject: bug#58158: 29.0.50; [overlay] Interval tree iteration considered harmful
Date: Thu, 29 Sep 2022 12:48:34 -0400 [thread overview]
Message-ID: <jwvill6rvm9.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <83o7uyfh5o.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 29 Sep 2022 16:23:47 +0300")
>> > I may be missing something, but it looks like the sole purpose of the
>> > iter_start/iter_finish dance is to ensure only one iteration per tree
>> > is running at any given time, and that's because the iteration uses
>> > some state variable(s) of which there's only one instance per tree.
>> >
>> > Stefan, am I missing something?
>>
>> One reason is that traversing a binary tree usually requires something
>> like recursion, but that wouldn't fit very conveniently with the current
>> code (nor with C in general since you can't make a local recursive
>> closure which accesses local variables from the surrounding function).
>
> I'm not sure I understand how recursion is related. Are you saying
> that recursion is replaced with iteration? But then, if _start and
> _finish are called by the same caller, we don't really need the
> protection, since no one can start another iteration while the first
> one runs. Right?
Typically, current code will look something like:
int x;
Lisp_Object y;
buffer_overlay_iter_start (current_buffer, prev, pos, ITREE_DESCENDING);
while ((node = buffer_overlay_iter_next (current_buffer)))
{
... do something that updates x and y ...
}
buffer_overlay_iter_finish (current_buffer);
If we were to use recursion, then we'd need to define a new (recursive)
function which does what's currently done in the `while` loop, but this
function can't access `x` and `y`, so it would need to take them as
argument, or a reference to them...
The use of an iterator is definitely convenient (and is a standard
approach in many languages).
>> Another is the need to update the begin/end fields (these need updating
>> because of insertions/deletions but they're updated lazily while
>> traversing the tree to avoid an O(N) complexity during the
>> insertions/deletions). Hiding that behind 'some kind of "next node"
>> function keeps the code more readable.
>
> Where in the code do you see iteration that adds or deletes nodes?
No, I meant insertion/deletion of text in the buffer, thus requiring
updates to `begin/end` fields.
> Btw, couldn't we handle this by having a flag in the node that tells
> us whether the begin/end fields can be trusted? Then the first caller
> who need them would run the update and reset the flags, and we still
> have lazy update, albeit somewhat less lazy, but without the need for
> guarding the iteration with start/finish calls. Would that work?
Yes, it would, but it's still O(N).
The current approach is inspired by the approach used in `intervals.c`
which also updates those fields lazily/ondemand so as to avoid the O(N)
performance impact.
>> For now, I pushed a simple fix to traverse the tree "by hand" in the GC
>> rather than via the iterator.
> So this removes the restriction of not having GC during iteration?
Yes.
> Also, I take it that you don't consider the current code is as
> "harmful" as Gerd thinks it is?
I don't like the global state it uses, but I think we can fix this
aspect without too much trouble.
> IOW, you don't share his opinion that this implementation is
> a "no-go"?
No, indeed, I don't.
Stefan
next prev parent reply other threads:[~2022-09-29 16:48 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-29 5:29 bug#58158: 29.0.50; [overlay] Interval tree iteration considered harmful Gerd Möllmann
2022-09-29 6:28 ` Eli Zaretskii
2022-09-29 7:03 ` Gerd Möllmann
2022-09-29 8:08 ` Eli Zaretskii
2022-09-29 9:09 ` Gerd Möllmann
2022-09-29 9:37 ` Eli Zaretskii
2022-09-29 10:05 ` Gerd Möllmann
2022-09-29 10:43 ` Eli Zaretskii
2022-09-29 11:33 ` Gerd Möllmann
2022-09-29 13:10 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-29 13:23 ` Eli Zaretskii
2022-09-29 16:48 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-09-29 13:40 ` Eli Zaretskii
2022-09-29 14:15 ` Gerd Möllmann
2022-09-29 14:37 ` Gerd Möllmann
2022-09-29 22:09 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-30 5:28 ` Gerd Möllmann
2022-09-30 6:11 ` Eli Zaretskii
2022-09-30 11:31 ` Gerd Möllmann
2022-09-30 18:29 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-02 8:06 ` Gerd Möllmann
2022-10-06 22:36 ` Dmitry Gutov
2022-10-07 19:47 ` Eli Zaretskii
2022-10-08 18:50 ` Dmitry Gutov
2022-10-10 8:10 ` Eli Zaretskii
2022-10-11 2:12 ` Dmitry Gutov
2022-10-11 6:37 ` Eli Zaretskii
2022-09-30 13:25 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-30 14:08 ` Gerd Möllmann
2022-09-30 15:25 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-30 16:04 ` Eli Zaretskii
2022-09-30 17:11 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-01 5:06 ` Gerd Möllmann
2022-10-01 13:54 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-02 8:22 ` Gerd Möllmann
2022-10-02 16:32 ` Andreas Politz
2022-10-03 4:35 ` Gerd Möllmann
2022-10-04 10:50 ` Andreas Politz
2022-10-01 7:25 ` Gerd Möllmann
2022-10-01 10:55 ` Gerd Möllmann
2022-10-01 14:01 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-09-29 16:40 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-01 1:57 ` Richard Stallman
2022-10-01 7:00 ` Eli Zaretskii
2022-10-06 22:26 ` Matt Armstrong
2023-10-06 13:14 ` Gerd Möllmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvill6rvm9.fsf-monnier+emacs@gnu.org \
--to=bug-gnu-emacs@gnu.org \
--cc=58158@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=gerd.moellmann@gmail.com \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).