all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Arash Esbati <arash@gnu.org>
To: emacs-devel@gnu.org
Cc: "Mattias Engdegård" <mattiase@acm.org>
Subject: Re: master 184d6e8c023: Avoid resizing mutation in subst-char-in-string
Date: Sun, 12 May 2024 13:53:14 +0200	[thread overview]
Message-ID: <m2pltrs0ut.fsf@macmutant.fritz.box> (raw)
In-Reply-To: <20240510141519.CE659C1F9CB@vcs2.savannah.gnu.org> ("Mattias Engdegård via Mailing list for Emacs changes"'s message of "Fri, 10 May 2024 10:15:19 -0400 (EDT)")

Mattias Engdegård via Mailing list for Emacs changes <emacs-diffs@gnu.org> writes:

> branch: master
> commit 184d6e8c02345583264b053bb59ae031bb1c5a00
> Author: Mattias Engdegård <mattiase@acm.org>
> Commit: Mattias Engdegård <mattiase@acm.org>
>
>     Avoid resizing mutation in subst-char-in-string
>     
>     * lisp/subr.el (subst-char-in-string):
>     Use string-replace to avoid resizing mutation and O(n^2) time.
> ---
>  lisp/subr.el | 20 +++++++++++++-------
>  1 file changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/lisp/subr.el b/lisp/subr.el
> index 0ac71560c59..444afc0e486 100644
> --- a/lisp/subr.el
> +++ b/lisp/subr.el
> @@ -5690,13 +5690,19 @@ The SEPARATOR regexp defaults to \"\\s-+\"."
>  (defun subst-char-in-string (fromchar tochar string &optional inplace)
>    "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
>  Unless optional argument INPLACE is non-nil, return a new string."
> -  (let ((i (length string))
> -	(newstr (if inplace string (copy-sequence string))))
> -    (while (> i 0)
> -      (setq i (1- i))
> -      (if (eq (aref newstr i) fromchar)
> -	  (aset newstr i tochar)))
> -    newstr))
> +  (if (and (not inplace)
> +           (if (multibyte-string-p string)
> +               (> (max fromchar tochar) 127)
> +             (> tochar 255)))
> +      ;; Avoid quadratic behaviour from resizing replacement.
> +      (string-replace (string fromchar) (string tochar) string)
> +    (let ((i (length string))
> +	  (newstr (if inplace string (copy-sequence string))))
> +      (while (> i 0)
> +        (setq i (1- i))
> +        (if (eq (aref newstr i) fromchar)
> +	    (aset newstr i tochar)))
> +      newstr)))
>  
>  (defun string-replace (from-string to-string in-string)
>    "Replace FROM-STRING with TO-STRING in IN-STRING each time it occurs."

This changes breaks Gnus for me: In the Summary buffer, point moves to
last article in Summary when I hit RET and doesn't shows the article
under point.  Building Emacs with 78761d699 gives me good results.
grep'ing for `subst-char-in-string' in the lisp/gnus gives:

--8<---------------cut here---------------start------------->8---
-> grep subst-char-in-string *.el
gnus-art.el:  (setq url (subst-char-in-string ?+ ?\  url))
gnus-art.el:  (setq url (subst-char-in-string ?_ ?\  url))
gnus-sum.el:  (subst-char-in-string
gnus-sum.el:   (subst-char-in-string ?\n ?\- string t) t))
gnus-sum.el:               (subst-char-in-string
nnheader.el:  (subst-char-in-string from to string))
nnimap.el:	  (insert (format "%S" (subst-char-in-string ?\n ?\s string))))
--8<---------------cut here---------------end--------------->8---

Best, Arash



       reply	other threads:[~2024-05-12 11:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <171535051944.19453.11709867489164614930@vcs2.savannah.gnu.org>
     [not found] ` <20240510141519.CE659C1F9CB@vcs2.savannah.gnu.org>
2024-05-12 11:53   ` Arash Esbati [this message]
2024-05-12 12:16     ` master 184d6e8c023: Avoid resizing mutation in subst-char-in-string Mattias Engdegård
2024-05-12 13:26       ` Mattias Engdegård
2024-05-12 13:53         ` Arash Esbati

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2pltrs0ut.fsf@macmutant.fritz.box \
    --to=arash@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=mattiase@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.