From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: Tree-sitter navigation time grows as sqrt(line-number) Date: Sat, 19 Aug 2023 15:16:12 -0700 Message-ID: <69D18963-D94F-4792-9FF1-159897A99E50@gmail.com> 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> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_CE95BE45-A34F-4904-A5EB-8F9B30DCFA86" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13051"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Dmitry Gutov , emacs-devel@gnu.org To: JD Smith Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Aug 20 00:17:23 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 1qXUG2-00039Z-P3 for ged-emacs-devel@m.gmane-mx.org; Sun, 20 Aug 2023 00:17:22 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXUFC-0000mf-2S; Sat, 19 Aug 2023 18:16:30 -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 1qXUF9-0000lx-Rp for emacs-devel@gnu.org; Sat, 19 Aug 2023 18:16:27 -0400 Original-Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXUF7-00036G-D8 for emacs-devel@gnu.org; Sat, 19 Aug 2023 18:16:27 -0400 Original-Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-68a40d8557eso9231b3a.1 for ; Sat, 19 Aug 2023 15:16:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692483384; x=1693088184; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=KEaH7/J/fKyk2+PA1ZVVGLmhbDfS5QnjiUEwutHXakA=; b=bQSpOueS3zKRwRcmbUiGVes8rSbzoU4wFksYpV4HDi+tAWzhnLRy3SsfivaA4cjOlP hLzyeelfRCLOw//bzL9h6diRVdjgXriI3nedxfPzjUttDjOgam9qR2NSSOxV/1m5kUdH 1wJfU8ESAxj/y34Vm3CybpV/6Bm6JnH0eIzAoZFKfr8bAcPpgbnZ9X4mb/5k5jENiCyK E6B3r5VJ2wN9mO2eq8R9VUL/QMkKangYYUGVBW0qE65HEAWwolciKGRR4O5mU2jFGlRl mOPcPDibuuqh/jZO43542zJuCD5he9JvjrI1sZ7RmEd1puHGMIJ9bQAdMM6Equ3sgvvK sZvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692483384; x=1693088184; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KEaH7/J/fKyk2+PA1ZVVGLmhbDfS5QnjiUEwutHXakA=; b=XC3Q0ZNhgS4M0QW1TLWK4nl/HVG4jQJLNWzIrmL/QZhM8kE+Poviq1HZj2DUCQ9gBX 9cEKF1KL8PFaGKD5G+9SO+EcdsZAYLRDS/9QkjkTjSw3LOPUHi1IS0nrUJW7a1FeaFYn 5lgrc/PTIkhgj8nA39D+0QyrimJiT8P2SGwT62ziEkphRHiar7NG8kUmJW5Qwu5qm+B1 gMiiLzUskwBLZ6zgGsFp61BHLFiAz0c5A1aZ7BjZEmLjjWkATYxOFFrF8aneursfbfBD MkTQyv+m38PsAbGK2cYeJHopGjSMzQa0aU7/iz6220lW7bRS/PFzxtzom29yJkSs+edJ uBvQ== X-Gm-Message-State: AOJu0YyX8M5qhg9Zf2bdDsxgPKLbSk+/jo42lPliJnVZKz/0eAmdvwkI g7kebF/KOEB7d9whRSORdCixsSqjZAs= X-Google-Smtp-Source: AGHT+IHGrgWdQsxVEg6/ZMX11CgCikBc4EqjSfN21hVeVuj82o99dFVOLWUnjevt1YQtKD/17cMpVw== X-Received: by 2002:a05:6a00:1388:b0:666:b22d:c6e0 with SMTP id t8-20020a056a00138800b00666b22dc6e0mr4039495pfg.11.1692483383884; Sat, 19 Aug 2023 15:16:23 -0700 (PDT) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id n9-20020aa79049000000b006870ed427b2sm3692982pfo.94.2023.08.19.15.16.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2023 15:16:23 -0700 (PDT) In-Reply-To: <1F7C956D-6D22-4CC1-8656-6E2A4D07D5FB@gmail.com> X-Mailer: Apple Mail (2.3731.600.7) Received-SPF: pass client-ip=2607:f8b0:4864:20::42f; envelope-from=casouri@gmail.com; helo=mail-pf1-x42f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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:308941 Archived-At: --Apple-Mail=_CE95BE45-A34F-4904-A5EB-8F9B30DCFA86 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Aug 19, 2023, at 7:24 AM, JD Smith wrote: >=20 > Thanks for your patch, Dmitry. I had a chance to test it this morning = (the new, non-crashing version). I made a new NS build, with and = without the patch. The results are really striking (scroll to bottom): >=20 > https://gist.github.com/jdtsmith/7fa6263a13559d587abb51827e6ae472 >=20 > Summary: >=20 > - Applying the same test above on _axes.py reproduces the earlier = emacs-mac/29 results: the time to navigate from the node at line = beginning to root starts at under 10=C2=B5s, but rises as sqrt(N) by = ~100x, reaching over 3000=C2=B5s. >=20 > - With Dimitry=E2=80=99s patch, it performs much, much better, = starting off with similar timing at early positions in the file, but = rising no higher than 50=C2=B5s, scaling much shallower than sqrt(N). >=20 > I should emphasize this is a new fast machine; I fully expect my old = laptop would be much slower (10x ?) than 3ms in files this large, which = makes using parent navigation for things like font-lock problematic. >=20 > The patched version results also make a lot more sense in terms of = their similar logarithmic growth as node-at-point, since the method of = search for a node at point and for its parent is, as Yuan points at, = quite similar. I inspected the descending algorithm, and there=E2=80=99s indeed an = oversight made by me. Here=E2=80=99s a patch that should fix it. I = tested it briefly and it does speeds things up greatly. Thanks for = investigating this, JD! I think the patch is relatively safe, so maybe we can push it to = emacs-29 instead of master. Yuan --Apple-Mail=_CE95BE45-A34F-4904-A5EB-8F9B30DCFA86 Content-Disposition: attachment; filename=node-parent.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="node-parent.patch" Content-Transfer-Encoding: quoted-printable =46rom=20dd20c4449493765c22dd2067ae410490e9f1d1dc=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Yuan=20Fu=20=0ADate:=20Sat,=20= 19=20Aug=202023=2015:04:20=20-0700=0ASubject:=20[PATCH]=20Fix=20= treesit_cursor_helper_1=0A=0A*=20src/treesit.c=20= (treesit_cursor_helper_1):=20Skip=20child=20nodes=20that=20can't=0A= contain=20TARGET=20when=20traversing=20the=20tree:=20only=20traverse=20= down=20the=20child=0Anode=20if=20that=20node's=20end=20is=20grater=20or=20= equal=20to=20TARGET's=20end.=0A---=0A=20src/treesit.c=20|=203=20++-=0A=20= 1=20file=20changed,=202=20insertions(+),=201=20deletion(-)=0A=0Adiff=20= --git=20a/src/treesit.c=20b/src/treesit.c=0Aindex=20= 1f694e47201..f9e98244a4f=20100644=0A---=20a/src/treesit.c=0A+++=20= b/src/treesit.c=0A@@=20-3048,7=20+3048,8=20@@=20treesit_cursor_helper_1=20= (TSTreeCursor=20*cursor,=20TSNode=20*target,=0A=20=20=20=20=20=20= siblings=20that=20could=20contain=20TARGET.=20=20*/=0A=20=20=20while=20= (ts_node_start_byte=20(cursor_node)=20<=3D=20end_pos)=0A=20=20=20=20=20{=0A= -=20=20=20=20=20=20if=20(treesit_cursor_helper_1=20(cursor,=20target,=20= end_pos,=20limit=20-=201))=0A+=20=20=20=20=20=20if=20(ts_node_end_byte=20= (cursor_node)=20>=3D=20end_pos=0A+=09=20=20&&=20treesit_cursor_helper_1=20= (cursor,=20target,=20end_pos,=20limit=20-=201))=0A=20=09return=20true;=0A= =20=0A=20=20=20=20=20=20=20if=20(!ts_tree_cursor_goto_next_sibling=20= (cursor))=0A--=20=0A2.41.0=0A=0A= --Apple-Mail=_CE95BE45-A34F-4904-A5EB-8F9B30DCFA86--