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.bugs Subject: bug#61285: (Sometimes very) slow font-lock after %w in ruby-ts-mode Date: Sun, 5 Feb 2023 16:08:36 -0800 Message-ID: <2C6F83F0-DD84-4549-9D91-3D820188674D@gmail.com> References: <17a29b2e-beb2-f246-fe1c-2c3503c1bfe6@yandex.ru> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.300.101.1.3\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2251"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 61285@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 06 01:09:16 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1pOp4N-0000Nu-TW for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 06 Feb 2023 01:09:16 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOp4F-0005XH-6b; Sun, 05 Feb 2023 19:09:07 -0500 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 1pOp4A-0005Vq-Pl for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 19:09:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pOp4A-0006L0-Gz for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 19:09:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pOp4A-0006yH-28 for bug-gnu-emacs@gnu.org; Sun, 05 Feb 2023 19:09:02 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <17a29b2e-beb2-f246-fe1c-2c3503c1bfe6@yandex.ru> Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 06 Feb 2023 00:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61285 X-GNU-PR-Package: emacs Original-Received: via spool by 61285-submit@debbugs.gnu.org id=B61285.167564213726785 (code B ref 61285); Mon, 06 Feb 2023 00:09:02 +0000 Original-Received: (at 61285) by debbugs.gnu.org; 6 Feb 2023 00:08:57 +0000 Original-Received: from localhost ([127.0.0.1]:46758 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOp44-0006xw-Kz for submit@debbugs.gnu.org; Sun, 05 Feb 2023 19:08:57 -0500 Original-Received: from mail-pj1-f41.google.com ([209.85.216.41]:37837) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOp42-0006xh-7S for 61285@debbugs.gnu.org; Sun, 05 Feb 2023 19:08:54 -0500 Original-Received: by mail-pj1-f41.google.com with SMTP id c10-20020a17090a1d0a00b0022e63a94799so13651259pjd.2 for <61285@debbugs.gnu.org>; Sun, 05 Feb 2023 16:08:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:from:to:cc:subject:date:message-id :reply-to; bh=7nJV3m2659JxdgFr8rMpzKKUXncrIfhrKeT4r7KqLdc=; b=a9Y1V/DR0jT7LUIm+u6T9Ju2ugQCitkwh/z/7WoY+sD1qVFj78qAbMZe/ri1mr7f5c LsrZtmf0rNPt/MxVa9kWDFRm9QbxxMaiBq0rGibaUT28AoV0T7l1hWVy5rR1tdN9QzCb 6nbnDeqQvf5A5sOKVZH7Zte1icXIrBJOIA0PsMAEFxUSUbGaChShcagye4Lw0dOiopCE bfuRIrTUvkKrK8Fnoe3AbEPf7JKsixtLpiIenbYzFiHPRbzmUQ6bvFjX+WjgSnhxdUKE FhHc93TePUJce9QKcvpw6NShSYTqHpetbW0PX51WWh5ezETndmaOfJjVvS3epe1Qh9Jw Uyng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7nJV3m2659JxdgFr8rMpzKKUXncrIfhrKeT4r7KqLdc=; b=cfuU4Ck5hMqu6ggDGEU1JEcxhfj1HScFqdzovb2gPFGZhEa6PGbYe785Biy1l1mP8q OOaYrcc2I4qXz+7l8f8E7DktR/bC5hLuzQqAwCL95lLv7KXOh3uqppEr44nNwN355Bfa eVi8Xfg3kuqpH1XrH3jac6MYvEAAF2j+Om3b5bC2fMZDoEY3ZC3LGIpjn1LMW9qrtDIS gIJetl4gi7svewADaWlSTvVbRFStHQPQaspvrBX7Tdyh8NvvYVW5kAvVMXDYpC8Sz2oR fSIxcb1IJSY3Onh6gTBpFvgBC2qmAcZ/PaF+9Zl3me7V4b+9JYldQ2UL9fdgEGLM4BSY mEJg== X-Gm-Message-State: AO0yUKXzvcIy2z/vhQlzIEvxYBVlJWC9GkWj2Kt/F9UImKAazRWwuQfV FL2vPmExwGkGUWQC7KQTutA= X-Google-Smtp-Source: AK7set+ZgRVRIV+ob+h6xw0OqsFEKAh7Q5Twu0lqK21gZW/SoaHfO/RhH3lDNHfyQ6C+bY7XyHjEPw== X-Received: by 2002:a17:903:1247:b0:195:e92e:c4d3 with SMTP id u7-20020a170903124700b00195e92ec4d3mr22323133plh.46.1675642128026; Sun, 05 Feb 2023 16:08:48 -0800 (PST) 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 g24-20020a1709029f9800b0019663238703sm3168204plq.109.2023.02.05.16.08.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Feb 2023 16:08:47 -0800 (PST) X-Mailer: Apple Mail (2.3731.300.101.1.3) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:254909 Archived-At: Dmitry Gutov writes: > This probably involves a parser bug and/or maybe a tree-sitter one. > But I'm posting this here anyway because this might not be the only > way to trigger this problem. Or it could give us some optimization > insights. > > Also, while it involves a node which is parsed to have a large number > of descendants, the performance depends heavily on whether the node is > at the top level of the program (then it's slow), or not. > > To repro: > > 1. Visit test/lisp/progmodes/ruby-mode-resources/ruby.rb > 2. add 'a =3D %w' (without quotes) as a separate new line before all = of > the existing code. > 3. Notice the delay in redisplay after you type 'w'. > > In you do that with a larger file, BTW, this delay may be on the order > of a minute. Here's an example of such file: > = https://github.com/rails/rails/blob/main/activerecord/lib/active_record/as= sociations.rb > > The superficial reason for this delay is that %w opens a new "array of > strings" literal which parses every separate word in the rest of the > buffer as a separate string. So we get a node with thousands of > children, in the case of associations.rb. Or just ~1000 in the case of > ruby.rb. > > I also tried setting treesit--font-lock-fast-mode to t: no effect. > > But! If we do the same not on top-level -- say, put the 'a =3D %w' = line > after the 'foo' line inside the first 'if' statement (i.e. on line 7), > the delay is much smaller -- not noticeable in ruby.rb, and still > apparent but much more bearable in associations.rb (you can put that > statement right after 'module ActiveRecord') -- even though the size > of the tree is changed minimally, and the number of children nodes for > that "array of strings" still counts in the thousands (e.g. 13319). > > Perf report for the "bad" highlighting delay looks like this: > > 61.19% emacs libtree-sitter.so.0.0 [.] = ts_tree_cursor_current_status > 30.88% emacs libtree-sitter.so.0.0 [.] ts_tree_cursor_parent_node > 7.44% emacs libtree-sitter.so.0.0 [.] ts_language_symbol_metadata > 0.06% emacs libtree-sitter.so.0.0 [.] = ts_tree_cursor_goto_first_child > 0.05% emacs libtree-sitter.so.0.0 [.] = ts_tree_cursor_goto_next_sibling > 0.03% emacs libtree-sitter.so.0.0 [.] ts_node_end_byte > > And like this in the "good" case (with many type-backspace = repetitions): > > 32.10% emacs libtree-sitter.so.0.0 [.] = ts_tree_cursor_current_status > 9.50% emacs libtree-sitter.so.0.0 [.] = ts_tree_cursor_goto_first_child > 7.89% emacs libtree-sitter.so.0.0 [.] = ts_tree_cursor_goto_next_sibling > 7.51% emacs libtree-sitter.so.0.0 [.] ts_language_symbol_metadata > 6.45% emacs libtree-sitter.so.0.0 [.] ts_tree_cursor_parent_node > 1.87% emacs libtree-sitter.so.0.0 [.] ts_node_start_point > 1.85% emacs emacs [.] process_mark_stack > 0.93% emacs libtree-sitter.so.0.0 [.] ts_tree_cursor_current_node Interesting. Perhaps it has to do with how tree-sitter implements the "incremental" part of the parser? But the profile doesn=E2=80=99t look = like it=E2=80=99s spending time parsing, I need to look at what does ts_tree_cursor_current_status actually do (maybe it=E2=80=99s used in = parsing?) Yuan