Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: bef2808f4db21ed58331af48a14ce9bda14e17d7
      
https://github.com/WebKit/WebKit/commit/bef2808f4db21ed58331af48a14ce9bda14e17d7
  Author: Sosuke Suzuki <[email protected]>
  Date:   2026-07-07 (Tue, 07 Jul 2026)

  Changed paths:
    A JSTests/microbenchmarks/string-replace-regexp-global-substitution.js
    M Source/JavaScriptCore/runtime/StringPrototypeInlines.h

  Log Message:
  -----------
  [JSC] Parse the replacement template only once in `String#replace` with a 
global RegExp and `'$'` substitutions
https://bugs.webkit.org/show_bug.cgi?id=318418

Reviewed by Yusuke Suzuki.

When the replacement string of a global RegExp replace contains '$',
replaceAllWithStringUsingRegExpSearch re-parsed the replacement template
and allocated an intermediate string for every match, and then copied
everything again when joining the result.

This patch parses the template into parts once per replace() call and
appends each match's substitution directly into a single StringBuilder.

When there is only one match, the result is built as a rope sharing the
source around the substitution, preserving the existing fast path for large
sources.

                                                   baseline                  
patched

string-replace-regexp-global-substitution
                                              258.8735+-0.9881     ^    
122.2286+-2.0821        ^ definitely 2.1179x faster

Test: JSTests/microbenchmarks/string-replace-regexp-global-substitution.js

* JSTests/microbenchmarks/string-replace-regexp-global-substitution.js: Added.
(test):
* Source/JavaScriptCore/runtime/StringPrototypeInlines.h:
(JSC::StringReplaceTemplatePart::literal):
(JSC::StringReplaceTemplatePart::capture):
(JSC::StringReplaceTemplatePart::matchPrefix):
(JSC::StringReplaceTemplatePart::matchSuffix):
(JSC::StringReplaceTemplatePart::namedCapture):
(JSC::parseReplacementTemplate):
(JSC::appendReplacementUsingTemplate):
(JSC::replaceAllWithStringUsingRegExpSearch):

Canonical link: https://commits.webkit.org/316694@main



To unsubscribe from these emails, change your notification settings at 
https://github.com/WebKit/WebKit/settings/notifications

Reply via email to