8000 Avoid allocations with `(*regexp.Regexp).MatchString` by Juneezee · Pull Request #245 · getanteon/anteon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Avoid allocatio 8000 ns with (*regexp.Regexp).MatchString #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Nov 7, 2023
Merged

Avoid allocations with (*regexp.Regexp).MatchString #245

merged 35 commits into from
Nov 7, 2023

Conversation

Juneezee
Copy link
Contributor
@Juneezee Juneezee commented Nov 6, 2023

Pull Request Template

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • This change requires a documentation update

Description

We should use (*regexp.Regexp).MatchString instead of (*regexp.Regexp).Match([]byte(...)) when matching string to avoid unnecessary []byte conversions and reduce allocations. A one-line change for free performance improvement.

Example benchmark:

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

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

Result:

goos: linux
goarch: amd64
pkg: go.ddosify.com/ddosify/core/types
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 4542174	       277.2 ns/op	       8 B/op	       1 allocs/op
BenchmarkMatchString-16    	 7210927	       172.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	go.ddosify.com/ddosify/core/types	3.908s

Related Issue(s)

Checklist

  • I have read the CONTRIBUTING.md document.
  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the README.md as necessary if there are changes.
  • I have tested the changes on my local machine before submitting the PR.

Screenshots

fatihbaltaci and others added 30 commits April 18, 2023 15:42
Performance Improvement on Multipart
Response Duration & Cookies & Fixes & Remote URL Payload
fix invalid capacity bug for distinct user mode
Add helm chart k8s install info to readme
Update docker compose for self hosted
Update self hosted install script
Update self hosted install script
Check status code for remote file on multipart
Update self hosted app versions
Update Ddosify Self Hosted - Hammer 1.4.0
override req host from header
Update self hosted install command
Update prometheus scrape interval
Update Ddosify Self Hosted
Update Ddosify Self Hosted
Update ddosify self hosted
Update self hosted frontend
Update icon selection dark / light
fatihbaltaci and others added 5 commits October 19, 2023 21:24
Update self hosted frontend and backend
Update self hosted frontend and backend
Update self hosted backend
We should use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` when matching string to avoid
unnecessary `[]byte` conversions and reduce allocations.

Example benchmark:

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

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

goos: linux
goarch: amd64
pkg: go.ddosify.com/ddosify/core/types
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 4542174	       277.2 ns/op	       8 B/op	       1 allocs/op
BenchmarkMatchString-16    	 7210927	       172.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	go.ddosify.com/ddosify/core/types	3.908s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@ddosifyadmin
Copy link

Max. Delta Time op: 2.71 / 15
Max. Delta Alloc op: 27.19 / 15
Max. Delta Allocs op: 22.56 / 15

@fatihbaltaci fatihbaltaci changed the base branch from master to develop November 7, 2023 11:48
@fatihbaltaci
Copy link
Member

Hey @Juneezee, thanks for your contribution

@fatihbaltaci fatihbaltaci merged commit ffc945f into getanteon:develop Nov 7, 2023
@ddosifyadmin
Copy link

Max. Delta Time op: 3.07 / 15
Max. Delta Alloc op: 2.33 / 15
Max. Delta Allocs op: 2.66 / 15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0