Subject: bug#71644: 30.0.50; Severe slowdown in larger files with markers beginning in emacs 29+

From: Ihor Radchenko <[email-masked]>
Date: Sat, 22 Jun 2024 14:10:23 +0000

Stefan Monnier <[email-masked]> writes:

>> I got 5x `re-search-forward' speed improvement in my setup with this
>> dumb change.
>
> Hmm... of course, there'd likely be other circumstances where it would
> make it 5x slower.  E.g. in a large buffer, doing a forward search from
> the middle of the buffer when the first 50 markers happen to all be near
> the beginning of the buffer will mean that we always use the "slow path"
> which scans all the bytes between PT and the position of interest to
> count the number of chars therein.

Yup. I _do not_ propose my patch to be merged.
(BTW, we should probably merge this bug and bug#63040 where I first
shared this patch - just to demonstrate the problem and discuss possible
solutions)

> BTW, we already stop after at most N/50 markers where N is the smallest
> distance between the position of interest and point/bob/eob/gap (oh and
> the position of the last conversion).  So it seems that in your test,
> this distance N is >2500.  Also when that distance is >5000 we create
> a new marker, so next time around that position should be at the
> beginning of the marker-list.  So I wonder what happens in your test:
> why do we jump "very far" (more than 2500) between each call to the
> conversion function?  Also, maybe we should increase
> BYTECHAR_DISTANCE_INCREMENT?  ]

It is indeed another option.
Also, from bug#63040

    Another idea could be moving the cache markers into a separate
    array, so that we can examine them without mixing with all other
    buffer markers.

> Using markers as a cheap cache of conversions was a cute hack but we
> really need to replace it.
>
> Some options that come to mind:
>
> - Keep the tradition of abusing an existing data structure, and stash
>   the bytepos info inside the overlay tree or the text properties.
>   This way the conversion is bounded by O(log BUFFERSIZE).

For overlay tree, it might be even better to stash all the markers in
Emacs into itree structure. For now, every operation involving
adjusting/searching markers scales linearly - not ideal.

> - Use a dedicated data-structure.  E.g. a pair of array-with-a-gap
>   (one indexed by BYTEPOS/STEP the other indexed by CHARPOS/STEP, where
>   STEP would be a large enough constant to make those arrays cheap yet
>   small enough that the remaining scan is cheap).
>   This way the conversion is O(STEP), i.e. "constant-time".

I think that it will be less efficient compared to using a tree-like
structure (if we can manage to use it). Will it be easier?

> BTW, among my various local hacks, I've been using the hack below, which
> aims to randomize the order in our markers-list, so as to minimize the
> risk that we have to wade through lots of markers all clumped around the
> same position.  I don't think it does a good job of it, but maybe we can
> improve the execution of this idea, tho it still doesn't help if there's
> no GC involved.

I am not sure if I believe that this approach can yield practical gains.
AFAIU, the problem with the slowdown we are discussing here is markers
that are all around the same position. It's rather too many markers in
general, spaced not far from each other.

> BTW, if/when we use some other data-structure to convert bytes<->chars,
> then we could presumably get rid of our markers-list and stash markers
> inside our overlay tree (basically represent them as degenerate overlays
> with beg==end and no properties).

I am wondering why it is impossible to stash markers inside overlay tree
without doing anything special about bytes<->chars conversion (other
than changing the linear loop with itree query).

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>