Skip to content

regexp: avoid allocating string copy if ReplaceAllString is a no-op #80280

Description

@nicholasngai

Go version

go version go1.26.3 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/nngai/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/nngai/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/zk/r_k5nrwj4ks2_trs76z1fw2c0000gq/T/go-build539003837=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/nngai/git/test/go.mod'
GOMODCACHE='/Users/nngai/go/pkg/mod'
GONOPROXY=''
GONOSUMDB='='
GOOS='darwin'
GOPATH='/Users/nngai/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.26.3/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/nngai/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.26.3/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.26.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Simple benchmark which compares strings.ReplaceAll and (*regexp.Regexp).ReplaceAllString, with both a real replacement and a no-op replacement.

package test

import (
	"regexp"
	"strings"
	"testing"
)

func BenchmarkRegexpReplace(b *testing.B) {
	s := "foobar"
	re := regexp.MustCompile(`fizz`)
	for b.Loop() {
		_ = re.ReplaceAllString(s, "buzz")
	}
}

func BenchmarkRegexpReplaceNoop(b *testing.B) {
	s := "foobar"
	re := regexp.MustCompile(`foo`)
	for b.Loop() {
		_ = re.ReplaceAllString(s, "bar")
	}
}

func BenchmarkStringsReplace(b *testing.B) {
	s := "foobar"
	for b.Loop() {
		_ = strings.ReplaceAll(s, "foo", "bar")
	}
}

func BenchmarkStringsReplaceNoop(b *testing.B) {
	s := "foobar"
	for b.Loop() {
		_ = strings.ReplaceAll(s, "fizz", "buzz")
	}
}

What did you see happen?

BenchmarkRegexpReplaceNoop still uses allocations on a new string, even though the replacement is a no-op:

goos: darwin
goarch: arm64
pkg: test
cpu: Apple M2 Max
BenchmarkRegexpReplace-12        	16321080	        66.28 ns/op	      32 B/op	       3 allocs/op
BenchmarkRegexpReplaceNoop-12    	10023492	       119.4 ns/op	      32 B/op	       3 allocs/op
BenchmarkStringsReplace-12        	33732698	        34.34 ns/op	       8 B/op	       1 allocs/op
BenchmarkStringsReplaceNoop-12    	132301630	         9.081 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	test 5.204s

What did you expect to see?

It would be nice if (*regexp.Regexp).ReplaceAllString could provide parity with strings.ReplaceAll where, if there is no match, we return the input string rather than allocating a new one off the heap.

A native implementation could just call Match to see if there is a match before proceeding with a replacement. I’m sure you could find a more efficient way of doing it though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions