Subject: bug#57447: 28.1.90; Can font-lock stop requesting fontification of invisible text?

From: Eli Zaretskii <[email-masked]>
Date: Sat, 27 Aug 2022 10:02:39 +0300

> From: Ihor Radchenko <[email-masked]>
> Date: Sat, 27 Aug 2022 13:34:57 +0800
> 
> Org mode tends to hide large chunks of text under folded headlines.
> I expect this hidden text to be ignored by the font-lock engine.
> 
> However, in reality, there appear to be some scenarios when hidden text
> is, in fact, being requested for fontification.
> 
> I have an Org file with a number of folded headlines. 32 of them can fit
> into one screen of text on my system, when folded. The total character
> count contained within the folded headlines (both visible and inside the
> 32 hidden text chunks) is 12742. However, most of this text is folded, and only 2446 characters are visible in the buffer.
> 
> I expect font-lock to fontify around 2446 characters (32 lines), but
> Emacs instead requests fontification of 9374 characters (315 lines; 10
> screens of text) at once; ~1.5k chars for each folded text region.
> 
> Fontification of 10 screens of text is obviously 10x slower and can
> create noticeable feedback slowdowns.
> 
> Can anything be done to make Emacs fontify less (or none) of the hidden
> text?

I explained that recently, in a discussion in which you participated:
the display engine skips invisible text, but that cannot completely
avoid fontifications of some of the invisible text.

The technical reason for that is that the display engine considers the
'fontified' property before it considers the 'invisible' property (and
any other properties).  Since fontification-functions put text
properties on buffer text, the 'fontified' property _must_ be examined
first, and the fontification-functions _must_ be invoked _before_ the
properties are examined; otherwise we'd risk behaving incorrectly due
to outdated or not-yet-existing properties.

One way this could be improved is by making jit-lock-chunk-size
smaller.  This will make the probability of such "over-fontification"
lower.

If a mode _knows_ that no fontifications could ever produce invisible
properties or overlays, then its fontification-functions could stop
whenever they encounter invisible text.

> The fontification log is:
> 
> org-font-lock: About to fontify 13481..15094
> org-font-lock: Fontified drawer(13372..13487) completely.

Thanks, but this log is insufficient to analyze whether the code
functions correctly.  To do such an analysis one must know which text
parts (in therms of buffer positions) were invisible and which
weren't.  Also, you haven't shown the code which produces the log, so
the exact meanings of "about to fontify", "fontfied drawer", and the
rest of the log messages, as well as the numerical notations like
"13487:+235", are unclear.