Skip to content
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

perf: cli: avoid allocations with (*regexp.Regexp).MatchString #11499

Closed
wants to merge 1 commit into from
Closed

perf: cli: avoid allocations with (*regexp.Regexp).MatchString #11499

wants to merge 1 commit into from

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Dec 7, 2023

Related Issues

Proposed Changes

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

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

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

Result:

goos: linux
goarch: amd64
pkg: github.com/filecoin-project/lotus/cli/util
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 2077356	       692.3 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 2970835	       415.6 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/filecoin-project/lotus/cli/util	3.704s

Additional Info

Checklist

Before you mark the PR ready for review, please make sure that:

  • Commits have a clear commit message.
  • PR title is in the form of of <PR type>: <area>: <change being made>
    • example: fix: mempool: Introduce a cache for valid signatures
    • PR type: fix, feat, build, chore, ci, docs, perf, refactor, revert, style, test
    • area, e.g. api, chain, state, market, mempool, multisig, networking, paych, proving, sealing, wallet, deps
  • If the PR affects users (e.g., new feature, bug fix, system requirements change), update the CHANGELOG.md and add details to the UNRELEASED section.
  • New features have usage guidelines and / or documentation updates in
  • Tests exist for new functionality or change in behavior
  • CI is green

We should use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` when matching string to avoid
unnecessary `[]byte` conversions and reduce allocations.

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

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

goos: linux
goarch: amd64
pkg: github.com/filecoin-project/lotus/cli/util
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 2077356	       692.3 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 2970835	       415.6 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/filecoin-project/lotus/cli/util	3.704s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@Juneezee Juneezee requested a review from a team as a code owner December 7, 2023 15:38
@Juneezee Juneezee changed the title cli/util: avoid allocations with (*regexp.Regexp).MatchString perf: cli: avoid allocations with (*regexp.Regexp).MatchString Dec 7, 2023
@magik6k
Copy link
Contributor

magik6k commented Dec 7, 2023

Please read the contributing guide

Please refrain from submitting PRs to adapt existing code to subjective preferences. The changeset should contain functional or technical improvements/enhancements, bug fixes, new features, or some other clear material contribution. Simple stylistic changes are likely to be rejected in order to reduce code churn.

Minimize code churn. Modify only what is strictly necessary..

@magik6k magik6k closed this Dec 7, 2023
@Juneezee
Copy link
Contributor Author

Juneezee commented Dec 7, 2023

Please read the contributing guide

@magik6k This is an enhancement and not a subjective and stylistic change.

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.

2 participants