From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: JD Smith Newsgroups: gmane.emacs.devel Subject: Tree-sitter navigation time grows as sqrt(line-number) Date: Thu, 17 Aug 2023 00:01:34 -0400 Message-ID: <3E82D409-6903-4679-9031-939CA35791FF@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_3EDBD708-A953-4306-924A-6AEDAA78B3F5" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22125"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Aug 17 06:02:50 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 1qWUDi-0005an-Eo for ged-emacs-devel@m.gmane-mx.org; Thu, 17 Aug 2023 06:02:50 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qWUCv-0007tt-AN; Thu, 17 Aug 2023 00:02:01 -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 1qWUCn-0007tP-Vq for emacs-devel@gnu.org; Thu, 17 Aug 2023 00:01:59 -0400 Original-Received: from mail-il1-x136.google.com ([2607:f8b0:4864:20::136]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qWUCk-0004TY-K7 for emacs-devel@gnu.org; Thu, 17 Aug 2023 00:01:53 -0400 Original-Received: by mail-il1-x136.google.com with SMTP id e9e14a558f8ab-349a94f3d69so1414285ab.1 for ; Wed, 16 Aug 2023 21:01:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692244906; x=1692849706; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=kHlGw8ENvAc453kcUUgbbZr286OeFSSL9D8pQVIyajE=; b=XBA7xIKkHlkGmPemMlnjHvHUg9/74WLLHL/30BPeBhyC6Kx/C2SexDCNZIeKsjWeUw Fc1yzYBrZrcHpO0HFQlhkVU7EeiXDiD9Nx+C+gML9U2+Vr1OnUt71sHowC/3C7c4wzpq mhbOVBhN84v/cGShwtYVoHQbCNn/o8uSmc8mFMss0kBmb9OdVSLi/ZmKqTX+D61FMdzj trBpgfAfVa8SlWvaIJi0gqGF+rc4Q5bXg8RL1OC8KPzogSyZxysFqWj51PCh6A7n1ewU xThrcpRBux+8mNlCqxUBWTaZN5977fBJVwN3VzIqnkLxH4s0NpIhrZLz4BQiW7+q8bg3 TUZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692244906; x=1692849706; h=to:date:message-id:subject:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kHlGw8ENvAc453kcUUgbbZr286OeFSSL9D8pQVIyajE=; b=A0+eqM4JNnJViysbgy5gqwQtPyhlHq9UtDbyCFUPbOKCgeWFzPpu2DP521m5gj8+KV AAMcvUj81wfBdChdrEiuz/TNb+NHIYuCbNhBsV6Mej2KMjNDH65ZhYQq6WAKyzgwS9JK HREdilSHsyOisNKqdeCJuFfP/FZyeyxbjfI7Bn3mThJJyaX0V25d9uPu2f5lxkUHdImm 3TorCnhZ0HNlCxQ0J3jMDGdeTURcUYjwXTxa4gWysP51nQ9UW8djWc/4eeaVDsYiRJ4p shY+rPxCGtDmVNZT4YnkrPusSTZvG5HhVcj4sKVar+4sxf3N4d2nkgaKR3fPDkZi8w+G Tc0g== X-Gm-Message-State: AOJu0YwiXwW5OdOXSgZcgrnU1/aaCWTIza7//yhVRAkCku5XygozFX+i PmKZOe4fdmdPvHZ10fiZk5sYtb1kCEWyNg== X-Google-Smtp-Source: AGHT+IF35dC1F8kFq8fsMt25m8YFqVjxYCDBDLG8rDYZVZyygHSL0qQt17zgeNMgrjHdaT3k7YXw4g== X-Received: by 2002:a05:6e02:12c5:b0:349:9988:fc51 with SMTP id i5-20020a056e0212c500b003499988fc51mr2727953ilm.14.1692244906388; Wed, 16 Aug 2023 21:01:46 -0700 (PDT) Original-Received: from smtpclient.apple (cm-24-53-184-115.buckeyecom.net. [24.53.184.115]) by smtp.gmail.com with ESMTPSA id r8-20020a92ce88000000b003493fcfaef1sm5067435ilo.67.2023.08.16.21.01.44 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Aug 2023 21:01:45 -0700 (PDT) X-Mailer: Apple Mail (2.3731.700.6) Received-SPF: pass client-ip=2607:f8b0:4864:20::136; envelope-from=jdtsmith@gmail.com; helo=mail-il1-x136.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, DC_PNG_UNO_LARGO=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=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:308845 Archived-At: --Apple-Mail=_3EDBD708-A953-4306-924A-6AEDAA78B3F5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I recently posted about the high variability of Emacs 29=E2=80=99s = tree-sitter navigation performance within a file. I decided to conduct = a simple test on a large python file of about 8400 lines to see if I = could learn more. The test is as follows: at the start of each line, = locate the current syntax node, and starting from it, navigate up to the = root node via `treesit-node-parent=E2=80=99. =20 I was surprised to find that the time this takes grows as sqrt(N), for = line number N. This leads to performance variability of >100x for code = that needs to walk the local syntax tree in large files. Such = variability can make performance projections and optimizations for = latency-sensitive uses of tree-sitter (e.g. via font-lock) tricky. =20 I=E2=80=99m unclear whether this is fundamental to the tree-sitter = parse/tree algorithm, or if the scaling comes from Emacs=E2=80=99 TS = implementation. It does vaguely remind me of similar scaling with an = old line-numbering algorithm, where lines were always being counted from = the beginning of the buffer, so very fast at the front, and very slow = near the end.=20 Code and details here: https://gist.github.com/jdtsmith/7fa6263a13559d587abb51827e6ae472=EF=BF=BC= tree-sitter navigation speed test gist.github.com =09= --Apple-Mail=_3EDBD708-A953-4306-924A-6AEDAA78B3F5 Content-Type: multipart/related; type="text/html"; boundary="Apple-Mail=_483CCA40-26C0-4A4B-B113-6847A82499E2" --Apple-Mail=_483CCA40-26C0-4A4B-B113-6847A82499E2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 I recently = posted about the high variability of Emacs 29=E2=80=99s tree-sitter = navigation performance within a file.  I decided to conduct a = simple test on a large python file of about 8400 lines to see if I could = learn more.  The test is as follows: at the start of each line, = locate the current syntax node, and starting from it, navigate up to the = root node via `treesit-node-parent=E2=80=99. =  

I was surprised to find that the = time this takes grows as sqrt(N), for line number N.  This leads to = performance variability of >100x for code that needs to = walk the local syntax tree in large files.  Such variability can = make performance projections and optimizations for latency-sensitive = uses of tree-sitter (e.g. via font-lock) tricky. =  

I=E2=80=99m unclear whether this is = fundamental to the tree-sitter parse/tree algorithm, or if the scaling = comes from Emacs=E2=80=99 TS implementation.  It does vaguely = remind me of similar scaling with an old line-numbering algorithm, where = lines were always being counted from the beginning of the buffer, so = very fast at the front, and very slow near the = end. 

Code and details = here: