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 indentation for js-mode & cc-mode Date: Fri, 28 Oct 2022 12:43:31 -0700 Message-ID: <89721927-13CE-4A65-944C-0DC924127373@gmail.com> References: <9AF8BFDC-C9A2-4AE5-A8D2-E6AA05DA3C91@gmail.com> <87k04lljh6.fsf@thornhill.no> <541CF451-6FAC-4531-A8AF-8C86FBB9D40B@thornhill.no> <425E0075-3F44-4832-BA2E-61E7D0A26FF4@gmail.com> <87mt9ggvph.fsf@thornhill.no> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) 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="1447"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel , Stefan Monnier To: Theodor Thornhill Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Oct 28 21:45:07 2022 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 1ooVHu-00007I-7e for ged-emacs-devel@m.gmane-mx.org; Fri, 28 Oct 2022 21:45:06 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ooVGT-0004d2-KI; Fri, 28 Oct 2022 15:43:37 -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 1ooVGS-0004cr-CJ for emacs-devel@gnu.org; Fri, 28 Oct 2022 15:43:36 -0400 Original-Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ooVGQ-00077K-UU for emacs-devel@gnu.org; Fri, 28 Oct 2022 15:43:36 -0400 Original-Received: by mail-pj1-x102d.google.com with SMTP id o7so2324115pjj.1 for ; Fri, 28 Oct 2022 12:43:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ZWl7IbGxPSciVKxW/AWoIKEGD23+wLiNXAlJnmkpvJw=; b=Mf1UYr08ve+FOKLI3Sh8n9ekqwKAtU6Af/p5NEQZWIJCYxDcNJicEalsi7YtG1M/qI gP63rCX/FZr17524WDVEYrZqiYdajjNA8NNLx5w1GsjhkHC7KJt3I+7qQltdsGROCq/m TeNYEioDs7tqRLZGc4zN8jB7bO9UOU0FPsY/LHBn8lufB/fAimA5m11rJRkUwaAOr8Wm LyaoXXZthSfS1BtYFLq54Yd+t+VBUKroAR3jD9RzzOyhsGWxJ5Y/fFEfE/1PbZker5rM QWIwqzqcLtLIi9MJG6piSnje1nqBk5WMEgJHqhcaa25bviy1wB9pDtIFDYIMREEIUYqr ZmOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZWl7IbGxPSciVKxW/AWoIKEGD23+wLiNXAlJnmkpvJw=; b=zW9IqnV2l/0LgBPNAW80DqvkaueWSSqPQrIxeUPBdnYcjC0VLXBmiaB3ASBT8RXW6E aV/d+FjkWBfxCYfta9ERpxpnCueorvgy8URq97DRXOwAOV0msltDpxPBKofbMHUHlvyK WB6IESGnOiRUtROC4w54k9Elz7JWA+38uqkV6CCfZZVkO8WDkrRWZ7aZ+eiU7Ffeq8pE 90YQ6xQLR1K0pOZVxVeZHeef1HvZnmq3e5Vo7HMyWwbNFNDrZ3xHXoPiLks3riw/G7TB FkQ0MZMrKXeTOH4C81Vgj7pNGdfhoeQ1H6QpmnasJ2DbZyGBR/U39LSMduVrE2CaLZYD 38sw== X-Gm-Message-State: ACrzQf0WDV0R7jHXQqAAvjzLcQ1d4PwcMrj1odqbpuMo/FOZoXPwh4te FcZWh/WCZzuJSsXe7aP6AjU= X-Google-Smtp-Source: AMsMyM45+uO+cdfRn4I8QA9CS+Mq/SvkujiqRbFWatXz5NeT+bXFiS3TDGCb9791B5Dxqapu/oXxxg== X-Received: by 2002:a17:902:e952:b0:17c:2eee:c0ce with SMTP id b18-20020a170902e95200b0017c2eeec0cemr566570pll.145.1666986213340; Fri, 28 Oct 2022 12:43:33 -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 p18-20020a170902ead200b00176a6ba5969sm3409880pld.98.2022.10.28.12.43.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Oct 2022 12:43:32 -0700 (PDT) In-Reply-To: <87mt9ggvph.fsf@thornhill.no> X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::102d; envelope-from=casouri@gmail.com; helo=mail-pj1-x102d.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: , Original-Sender: "Emacs-devel" Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:298694 Archived-At: > On Oct 28, 2022, at 2:10 AM, Theodor Thornhill = wrote: >=20 > Yuan Fu writes: >=20 >>>=20 >>>>> looking up way to much the root of the tree, but you know the = internals >>>>> here better than me. Is this something we can optimize away? See = the >>>>> attached report at the bottom. >>>>=20 >>>> This is very strange, I need to look into it. >>>>=20 >>>=20 >>> I'm happy to provide more info and profiling, as well as testing if = need be!=20 >>=20 >> I just tried running treesit-buffer-root-node and treesit-node-at >> 10000 times in the end of buffer and they are pretty fast, so I = don=E2=80=99t >> know why the benchmark says 99% time is spent in >> treesit-buffer-root-node. Could you share the benchmark code and test >> file? Thanks! >>=20 >> Yuan >=20 >=20 > Absolutely. I ran the test again - see test file and new report in > attachments. >=20 > You need to `M-x eval-buffer` in `treesit.el` to avoid the compiled > functions to get better profile report, then in the testfile: >=20 > M-x profiler-start > C-x h ;; (mark-whole-buffer) > C-i ;; (indent-for-tab-command) > ;; --- waaaaait > M-x profiler-stop > M-x profiler-report >=20 > There's no test code for this, just running the commands sequentially > and get the report :-) >=20 > Are we parsing the whole file over and over in = treesit-buffer-root-node? > Do we for some reason not hit the early return? >=20 > The js-file [0] is taken from [1] and duplicated and messed up > indentation. Report [2] was messed up by dpaste it seems. Gmail = didn't > want the files as attachments, so paste it is :-) >=20 > Hope this is useful! >=20 > Theo >=20 Ok, I=E2=80=99m fairly certain this is due to tree-sitter reparsing = after we indenting each line: treesit-buffer-root-node asks for the root = node of the parser, which triggers a reparse, because last indent = modified the buffer. We are basically reparsing as many time as there = are lines in the buffer. Indenting a similarly sized buffer where all indent are good is much = faster, because there is no reparse due to change to the buffer. Tree-sitter indent should add an implementation for indent-for-region = function which precomputes indent for each line and indent lines in = batch. That ought to fix it. Added to TODO :-) Yuan=