miller/pkg
Eng Zer Jun 3966a6a0a1
lib/regex: use string version of regexp methods to reduce allocs (#1614)
Both `(*Regexp).Match` and `(*Regexp).FindAllSubmatchIndex` have
string-based equivalents: `(*Regexp).MatchString` and
`(*Regexp).FindAllStringSubmatchIndex`. We should use the string version
to avoid unnecessary `[]byte` conversions.

Benchmark:

var regex = regexp.MustCompile("foo.*")

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := regex.Match([]byte("foo bar baz")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := regex.MatchString("foo bar baz"); !match {
			b.Fail()
		}
	}
}

func BenchmarkFindAllSubmatchIndex(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := regex.FindAllSubmatchIndex([]byte("foo bar baz"), -1); len(match) == 0 {
			b.Fail()
		}
	}
}

func BenchmarkFindAllStringSubmatchIndex(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := regex.FindAllStringSubmatchIndex("foo bar baz", -1); len(match) == 0 {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/johnkerl/miller/pkg/lib
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16                         	 2198350	       517.5 ns/op	      16 B/op	       1 allocs/op
BenchmarkMatchString-16                   	 3143605	       371.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkFindAllSubmatchIndex-16          	  921711	      1199 ns/op	     273 B/op	       3 allocs/op
BenchmarkFindAllStringSubmatchIndex-16    	 1212321	       981.0 ns/op	     257 B/op	       2 allocs/op
PASS
coverage: 0.0% of statements
ok  	github.com/johnkerl/miller/pkg/lib	6.576s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2024-08-09 13:09:53 -04:00
..
auxents Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
bifs Add a stat DSL function (#1560) 2024-05-09 18:39:44 -04:00
cli Be smarter about auto-unflatten (#1584) 2024-06-08 20:58:26 -04:00
climain Be smarter about auto-unflatten (#1584) 2024-06-08 20:58:26 -04:00
colorizer Support $NO_COLOR (#1580) 2024-06-08 13:08:15 -04:00
dsl Add a stat DSL function (#1560) 2024-05-09 18:39:44 -04:00
entrypoint Replace deprecated io/ioutil functions (#1452) 2023-12-20 09:44:02 -05:00
go-csv Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
input Avoid spurious [] on JSON output in some cases (#1528) 2024-03-16 17:00:59 -04:00
lib lib/regex: use string version of regexp methods to reduce allocs (#1614) 2024-08-09 13:09:53 -04:00
mlrval Add a stat DSL function (#1560) 2024-05-09 18:39:44 -04:00
output Fix typo in online help for --no-jlistwrap (#1541) 2024-04-11 08:12:45 -04:00
parsing Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
pbnjay-strptime Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
platform Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
runtime Preserve regex captures across stack frames (#1447) 2023-12-18 10:21:09 -05:00
scan Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
stream Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00
terminals Be smarter about auto-unflatten (#1584) 2024-06-08 20:58:26 -04:00
transformers Bash process substitution not working with put -f (#1583) 2024-06-08 20:37:31 -04:00
types Avoid spurious [] on JSON output in some cases (#1528) 2024-03-16 17:00:59 -04:00
version 6.12.0 doc link 2024-03-16 17:51:17 -04:00
README.md Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00

Please see ../../README-dev.md for an overview; please see each subdirectory for details about it.