all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: "Ergus via Emacs development discussions." <emacs-devel@gnu.org>
Subject: Re: treesitter local parser: huge slowdown and memory usage in a long file
Date: Tue, 21 May 2024 22:51:02 -0700	[thread overview]
Message-ID: <8E3466C4-0875-4187-ADC3-5C72FF23A24F@gmail.com> (raw)
In-Reply-To: <f1e4b038-57b2-47cd-a977-3147896edb3b@gutov.dev>



> On May 12, 2024, at 4:44 PM, Dmitry Gutov <dmitry@gutov.dev> wrote:
> 
> On 09/05/2024 03:16, Yuan Fu wrote:
> 
>>> Is it possible that there would occur multiple changes and reparses between some firings of the above hooks? For example, some new feature might go over the buffer's text with an automated multi-step transformation, calling the parser (but not syntax-ppss) on each step.
>>> In such a scenario it seems treesit--pre-redisplay might miss intermediate range updates. Would that be okay?
>> I think you’re right. The chance of it actually go wrong will be slim, but anything that’s possible to go wrong will eventually go wrong.
> 
> Thanks for confirming the concern.
> 
>> The remaining question is how. I’m thinking of keeping a history of updated ranges, each marked with the parser timestamp. The parser timestamp is already there, it’s incremented every time the parser reparses. And treesit-parser-changed-ranges will return the timestamp along with the updated ranges. Then in the next iteration, the consumer can pass the last timestamp to treesit-parser-changed-ranges, which tells it to return all the changed ranges since that timestamp.
>> The only problem is to decide how long a history of updated ranges do we keep for each parser. The 100% correct approach is to maintain a separate history for each consumer, and never throw away old ranges until the consumer consumes them. But then you risk wasting memory if some consumer never consumes the ranges. To handle that we can add a hard limit. But then this hard limit might be too low for some edge case… We can make this hard limit configurable, and if we ever encountered a case where this hard limit is not enough and there’s no way around it (unlikely), we can instruct users or lisp program to increase it.
> 
> That could work. Although it's hard for me to imagine how far back the history would have to be stored, and would that have any practical consequences for Emacs's memory use. Maybe not.
> 
> The approach I was thinking of is in different direction: we take a step back, remove (or stop using at least) the new function, and go back to the idea of subscribing to parsers' after-change notifiers. The improvement in commit f62c1b4cd00 seems to stem from relying foremost on changes ranges in the primary parser. Okay - we re-add the listener for the primary parser only.
> 
> This listener would be specific for a particular consumer. In our case, we'd have a listener which would populate - and then update - the variable used by treesit--pre-redisplay. That variable would store the "up to date" list of updated ranges. The listener, on every call, would "merge" its current value one with the new list of ranges (*). treesit--pre-redisplay would use the data in that data structure instead of calling treesit-parser-changed-ranges, and set the value to nil to "reset" it for the next update.
> 
> (*) So real "merging" would only need to be performed when listener fires 2+ times between the two adjacent treesit--pre-redisplay calls. Otherwise the current value is nil, so the the new list is simply assigned to the variable. Anyway, the merging logic seems to be the trickiest part in this scheme (managing and interpreting offsets), but it should be very similar in both approaches.

I agree. The usefulness of treesit-parser-changed-ranges aren’t really justified at this point (well, except that it makes the caller’s code much easier to follow). Let me implement what you described and let’s see how it goes. I think we don’t even need to merge the ranges (which will be prone to bugs if I were to write it ;-), we can just push the new ranges to a list and later process them one by one.

Yuan


  reply	other threads:[~2024-05-22  5:51 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-20  2:18 treesitter local parser: huge slowdown and memory usage in a long file Yuan Fu
2024-04-20 19:14 ` Vincenzo Pupillo
2024-04-23  5:09   ` Yuan Fu
2024-05-06  2:04 ` Dmitry Gutov
2024-05-09  0:16   ` Yuan Fu
2024-05-12 23:44     ` Dmitry Gutov
2024-05-22  5:51       ` Yuan Fu [this message]
2024-05-22 23:42         ` Dmitry Gutov
2024-05-27 22:03           ` Yuan Fu
2024-05-27 22:24             ` Dmitry Gutov
2024-06-04  4:53               ` Yuan Fu
2024-06-04  8:19                 ` Vincenzo Pupillo
2024-06-04 15:01                   ` Eli Zaretskii
2024-06-04 20:44                     ` Vincenzo Pupillo
2024-06-06 23:59                 ` Dmitry Gutov
2024-06-07  0:13                   ` Dmitry Gutov
2024-06-11 16:36                     ` Yuan Fu
2024-05-26  4:23     ` Stefan Monnier via Emacs development discussions.
2024-05-27 22:05       ` Yuan Fu
2024-05-27 22:34         ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2024-02-11 21:53 Vincenzo Pupillo
2024-02-12  4:16 ` Yuan Fu
2024-02-12 14:09   ` Eli Zaretskii
2024-02-13  8:15     ` Yuan Fu
2024-02-13  9:39       ` Vincenzo Pupillo
2024-02-13 12:59       ` Eli Zaretskii
2024-02-13  0:50   ` Dmitry Gutov
2024-02-13  8:08     ` Yuan Fu
2024-02-18  3:37       ` Dmitry Gutov
2024-02-19  5:53         ` Yuan Fu
2024-03-21  6:39           ` Yuan Fu

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8E3466C4-0875-4187-ADC3-5C72FF23A24F@gmail.com \
    --to=casouri@gmail.com \
    --cc=dmitry@gutov.dev \
    --cc=emacs-devel@gnu.org \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.