From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Dmitry Gutov Newsgroups: gmane.emacs.devel Subject: Re: Tree-sitter navigation time grows as sqrt(line-number) Date: Sun, 20 Aug 2023 03:39:48 +0300 Message-ID: References: <3E82D409-6903-4679-9031-939CA35791FF@gmail.com> <32507689-3b2c-ccbf-dd14-e7bf0bed1ac7@gutov.dev> <6db52945-5459-197c-405d-153ff395a824@gutov.dev> <1F7C956D-6D22-4CC1-8656-6E2A4D07D5FB@gmail.com> <69D18963-D94F-4792-9FF1-159897A99E50@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26671"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Cc: emacs-devel@gnu.org To: JD Smith , Yuan Fu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Aug 20 02:41:00 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qXWV0-0006mu-RV for ged-emacs-devel@m.gmane-mx.org; Sun, 20 Aug 2023 02:40:59 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXWU6-0004Do-4n; Sat, 19 Aug 2023 20:40:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qXWU4-0004Co-4C for emacs-devel@gnu.org; Sat, 19 Aug 2023 20:40:00 -0400 Original-Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qXWU0-000206-QI for emacs-devel@gnu.org; Sat, 19 Aug 2023 20:39:59 -0400 Original-Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 9E4B732007BE; Sat, 19 Aug 2023 20:39:52 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Sat, 19 Aug 2023 20:39:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm2; t= 1692491992; x=1692578392; bh=K/lmp5X8VRnaWcEBHD0h+ED7Gv270P6qFT6 C4XJ/LGk=; b=NHQDj1xW1e6FW99nM6GWM1sYgb/DEA4diJOCNS7CIEDI8v9IFLZ uRo7n6Yo2m/P1KjpVh66GI+oOE87G0iqLjaRzNy085mciyjN/9XedIMw/HBFrhKK ZIs1Q3h6jDtjOt8tLmLDKOirUoyTCruaUXIfZ6DwTdLvshsYpwTwnuKKVyjGXqen JY+6k3KtZU3LfleJwOk0oaprJ5csONbG1I2qmmmt9iXarMsl6RwBsFo8qB7j5cwg ulV7heu+GgfSxxmht+mZhn0DKEoYNE2f9P5q3bChZzR/x2ix2B7pe+p/70n2637J LgxTBLFs/RcBRzdafxcjEN1/fIT+lCxxnig== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1692491992; x=1692578392; bh=K/lmp5X8VRnaWcEBHD0h+ED7Gv270P6qFT6 C4XJ/LGk=; b=XA0jtUaokL7Rzlp6CFL66EzNgXMyN0hq0p+vGvxuiN7woa1OF9O 1XYsTbC9F+lrtYyVNy7EHv8hnFYrykAnG++Dg/RjfbV1tdMiVkXBGxx3HDaTlrJg 6CCRmnpw4wIXsDKRyHv7qHhfQWtk2Pv7Xrz0++fSjFSmFeDFrRqEoKRjJBN6SzgB bNvQwry4pM9puYJwM/BQ4GXRlWMtdotS0cgdMYVI7qxFWCmMM/FuRs/7BIvp/ipq ydqzonavnyGv0fsMxzy3cUjgbvTbyBuinCelwVsI1EGUoeRF4IfKUT0eNLJHzIZA qiTZZeaLMqdiuluSHVVeEifOZUe1f6E86lQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudduiedgfeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddtfeejnecuhfhrohhmpeffmhhi thhrhicuifhuthhovhcuoegumhhithhrhiesghhuthhovhdruggvvheqnecuggftrfgrth htvghrnhepueffveeiffeugffgveejvdegteeuhfdugfehleelfeejtdelteethfdtieeg vddunecuffhomhgrihhnpehgihhthhhusgdrtghomhenucevlhhushhtvghrufhiiigvpe dtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegumhhithhrhiesghhuthhovhdruggvvh X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 19 Aug 2023 20:39:51 -0400 (EDT) Content-Language: en-US In-Reply-To: Received-SPF: pass client-ip=64.147.123.20; envelope-from=dmitry@gutov.dev; helo=wout4-smtp.messagingengine.com X-Spam_score_int: -46 X-Spam_score: -4.7 X-Spam_bar: ---- X-Spam_report: (-4.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-1.862, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:308943 Archived-At: On 20/08/2023 03:18, JD Smith wrote: > Great, thanks. I tried this patch out, and there is indeed about 10x of improvement. Check the bottom of the gist. That said, node_parent remains 10x faster yet (at worst, in a long file), so maybe there’s room for further improvement? Similarly, I also see an improvement from Yuan's patch in my testing (about 2x), while the patch with ts_node_parent remains the fastest anyway. Where my test looks like this: (benchmark 1000 '(treesit-node-parent n)) I looked around for the reasons for the difference. Built the latest tree-sitter (didn't help) and found these two threads on GH: https://github.com/tree-sitter/tree-sitter/issues/567#issuecomment-595564171 - Max Brunsfield says "There is some caching done in that method to make sure it performs well in the common case of walking repeatedly up the tree", but I haven't found where said caching resides so far. https://github.com/tree-sitter/tree-sitter/discussions/878 - mentions that mixing cursor and direct node apis leads to suboptimal results, and just using the former gives an improvement. No "good" code example in there. > May be worth looking at how others are doing it, e.g. the python API. Apparently they have both a wrapper for a cursor API, and node_get_parent which is implemented using ts_node_parent: https://github.com/tree-sitter/py-tree-sitter/issues/34 Leaving it to the callers to choose which one to use.