Subject: Re: bug#64391: buffer narrowing slowdown regression in emacs 29

From: Mattias Engdegård <[email-masked]>
Date: Sat, 1 Jul 2023 14:52:58 +0200

1 juli 2023 kl. 14.08 skrev Eli Zaretskii <[email-masked]>:

>> The attached patch combines narrow-to-region and internal--label-restriction into a single function, internal--narrow-to-region. (We could also add the label as an optional argument to narrow-to-region.)
> 
> It does more than that, so I'd appreciate a more detailed description
> of the changes and their rationale.

Actually that's just what it does. Here is a tentative commit message:

    Fix severe narrowing performance bug (regression from emacs 28)
    
    In Emacs 29, `narrow-to-region` conses and creates markers
    unconditionally on the offchance that a call to
    `internal--label-restriction` would need it, which is only rarely the
    case (in `with-restriction` with a :label argument).
    
    As a remedy we fuse the two functions to one,
    `internal--narrow-to-region`, and only perform the costly consing and
    marker creation for labelled narrowing. (Bug#64391)
    
    * lisp/subr.el (internal--with-restriction):
    * src/editfns.c (labeled_narrow_to_region):
    Call `internal--narrow-to-region` instead of `narrow-to-region`
    followed by `internal--label-restriction`.
    (Fwiden): Remove assignment to eliminated `outermost-restriction`.
    (Fnarrow_to_region): Reduce to a stub that calls the original
    function, now named...
    (Finternal__narrow_to_region): ...this, which takes an added
    `label` argument and includes at the end the optimised body of...
    (Finternal__label_restriction): ...this function, now removed
    since it has been entirely absorbed.
    (syms_of_editfns): Remove the `outermost-restriction` buffer-local
    variable as it was only used to convey data from `narrow-to-region`
    to `internal--label-restriction` called immediately afterwards.

And again, if anyone would prefer an optional `label` argument to `narrow-to-region` then that would be fine too. It depends a little on whether we want to expose that functionality to the user in that function, or as now keep it in `with-restriction` only.