-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(mangling): optimized the processing of initialisms
This PR significantly improves the performance of name mangling utilities (e.g. ToGoName, etc). The need for this occured while benchmarking go-swagger's CI suite: surprisingly, the topmost allocator was swag.ToGoName. It is the result of a dozen successive optimization passes driven by profiling. These functions now execute ~10x faster and need 100 times less memory allocations. See BENCHMARK.md Optimization techniques used to reduce allocations: * pointer -> value (everything was pointers: now everything is values) * interface -> struct with a Kind field * closure -> func * string to []byte: use unsafe conversion * string concatenation -> use bytes.Buffer (recyclable, unlike strings.Builder) * static values converted over and over again: pre-bake the conversions * var x []T -> make([]T, 0, heuristic size) * temporarily allocated values -> use pool to recycle previously allocated data items Optimization techniques used to reduce CPU: * read unicode rune -> short-circuit for single byte runes * map lookup -> func with switch statement * for i, v := range -> for i := range (minor impact) Signed-off-by: Frédéric BIDON <[email protected]>
- Loading branch information
Showing
11 changed files
with
828 additions
and
278 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Benchmarks | ||
|
||
## Name mangling utilities | ||
|
||
```bash | ||
go test -bench XXX -run XXX -benchtime 30s | ||
``` | ||
|
||
### Benchmarks at b3e7a5386f996177e4808f11acb2aa93a0f660df | ||
|
||
``` | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/go-openapi/swag | ||
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz | ||
BenchmarkToXXXName/ToGoName-4 862623 44101 ns/op 10450 B/op 732 allocs/op | ||
BenchmarkToXXXName/ToVarName-4 853656 40728 ns/op 10468 B/op 734 allocs/op | ||
BenchmarkToXXXName/ToFileName-4 1268312 27813 ns/op 9785 B/op 617 allocs/op | ||
BenchmarkToXXXName/ToCommandName-4 1276322 27903 ns/op 9785 B/op 617 allocs/op | ||
BenchmarkToXXXName/ToHumanNameLower-4 895334 40354 ns/op 10472 B/op 731 allocs/op | ||
BenchmarkToXXXName/ToHumanNameTitle-4 882441 40678 ns/op 10566 B/op 749 allocs/op | ||
``` | ||
|
||
### Benchmarks after PR #79 | ||
|
||
~ x10 performance improvement and ~ /100 memory allocations. | ||
|
||
``` | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/go-openapi/swag | ||
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz | ||
BenchmarkToXXXName/ToGoName-4 9595830 3991 ns/op 42 B/op 5 allocs/op | ||
BenchmarkToXXXName/ToVarName-4 9194276 3984 ns/op 62 B/op 7 allocs/op | ||
BenchmarkToXXXName/ToFileName-4 17002711 2123 ns/op 147 B/op 7 allocs/op | ||
BenchmarkToXXXName/ToCommandName-4 16772926 2111 ns/op 147 B/op 7 allocs/op | ||
BenchmarkToXXXName/ToHumanNameLower-4 9788331 3749 ns/op 92 B/op 6 allocs/op | ||
BenchmarkToXXXName/ToHumanNameTitle-4 9188260 3941 ns/op 104 B/op 6 allocs/op | ||
``` | ||
|
||
``` | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/go-openapi/swag | ||
cpu: AMD Ryzen 7 5800X 8-Core Processor | ||
BenchmarkToXXXName/ToGoName-16 18527378 1972 ns/op 42 B/op 5 allocs/op | ||
BenchmarkToXXXName/ToVarName-16 15552692 2093 ns/op 62 B/op 7 allocs/op | ||
BenchmarkToXXXName/ToFileName-16 32161176 1117 ns/op 147 B/op 7 allocs/op | ||
BenchmarkToXXXName/ToCommandName-16 32256634 1137 ns/op 147 B/op 7 allocs/op | ||
BenchmarkToXXXName/ToHumanNameLower-16 18599661 1946 ns/op 92 B/op 6 allocs/op | ||
BenchmarkToXXXName/ToHumanNameTitle-16 17581353 2054 ns/op 105 B/op 6 allocs/op | ||
``` |
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
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
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
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
Oops, something went wrong.