From f2bd2386a79fed311361fd9ca809ab1b2a0362a6 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Wed, 22 Sep 2021 07:22:17 +0200 Subject: [PATCH] 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. --- src/search.c | 7 +++++++ 1 file changed, 7 insertions(+) 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 */ -- 2.39.5