unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dmitry@gutov.dev>
To: Yuan Fu <casouri@gmail.com>
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: Mon, 13 May 2024 02:44:31 +0300	[thread overview]
Message-ID: <f1e4b038-57b2-47cd-a977-3147896edb3b@gutov.dev> (raw)
In-Reply-To: <0132CFC2-CFA0-4D58-9632-6E6E03FE57DB@gmail.com>

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.



  reply	other threads:[~2024-05-12 23:44 UTC|newest]

Thread overview: 19+ 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 [this message]
2024-05-22  5:51       ` Yuan Fu
2024-05-22 23:42         ` Dmitry Gutov
  -- 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

  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=f1e4b038-57b2-47cd-a977-3147896edb3b@gutov.dev \
    --to=dmitry@gutov.dev \
    --cc=casouri@gmail.com \
    --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 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).