]> git.eshelyaron.com Git - emacs.git/commitdiff
Speed up `replace-match' slightly
authorLars Ingebrigtsen <larsi@gnus.org>
Wed, 22 Sep 2021 05:22:17 +0000 (07:22 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Wed, 22 Sep 2021 05:22:22 +0000 (07:22 +0200)
* 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

index 14adeb58e96b2bcefa8d12bb360d122e5977dd4c..08f1e9474f1739d89f27ad41e1fc42ff7704a741 100644 (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 */