all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: JD Smith <jdtsmith@gmail.com>
To: Yuan Fu <casouri@gmail.com>
Cc: Dmitry Gutov <dmitry@gutov.dev>, emacs-devel@gnu.org
Subject: Re: Tree-sitter navigation time grows as sqrt(line-number)
Date: Tue, 22 Aug 2023 17:07:19 -0400	[thread overview]
Message-ID: <E59EDBB5-1F5D-4CEB-B4E7-3484E6926C6C@gmail.com> (raw)
In-Reply-To: <48CD64C5-CC2A-42C5-8496-33B188497B99@gmail.com>



> On Aug 21, 2023, at 9:41 PM, Yuan Fu <casouri@gmail.com> wrote:
> 
> 
> 
>> On Aug 20, 2023, at 1:26 PM, Dmitry Gutov <dmitry@gutov.dev> wrote:
>> 
>> On 20/08/2023 15:40, JD Smith wrote:
>>> Looks like a winner (see below, or the gist)!  Thanks all.
>> 
>> Same here, thanks all indeed.
> 
> Let’s run with this patch for sometime. If all goes well, I’ll push to emacs-29. 
> 
>> 
>>> I do think we should consider a treesit-node-ancestors function that collects all the parent (of parent) nodes in one go into an (emacs) list, since you basically have to descend the whole tree from root to find the 1st parent anyway.   Then people who want to know, e.g., “am I in an if block?” can just test node type down the full ancestor list.  Of course, also, node-parent-until/while could be re-written to use node-ancestors, for some additional efficiency.
>> 
>> Should be useful, but the speedup from traversing only once might be negated by the work of allocating the full list of Lisp objects. So it might improve only certain applications.
>> 
>> OTOH, node-parent-until/while could be rewritten in a way that only allocates Lisp on-demand.
> 
> Yeah, node-parent is already fast, and a tree’s height mostly doesn’t grow higher than, say, 30 levels, so I won’t worry about it until some real scenario pops up.
> 

Sounds good.  In the meantime I have discovered treesit-query-capture, which is already very fast and can generate a list of all parents (of a certain type etc.):

(let* ((qry (treesit-query-compile 'python '([(argument_list) (parameters) (list) (dictionary)
     (parenthesized_expression) (subscript)]
    @ctx)))
       (n (treesit-node-at (point)))
       (bg (treesit-node-start n))
       (en (treesit-node-end n)))
  (treesit-query-capture 'python qry bg en t))

It is key to compile the query in advance.  It’s actually pretty similar in speed to the parent navigation.


  reply	other threads:[~2023-08-22 21:07 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-17  4:01 Tree-sitter navigation time grows as sqrt(line-number) JD Smith
2023-08-17 10:56 ` Dmitry Gutov
2023-08-17 11:41   ` Eli Zaretskii
2023-08-17 11:51     ` tomas
2023-08-17 12:21   ` JD Smith
2023-08-17 12:34     ` Dmitry Gutov
2023-08-17 13:19       ` Dmitry Gutov
2023-08-19 14:24         ` JD Smith
2023-08-19 22:16           ` Yuan Fu
2023-08-20  0:18             ` JD Smith
2023-08-20  0:39               ` Dmitry Gutov
2023-08-20  2:01                 ` Yuan Fu
2023-08-20 12:40                   ` JD Smith
2023-08-20 20:26                     ` Dmitry Gutov
2023-08-22  1:41                       ` Yuan Fu
2023-08-22 21:07                         ` JD Smith [this message]
2023-08-31  4:26                           ` Yuan Fu
2023-08-31  6:03                             ` Eli Zaretskii
2023-08-31 11:04                               ` Dmitry Gutov
2023-08-31 11:42                                 ` Po Lu
2023-08-31 17:32                                   ` Dmitry Gutov
2023-08-31 12:51                                 ` Eli Zaretskii
2023-08-31 13:58                                   ` JD Smith
2023-08-31 17:49                                   ` Dmitry Gutov
2023-08-31 19:03                               ` Yuan Fu
2023-08-31 19:06                                 ` Eli Zaretskii
2023-08-31 20:24                                   ` Stefan Kangas
2023-09-01  5:33                                     ` Eli Zaretskii
2023-08-20  6:18             ` Eli Zaretskii
2023-08-18  3:00 ` Yuan Fu
2023-08-18  4:19   ` JD Smith
2023-08-18  5:20     ` Yuan Fu
2023-08-18 13:21       ` Dmitry Gutov
2023-08-18 13:39       ` JD Smith
     [not found] <87v8ddsqwe.fsf@web.de>
2023-08-17 14:25 ` Dmitry Gutov
2023-08-17 14:36   ` Dmitry Gutov
2023-08-17 15:01     ` Dmitry Gutov
2023-08-17 15:15       ` Felix
2023-08-18  2:49         ` Yuan Fu
2023-08-18 13:29           ` Dmitry Gutov

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=E59EDBB5-1F5D-4CEB-B4E7-3484E6926C6C@gmail.com \
    --to=jdtsmith@gmail.com \
    --cc=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.