-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Reduce allocations #39
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I honestly don't know what the CI error is about 🤔 |
inkel
force-pushed
the
reduce-allocations
branch
from
September 4, 2024 15:14
8db1722
to
a07287a
Compare
@inkel you'll need to amend the commit message(s) to match conventional commit patterns - maybe something like "fix(perf): Reduce allocations" |
inkel
force-pushed
the
reduce-allocations
branch
2 times, most recently
from
September 25, 2024 18:40
c23d5d6
to
483c183
Compare
@hairyhenderson done! |
The reader was being read only once in the first iteration, and afterwards it was always empty, thus `parseCodeowners` was parsing an empty file. These were the metrics before the change: $ go test -benchmem -bench=. goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners BenchmarkParseCodeowners-10 2677503 465.3 ns/op 4096 B/op 1 allocs/op PASS ok github.com/hairyhenderson/go-codeowners 2.255s After the change it's not showing more realistic data: $ go test -benchmem -bench=. goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners BenchmarkParseCodeowners-10 31054 38910 ns/op 61424 B/op 641 allocs/op PASS ok github.com/hairyhenderson/go-codeowners 3.529s
This greatly improves the allocations: $ go test -benchmem -bench=. goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners BenchmarkParseCodeowners-10 70927 16857 ns/op 27211 B/op 242 allocs/op PASS ok github.com/hairyhenderson/go-codeowners 5.297s This reduces the allocations in 62%: $ benchstat main.log precompile.log goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners │ main.log │ precompile.log │ │ sec/op │ sec/op vs base │ ParseCodeowners-10 38.72µ ± 2% 17.63µ ± 6% -54.46% (p=0.000 n=10) │ main.log │ precompile.log │ │ B/op │ B/op vs base │ ParseCodeowners-10 59.95Ki ± 0% 26.56Ki ± 0% -55.70% (p=0.000 n=10) │ main.log │ precompile.log │ │ allocs/op │ allocs/op vs base │ ParseCodeowners-10 641.0 ± 0% 242.0 ± 0% -62.25% (p=0.000 n=10)
These functions are faster than the regexp ones.
hairyhenderson
force-pushed
the
reduce-allocations
branch
from
September 27, 2024 19:20
483c183
to
4d84dbb
Compare
hairyhenderson
approved these changes
Sep 27, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this @inkel!
hairyhenderson
pushed a commit
that referenced
this pull request
Sep 27, 2024
* fix(perf): Fix benchmark The reader was being read only once in the first iteration, and afterwards it was always empty, thus `parseCodeowners` was parsing an empty file. These were the metrics before the change: $ go test -benchmem -bench=. goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners BenchmarkParseCodeowners-10 2677503 465.3 ns/op 4096 B/op 1 allocs/op PASS ok github.com/hairyhenderson/go-codeowners 2.255s After the change it's not showing more realistic data: $ go test -benchmem -bench=. goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners BenchmarkParseCodeowners-10 31054 38910 ns/op 61424 B/op 641 allocs/op PASS ok github.com/hairyhenderson/go-codeowners 3.529s * feat(perf): Precompile getPattern regexps This greatly improves the allocations: $ go test -benchmem -bench=. goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners BenchmarkParseCodeowners-10 70927 16857 ns/op 27211 B/op 242 allocs/op PASS ok github.com/hairyhenderson/go-codeowners 5.297s This reduces the allocations in 62%: $ benchstat main.log precompile.log goos: darwin goarch: arm64 pkg: github.com/hairyhenderson/go-codeowners │ main.log │ precompile.log │ │ sec/op │ sec/op vs base │ ParseCodeowners-10 38.72µ ± 2% 17.63µ ± 6% -54.46% (p=0.000 n=10) │ main.log │ precompile.log │ │ B/op │ B/op vs base │ ParseCodeowners-10 59.95Ki ± 0% 26.56Ki ± 0% -55.70% (p=0.000 n=10) │ main.log │ precompile.log │ │ allocs/op │ allocs/op vs base │ ParseCodeowners-10 641.0 ± 0% 242.0 ± 0% -62.25% (p=0.000 n=10) * feat(perf): Replace regexp replace with strings replace These functions are faster than the regexp ones.
Happy to contribute! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR reduces the allocations in
getPattern
and it also improves the overall speed of the function.First note that I had to fix the existing benchmark, as the reader was being read only once in the first iteration, and afterwards it was always empty, thus
parseCodeowners
was parsing an empty file.These were the metrics before the change:
After the change it's not showing more realistic data:
Then we move out of
getPattern
all regexp compilations, reducing the allocations ~62% and the performance in ~55%. Finally usesstrings.ReplaceAll
instead of the regexp versions when possible. These are the end results: