Subject: Re: bug#58558: 29.0.50; re-search-forward is slow in some buffers

From: Ihor Radchenko <[email-masked]>
Date: Wed, 12 Apr 2023 13:39:09 +0000

Eli Zaretskii <[email-masked]> writes:

>> Well. I just tried, but my Emacs-C foo is not good enough.
>> The attached patch fails to compile.
>
> That's because you've put DEFVAR_INT outside of any function.  They
> should be inside one of the syms_of_* functions instead.
> regex-emacs.c doesn't have such a function, but search.c does.  So
> just move those DEFVAR_INT lines to syms_of_search, and I think it
> will work.

Thanks!

I now managed to define these variables + also a counter inside
buf_bytepos_to_charpos.

The results are interesting.

The call count for each SYNTAX_TABLE_BYTE_TO_CHAR inside
re_match_2_internal (there are 6 places where it is called):
- master   :: 28 5011460 20 96  285  539911
- Emacs 28 :: 68 5015326 26 397 1404 558585

Master has less calls...

This was weird, so I also added a counter inside buf_bytepos_to_charpos:
- master   :: 6,304,522
- Emacs 28 ::   593,430

Now, it is clear that it is something in SYNTAX_TABLE_BYTE_TO_CHAR that
triggers buf_bytepos_to_charpos more on master compared to Emacs 28.

I looked into the code:

INLINE ptrdiff_t
SYNTAX_TABLE_BYTE_TO_CHAR (ptrdiff_t bytepos)
{
  return (! parse_sexp_lookup_properties
	  ? 0
  ...
}

parse_sexp_lookup_properties looks suspicious, so I checked the value of
parse-sexp-lookup-properties in Org files on master vs. Emacs 28.

On master, the value is t, even though Org mode does not set this
variable. On Emacs 28, the value is nil.

I looked further and narrowed things down to helpful package in my
config, where the culprit is (require 'cc-langs).

It looks like for some reason cc-langs changes the default value of
parse-sexp-lookup-properties globally!

Recipe:

1. emacs -Q
2. M-: (require 'cc-langs) <RET>
3. C-x b asd <RET>
4. M-: parse-sexp-lookup-properties <RET> => t

On Emacs 28, (4) yields nil.

-- 
Ihor Radchenko // yantar92,
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>