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.bugs Subject: bug#60691: 29.0.60; Slow tree-sitter font-lock in ruby-ts-mode Date: Tue, 10 Jan 2023 16:10:49 +0200 Message-ID: <4ed5051c-ea5a-91b1-6b8c-5349a3495a16@yandex.ru> References: <867cxv3dnn.fsf@mail.linkov.net> <51ee2f6f-6e1d-eccd-f536-461d916cc94d@yandex.ru> <86leman71u.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3747"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Cc: 60691@debbugs.gnu.org To: Juri Linkov , Yuan Fu Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jan 10 16:48:08 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 1pFGrA-0000MV-0g for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 10 Jan 2023 16:48:08 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pFFLD-0000hb-Up; Tue, 10 Jan 2023 09:11:04 -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 1pFFLC-0000hI-Ad for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2023 09:11: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 1pFFLC-0005P7-2r for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2023 09:11:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pFFLB-0006cO-Tm for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2023 09:11:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Dmitry Gutov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 10 Jan 2023 14:11:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60691 X-GNU-PR-Package: emacs Original-Received: via spool by 60691-submit@debbugs.gnu.org id=B60691.167335986025433 (code B ref 60691); Tue, 10 Jan 2023 14:11:01 +0000 Original-Received: (at 60691) by debbugs.gnu.org; 10 Jan 2023 14:11:00 +0000 Original-Received: from localhost ([127.0.0.1]:39163 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pFFLA-0006c8-8e for submit@debbugs.gnu.org; Tue, 10 Jan 2023 09:11:00 -0500 Original-Received: from mail-wm1-f46.google.com ([209.85.128.46]:52084) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pFFL8-0006bs-47 for 60691@debbugs.gnu.org; Tue, 10 Jan 2023 09:10:59 -0500 Original-Received: by mail-wm1-f46.google.com with SMTP id g10so8886810wmo.1 for <60691@debbugs.gnu.org>; Tue, 10 Jan 2023 06:10:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=TjVO7Iu2PTtRuEs848MOngu4IUBVHaOl2a8o366V1ms=; b=mrNBiJoScwiSRm/Lhc5+jcHxvT/Vdwmt6EVFVRtCfCxCxKCX4Iybb06RjBS+Ae87jl mncde8aZGREUgKaagbs8TnpZ1HX9gBR5KWrWZ7Y2aBBMo6vnCpM44Sj//0ttVlwzMMCd YF760PI+PIlkdhbKUGKao3uQuURe6rANQOnLDEc5+Yb8YkO+s9s/V7db/HfsFoz27Of/ QJzcy39gwJlgs9jeV4DiQ3bPo8vklzazgObzLybOhZ49zROZ/3Zs0nZs1jUoUbB6gOtr ZjedvEu9g1VV4VwOzCXQcO8ybTFWXVhNSnH0G72xWgu4IZC35bzhe+8uuKuDnytTozQR 1Bdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TjVO7Iu2PTtRuEs848MOngu4IUBVHaOl2a8o366V1ms=; b=2EtT6sutPxHqezg8hl7tHubZqrzA3mksm5tr/QxUU62nb4Aw9UTxWXmjNWoPA3DQ2y wAeOAPDPqGWxH0oB1EfwlYOaObJ2cWkdJpTRGzWkSComeor8zCNmB+obVzRTL8JCUrQa 2u5aYN+IhNI6h0T0RICZDl46AF9D91dHqfpNFFiEKj39zw57wiPWsbZI38198a8ADf6A rl6ZzmPfFxV2av+GzzMa0J209T2HARGMLsgGpNKM/3FF/l8prCkygBMKFVcDg9mPK64c LHgjpSeikTDVkxK1ttK6OZlUnw7IAAZvureCgBh6Ig3Gtd8ZZJa+/1BRplbqenpcIXH/ kb2Q== X-Gm-Message-State: AFqh2krljc7xaIfA0DSMbsL/HveqmiyqAUQOQDyGnixwDPtlcAh1tuYD MrCFqy049GMnAeltKO6w/3k= X-Google-Smtp-Source: AMrXdXvMhePPVrJP0Vk6j6S4D0bW5vHAxMAn40mHnz+tstN2UpV1JNYObp/GlBJMFWQkXFRIjfy2Ww== X-Received: by 2002:a05:600c:4da2:b0:3d2:39dc:f50e with SMTP id v34-20020a05600c4da200b003d239dcf50emr49123608wmp.7.1673359852028; Tue, 10 Jan 2023 06:10:52 -0800 (PST) Original-Received: from [192.168.0.2] ([46.251.119.176]) by smtp.googlemail.com with ESMTPSA id i14-20020a05600c354e00b003d1d5a83b2esm21678073wmq.35.2023.01.10.06.10.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jan 2023 06:10:51 -0800 (PST) Content-Language: en-US In-Reply-To: <86leman71u.fsf@mail.linkov.net> 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:253080 Archived-At: On 10/01/2023 10:10, Juri Linkov wrote: >>> After more rules were added recently to ruby-ts--font-lock-settings, >>> font-lock became slow even on very small files. Some measurements: >> >> If you saw a particular commit that made things slower, did you try >> reverting it? What was the performance after? > > No particular commit, just adding more rules degrades performance > gradually. But I don't think I added that many rules recently. No more than a quarter anyway. >>> M-: (benchmark-run 1000 (progn (font-lock-mode -1) (font-lock-mode 1) (font-lock-ensure))) >>> M-x ruby-mode >>> (1.3564674989999999 0 0.0) >>> M-x ruby-ts-mode >>> (8.349582391999999 2 6.489918534000001) >> >> I have tried this scenario (which, to be frank, is pretty artificial, given >> that fontification is usually performed in chunks, not over the whole >> buffer). >> >> Perhaps the results depend on a particular file. The ones I have tried >> (ruby.rb and ruby-after-operator-indent.rb) show only 2x difference (or >> less). The difference was in favor of ruby-mode, but given the difference >> in approaches I wouldn't be surprised if ruby-ts-mode incurs a fixed >> overhead somewhere. > > On test/lisp/progmodes/ruby-mode-resources/ruby.rb I see these numbers: > > ruby-mode > (8.701560543000001 95 1.045961102) > > ruby-ts-mode > (34.653148898000005 1464 16.904981779) Interesting. It's 12s vs 36s for me, as I've retested now. >>> This is not a problem when files are visited infrequently, but >>> becomes a problem for diff-syntax fontification that wants to >>> highlight simultaneously many files from git logs. >>> So a temporary measure would be not to enable ruby-ts-mode >>> in internal buffers: >> >> Is it common to try to highlight 1000 or even 100 files in one diff? > > 100 is rare, but tens is pretty common, so this problem affects > only this specific case. So it's a 0,8-3s delay in those cases? That's not ideal. >>> (add-hook 'find-file-hook >>> (lambda () >>> (when (and (eq major-mode 'ruby-mode) >>> ;; Only when not internal as from diff-syntax >>> (not (string-prefix-p " " (buffer-name)))) >>> (ruby-ts-mode)))) >> >> Have you tried similar tests with other -ts- modes? Ones with complex >> font-lock rules in particular. > > I tried with c-ts-mode, and it's very fast. Just how fast is it? The number of font-lock features is has is comparable (though a little smaller). I've tried the same benchmark for it in admin/alloc-colors.c, and it comes out to (3.2004193190000003 30 0.9609690980000067) Which seems comparable. Not sure how to directly test the modes against each other, but if I enable ruby-ts-mode in the same file, the benchmark comes to 1s. Or if I enable c-ts-mode in ruby.rb -- 16s. >> I've tried commenting out different rules in ruby-ts--font-lock-settings, >> but none of them seem to have particularly outsides impact. Performance >> seems, roughly, inversely proportional to the number of separate >> "features". > > Indeed, this is what I see - no particular rule, only their number > affects performance. > >> And if all ts modes turn out to have this problem, perhaps the place to >> improve this is inside some common code. > > I noticed that while most library files are small, e.g. > libtree-sitter-c.so is 401,528 bytes, > libtree-sitter-ruby.so is 2,130,616 bytes > that means that it has more complex logic > that might explain its performance. ruby is indeed one of the larger ones. Among the ones I have here compiled, it's exceeded only by cpp. 2.29 MB vs 2.12 MB. But testing admin/alloc-colors.c with c++-ts-mode vs c-ts-mode gives very similar performance, so it's unlikely that the complexity of the grammar is directly responsible. > In this case, when nothing could be done to improve performance, > please close this request. Perhaps Yuan has some further ideas. There are some strong oddities here: - Some time into debugging and repeating the benchmark again and again, I get the "Pure Lisp storage overflowed" message. Just once per Emacs session. It doesn't seem to change much, so it might be unimportant. - The profiler output looks like this: 18050 75% - font-lock-fontify-syntactically-region 15686 65% - treesit-font-lock-fontify-region 3738 15% treesit--children-covering-range-recurse 188 0% treesit-fontify-with-override - When running the benchmark for the first time in a buffer (such as ruby.rb), the variable treesit--font-lock-fast-mode is usually changed to t. In one Emacs session, after I changed it to nil and re-ran the benchmark, the variable stayed nil, and the benchmark ran much faster (like 10s vs 36s). In the next session, after I restarted Emacs, that didn't happen: it always stayed at t, even if I reset it to nil between runs. But if I comment out the block in treesit-font-lock-fontify-region that uses it ;; (when treesit--font-lock-fast-mode ;; (setq nodes (treesit--children-covering-range-recurse ;; (car nodes) start end (* 4 jit-lock-chunk-size)))) and evaluate the defun, the benchmark runs much faster again: 11s. (But then I brought it all back, and re-ran the tests, and the variable stayed nil that time around; to sum up: the way it's turned on is unstable.) Should treesit--font-lock-fast-mode be locally bound inside that function, so that it's reset between chunks? Or maybe the condition for its enabling should be tweaked? E.g. I don't think there are any particularly large or deep nodes in ruby.rb's parse tree. It's a very shallow file.