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.
This commit is contained in:
Lars Ingebrigtsen 2021-09-22 07:22:17 +02:00
parent 7ce721bdac
commit f2bd2386a7

View file

@ -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 */