From: Lars Ingebrigtsen Date: Wed, 22 Sep 2021 05:22:17 +0000 (+0200) Subject: Speed up `replace-match' slightly X-Git-Tag: emacs-28.0.90~747 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=f2bd2386a79fed311361fd9ca809ab1b2a0362a6;p=emacs.git Speed up `replace-match' slightly * src/search.c (Freplace_match): Speed up non-literal (but actually literal) common case. This makes (benchmark-run 1000000 (replace-regexp-in-string "a+" "foo" "ogihdipofdhookfdohkfdpokhpokhfdpokfdhpokfdhkdfkhgoadfphokfkhpofdkhkdpokf")) about 10% faster. --- diff --git a/src/search.c b/src/search.c index 14adeb58e96..08f1e9474f1 100644 --- a/src/search.c +++ b/src/search.c @@ -2387,6 +2387,13 @@ since only regular expressions have distinguished subexpressions. */) if (! NILP (string)) CHECK_STRING (string); + /* Most replacement texts don't contain any backslash directives in + the replacements. Check whether that's the case, which will + enable us to take the fast path later. */ + if (NILP (literal) + && !memchr (SSDATA (newtext), '\\', SBYTES (newtext))) + literal = Qt; + case_action = nochange; /* We tried an initialization */ /* but some C compilers blew it */