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

From: Ihor Radchenko <[email-masked]>
Date: Sat, 27 Aug 2022 18:37:36 +0800

Eli Zaretskii <[email-masked]> writes:

>> From: Ihor Radchenko <[email-masked]>
>> Cc: [email-masked]
>> Date: Sat, 27 Aug 2022 15:45:19 +0800
>> 
>> I did expect some invisible text to be fontified, but did not expect 10x
>> increase in the requested text size.
>
> The increase depends on how much text is hidden in each chunk,
> relative to jit-lock-chunk-size.  If you have a lot of small shunks of
> hidden text interspersed with visible text, you could have all of the
> invisible text fontified.

This is unfortunate.

>> > One way this could be improved is by making jit-lock-chunk-size
>> > smaller.  This will make the probability of such "over-fontification"
>> > lower.
>> 
>> I tried to reduce the chunk size and at appears to help. However, I am
>> not sure if there are other side effects of reducing
>> jit-lock-chunk-size.
>
> If the size is smaller than the visible portion of the buffer, it will
> cause repeated calls to fontification-functions, instead of just one
> call.  If fontification-functions do their job in approximately linear
> time, you shouldn't see any adverse effects, I think.  But if each
> call to fontification-functions incurs some one-time overhead that is
> relatively expensive, the result could be slower redisplay when the
> window is scrolled a lot.

This may be an issue in some scenarios.
I am wondering if something like font-lock-extend-region-functions may
be utilized to shrink the fontification boundaries.

> I only want to test that if you see something that is inconsistent
> with my description of how this stuff works, including the value of
> jit-lock-chunk-size.

If jit-lock-chunk-size must be an actual upper bound
 "Jit-lock fontifies chunks of at most this many characters at a time."

then it does not always hold:

jit-lock-chunk-size = 1500
|   beg |   end | chunk size |
|  6083 |  6852 |        769 |
|  7181 |  8725 |       1544 |
|  9934 | 11570 |       1636 |
| 11905 | 13418 |       1513 |
| 13481 | 15094 |       1613 |
| 15275 | 16796 |       1521 |
| 16827 | 18389 |       1562 |
| 18728 | 20395 |       1667 |
| 20605 | 22161 |       1556 |
| 22161 | 23795 |       1634 |
#+TBLFM: $3=$2-$1

jit-lock-chunk-size = 150
|   beg |   end | chunk size |
|     1 |   169 |        168 |
|   169 |   319 |        150 |
|   351 |   509 |        158 |
|   532 |   708 |        176 |
|   883 |  1043 |        160 |
|  1142 |  1463 |        321 |
|  1624 |  1780 |        156 |
|  1895 |  2060 |        165 |
|  2060 |  2235 |        175 |
|  2235 |  2395 |        160 |
|  2395 |  2562 |        167 |
|  2677 |  2900 |        223 |
|  3061 |  3294 |        233 |
|  3392 |  3559 |        167 |
|  3657 |  3813 |        156 |
|  3849 |  4021 |        172 |
|  4182 |  4534 |        352 | <- >2x chunk size
|  4743 |  4902 |        159 |
|  4938 |  5097 |        159 |
|  5133 |  5313 |        180 |
|  5313 |  5509 |        196 |
|  5561 |  5751 |        190 |
|  5751 |  5917 |        166 |
|  5917 |  6077 |        160 |
|  6077 |  6237 |        160 |
|  6237 |  6397 |        160 |
|  6460 |  6629 |        169 |
|  7181 |  7364 |        183 |
|  7829 |  7989 |        160 |
|  9934 | 10199 |        265 |
| 10478 | 10638 |        160 |
| 11268 | 11432 |        164 |
| 11905 | 12080 |        175 |
| 12502 | 12683 |        181 |
| 12746 | 12901 |        155 |
| 13000 | 13156 |        156 |
| 13241 | 13418 |        177 |
| 13481 | 13664 |        183 |
| 13716 | 13922 |        206 |
| 14171 | 14336 |        165 |
| 14336 | 14491 |        155 |
| 14576 | 14741 |        165 |
| 15275 | 15495 |        220 |
| 15501 | 15751 |        250 |
| 15877 | 16115 |        238 |
| 16200 | 16366 |        166 |
| 16451 | 16613 |        162 |
| 16827 | 17091 |        264 |
| 18728 | 18946 |        218 |
| 19061 | 19240 |        179 |
| 19416 | 19712 |        296 | <- 2x chunk size
| 19810 | 19970 |        160 |
| 20016 | 20193 |        177 |
| 20245 | 20408 |        163 |
| 20605 | 20770 |        165 |
| 22161 | 22427 |        266 |
| 22427 | 22590 |        163 |
#+TBLFM: $3=$2-$1

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92