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#56682: Fix the long lines font locking related slowdowns Date: Fri, 5 Aug 2022 22:01:24 +0300 Message-ID: <25717d84-3411-a93a-3620-e04fe0571aff@yandex.ru> References: <837d46mjen.fsf@gnu.org> <8a3eaeef01be5bfaa5ef@heytings.org> <05388e8d8812bfa3695d@heytings.org> <83v8rf5894.fsf@gnu.org> <65cb7c73fd4a999cca00@heytings.org> <8c7321f2f3400a5db9be@heytings.org> <8c7321f2f388e5343475@heytings.org> <8c7321f2f36494299e61@heytings.org> <83v8rc2n1h.fsf@gnu.org> <64084296-1953-8ef8-5938-adfb6fb9b43f@yandex.ru> <83r11uzs8n.fsf@gnu.org> <14845631-c2ef-8371-8606-c858092e3192@yandex.ru> <83mtcizov2.fsf@gnu.org> <83h72qzheq.fsf@gnu.org> 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="9968"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Cc: 56682@debbugs.gnu.org, gregory@heytings.org, monnier@iro.umontreal.ca To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 05 21:02:11 2022 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 1oK2aJ-0002PV-8u for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 05 Aug 2022 21:02:11 +0200 Original-Received: from localhost ([::1]:50656 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oK2aI-0002qk-2Z for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 05 Aug 2022 15:02:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56706) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK2aA-0002qY-Jm for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 15:02:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:42621) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oK2aA-0005NK-81 for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 15:02:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oK2aA-0007OK-0S for bug-gnu-emacs@gnu.org; Fri, 05 Aug 2022 15:02:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Dmitry Gutov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 05 Aug 2022 19:02:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56682 X-GNU-PR-Package: emacs Original-Received: via spool by 56682-submit@debbugs.gnu.org id=B56682.165972609928383 (code B ref 56682); Fri, 05 Aug 2022 19:02:01 +0000 Original-Received: (at 56682) by debbugs.gnu.org; 5 Aug 2022 19:01:39 +0000 Original-Received: from localhost ([127.0.0.1]:60603 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oK2Zm-0007Ni-KK for submit@debbugs.gnu.org; Fri, 05 Aug 2022 15:01:39 -0400 Original-Received: from mail-wm1-f47.google.com ([209.85.128.47]:55084) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oK2Zj-0007NS-TH for 56682@debbugs.gnu.org; Fri, 05 Aug 2022 15:01:37 -0400 Original-Received: by mail-wm1-f47.google.com with SMTP id n185so1802954wmn.4 for <56682@debbugs.gnu.org>; Fri, 05 Aug 2022 12:01:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=ctPHNiZnRxukhAj4rkfmudEw5yyHtco8nl5Lv4s6LuY=; b=fi+tlYMWzdQGXLnpk/zndoa4wGbSEaS1kgbRfYRcxve5NtLGECp9H/4ZyKv9BFmsAE 0Bfyx0U2YFYcJwM3r4kE6r9TZRsTziGm80Iu7dG3aPI6h0g47SIfWXtRMiOT6XPUiMpp 6BBGmirEszyEBZmqtCuumvTaRLjVhnoEiT/wU2Yuu/jlyXoFI/EbYrjA8bWQaLMHchGk zciIAfqpxO5zhCk/wXeNZO6aFQR64y01YLEaJTA78P37MeOi3cxjXyeUfSW1iSMwe0Ij gti2+s0xQyQX3orTTILmI6NwM6Sl5EnNBvJxNwJ9z1ufppuV95qaHwLj+3KZDMyIxyG9 y/vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:message-id:date:mime-version:user-agent :subject:content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=ctPHNiZnRxukhAj4rkfmudEw5yyHtco8nl5Lv4s6LuY=; b=Ft6ir0b/na9Kg/W3IpGnUeAv+zCHBlTxVmJPEzbh9egjO7/XZQxR9XkAOCfkok4T12 /TJVR3/2IHSB0ko3GlWWHfx6fJiNlA0d7DLfw5qd25nFIVpUIVTFzERWtKTQObbs5D/g W3ZW/Cqk3tgSO6LFMd/V2p8KVU15NWUXO3S7eX8OrWfejcSms856tat4bNOZzEOx11ZW h7KrycgfcCa8nSChZAL/Ft/UgGM9rA+GP5Cb1f5gpCx/MKk+FO2n4kffPnD+LmqBvKEB 8aSq3js9OIro8lmCqPoxNHLQW5cAivcNQB/LkF34YJ6UOfXdsLkxhz6U75a96XjCTvuE GSEQ== X-Gm-Message-State: ACgBeo3f9+h8d+1pkrq17wpGJj4xeqWVwqAwrKlZfBWav5H2AIRBE6/0 9zXffpe5kX7G/jshh6Kbql4= X-Google-Smtp-Source: AA6agR41KnJi4snWZkIO0Cs2irEEQGC6S3tzHHnvCMyeMych9t3WeZdUYmcmA+1osli5BX4Lg6pbRA== X-Received: by 2002:a05:600c:4f81:b0:3a3:1f5a:2b6a with SMTP id n1-20020a05600c4f8100b003a31f5a2b6amr5480727wmq.53.1659726089989; Fri, 05 Aug 2022 12:01:29 -0700 (PDT) Original-Received: from [192.168.0.6] ([46.251.119.176]) by smtp.googlemail.com with ESMTPSA id h14-20020a5d430e000000b002217540b222sm2119297wrq.58.2022.08.05.12.01.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 05 Aug 2022 12:01:27 -0700 (PDT) Content-Language: en-US In-Reply-To: <83h72qzheq.fsf@gnu.org> 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" Xref: news.gmane.io gmane.emacs.bugs:238939 Archived-At: On 05.08.2022 21:14, Eli Zaretskii wrote: >> Date: Fri, 5 Aug 2022 21:02:35 +0300 >> Cc: 56682@debbugs.gnu.org, gregory@heytings.org, monnier@iro.umontreal.ca >> From: Dmitry Gutov >> >>> How is (parse-partial-sexp 1 (point-max)) related to the issue at >>> hand? >> >> Or perhaps I should answer this way: >> >> We move to near EOB. >> fontification-functions are called. >> >> jit-lock >> calls >> (font-lock-fontify-region point-near-buffer-end (point-max)) >> which calls >> font-lock-fontify-syntactically-region >> which calls both >> (syntax-propertize (point-max)) >> and >> (syntax-ppss point-near-buffer-end) -> and it calls parse-partial-sexp >> >> syntax-propertize will also likely call syntax-ppss itself, probably >> through the major mode's syntax-propertize-function. But if >> syntax-propertize-function is nil, parse-partial-sexp gets called >> anyway, over the whole buffer, which makes it the main workload in >> fontifying near EOB. >> >> Now, if syntax-propertize-function is non-nil, parse-partial-sexp will >> also call it, and it adds its overhead (sometimes a multiple of p-p-s), >> which also scales linearly with the length of the buffer. >> >> So if one can demonstrate that (parse-partial-sexp (point-min) >> (point-max)) takes about the same time as it takes to fontify the last >> screen-ful of a buffer, then that says that everything else that >> jit-lock does to fontify, is negligible, time-wise. > > So you have demonstrated that, if visiting a file and moving inside it > calls parse-partial-sexp to scan the entire buffer, then this could be > some, perhaps a large, part of the slowdown. Yes. > First, we need to establish that indeed parse-partial-sexp is called > in that manner in the relevant major modes (not just one of them), or > by font-lock itself regardless of the mode. It is called by font-lock itself, which ends up calling syntax-ppss, which does its job with parse-partial-sexp. I have outlined the chain of calls in the previous message, you can verify it by looking at the sources. > Second, we need to establish that indeed this takes a large portion of > the time in the slow operations. Not just one particular operation, > but most or all of them. To establish that, I have described the experiment in the grandparent email (with scenarios 1,2a;1,2b;1,2a,2b), and performed it myself as well. But I'm talking about the slowdown observed when doing 'M->'. Not about any operations one might try to perform. Having said that, after the initial 'M->' most of navigation operations look snappy to me. So that's the slowdown I decided to investigate. > And after that, we may have some food for thought. Here's some more: All major modes we can currently use for JSON (the built-in js-mode and the two json-mode's in ELPA) inherit the value of syntax-propertize-function from js-mode. But there's no need for it: JSON doesn't have division, or regexps, or preprocessor directives, or embedded JSX structures. Setting syntax-propertize-function to nil speeds up parse-partial-sexp significantly. Here's a patch you can try to evaluate the effect on dictionary.json of that change combined with the previous tweak I suggested. Now it takes about 5x faster to fontify the last screenful, on my machine. Meaning, 'M->' feels almost (but not quite) instant. And the fontification is still correct. A "proper" change would involve creating a new major mode, probably, rather than regexp-matching against buffer-file-name. But I'm not sure what name to pick: 'json-mode' would step on the toes of two existing packages now. 'js-json-mode', maybe? Or we bring in json-mode from GNU ELPA (with a similar change). Anyway, try this please: diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index eb2a1e4fcc..ae8e980125 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3418,7 +3418,8 @@ js-mode (list js--font-lock-keywords nil nil nil nil '(font-lock-syntactic-face-function . js-font-lock-syntactic-face-function))) - (setq-local syntax-propertize-function #'js-syntax-propertize) + (unless (and buffer-file-name (string-match-p "\\.json\\'" buffer-file-name)) + (setq-local syntax-propertize-function #'js-syntax-propertize)) (add-hook 'syntax-propertize-extend-region-functions #'syntax-propertize-multiline 'append 'local) (add-hook 'syntax-propertize-extend-region-functions diff --git a/src/xdisp.c b/src/xdisp.c index 099efed2db..fcb2be8768 100644 --- a/src/xdisp.c +++ b/src/xdisp.c @@ -4391,20 +4391,6 @@ handle_fontified_prop (struct it *it) eassert (it->end_charpos == ZV); - if (current_buffer->long_line_optimizations_p) - { - ptrdiff_t begv = it->narrowed_begv; - ptrdiff_t zv = it->narrowed_zv; - ptrdiff_t charpos = IT_CHARPOS (*it); - if (charpos < begv || charpos > zv) - { - begv = get_narrowed_begv (it->w, charpos); - zv = get_narrowed_zv (it->w, charpos); - } - narrow_to_region_internal (make_fixnum (begv), make_fixnum (zv), true); - specbind (Qrestrictions_locked, Qt); - } - /* Don't allow Lisp that runs from 'fontification-functions' clear our face and image caches behind our back. */ it->f->inhibit_clear_image_cache = true;