The existing implementation has poor performance for large number of keys because it chooses to append the new key to the sorted keys array and sort the new array again. The improvement tries to utilize the sorted keys array by searching the index and doing an insertion if any. Benchmarked on 4-core machine with `go test -cpu 1,2,4,8 -count 5 -benchmem -bench.`(the equal results has been removed): name old time/op new time/op delta MapAddDifferentRandom-8 158ms ± 4% 0ms ± 7% -99.98% (p=0.008 n=5+5) MapSetDifferentRandom-8 154ms ± 5% 0ms ±10% -99.90% (p=0.008 n=5+5) MapAddDifferentRandom-4 25.6ms ±80% 0.0ms ± 9% -99.87% (p=0.008 n=5+5) MapSetDifferentRandom-4 35.0ms ± 2% 0.2ms ±24% -99.56% (p=0.008 n=5+5) MapAddSame-8 641ns ±10% 540ns ± 0% -15.83% (p=0.016 n=5+4) StringSet-8 43.8ns ±14% 37.5ns ± 5% -14.37% (p=0.016 n=5+5) MapAddSame-4 630ns ± 6% 549ns ± 5% -12.77% (p=0.008 n=5+5) IntAdd 9.84ns ± 7% 8.59ns ± 4% -12.73% (p=0.008 n=5+5) MapAddDifferentRandom-2 44.2µs ±15% 39.3µs ± 4% -11.02% (p=0.008 n=5+5) StringSet 47.8ns ±13% 43.6ns ± 2% -8.90% (p=0.008 n=5+5) IntSet-8 17.9ns ± 9% 16.6ns ± 3% -7.38% (p=0.016 n=5+5) RealworldExpvarUsage 44.2µs ± 6% 42.0µs ± 3% -5.04% (p=0.032 n=5+5) MapSetDifferent-4 371ns ± 7% 403ns ± 5% +8.46% (p=0.032 n=5+5) IntAdd-4 17.8ns ± 4% 19.8ns ± 8% +10.87% (p=0.016 n=5+5) MapAddSameSteadyState 42.7ns ± 3% 48.5ns ± 6% +13.44% (p=0.008 n=5+5) MapAddDifferentSteadyState 163ns ± 1% 189ns ± 4% +15.81% (p=0.008 n=5+5) MapSetString-8 105ns ± 6% 129ns ±12% +22.81% (p=0.008 n=5+5) MapSetString-4 109ns ± 8% 140ns ±23% +28.44% (p=0.008 n=5+5) MapSet-4 103ns ± 7% 146ns ±20% +41.88% (p=0.008 n=5+5) name old alloc/op new alloc/op delta MapAddDifferentRandom-4 28.3kB ±79% 0.0kB ±13% -99.92% (p=0.008 n=5+5) MapAddDifferentRandom-8 85.8kB ± 5% 0.1kB ±43% -99.92% (p=0.008 n=5+5) MapSetDifferentRandom-8 107kB ± 4% 32kB ± 0% -69.88% (p=0.008 n=5+5) MapSetDifferentRandom-4 66.0kB ± 0% 32.1kB ± 0% -51.41% (p=0.029 n=4+4) MapAddDifferentRandom 11.0B ± 0% 10.0B ± 0% -9.09% (p=0.008 n=5+5) MapAddSame 480B ± 0% 448B ± 0% -6.67% (p=0.008 n=5+5) MapAddSame-2 480B ± 0% 448B ± 0% -6.67% (p=0.008 n=5+5) MapAddSame-4 480B ± 0% 448B ± 0% -6.67% (p=0.008 n=5+5) MapAddSame-8 480B ± 0% 448B ± 0% -6.67% (p=0.008 n=5+5) name old allocs/op new allocs/op delta MapAddDifferentRandom-4 505 ±80% 0 -100.00% (p=0.008 n=5+5) MapAddDifferentRandom-8 1.35k ± 0% 0.00k -100.00% (p=0.000 n=5+4) MapSetDifferentRandom-8 2.55k ± 0% 2.00k ± 0% -21.42% (p=0.008 n=5+5) MapSetDifferentRandom-4 2.27k ± 0% 2.00k ± 0% -12.00% (p=0.008 n=5+5) MapAddSame 11.0 ± 0% 10.0 ± 0% -9.09% (p=0.008 n=5+5) MapAddSame-2 11.0 ± 0% 10.0 ± 0% -9.09% (p=0.008 n=5+5) MapAddSame-4 11.0 ± 0% 10.0 ± 0% -9.09% (p=0.008 n=5+5) MapAddSame-8 11.0 ± 0% 10.0 ± 0% -9.09% (p=0.008 n=5+5) Fixes golang#31414 |
||
|---|---|---|
| .github | ||
| api | ||
| doc | ||
| lib/time | ||
| misc | ||
| src | ||
| test | ||
| .gitattributes | ||
| .gitignore | ||
| AUTHORS | ||
| CONTRIBUTING.md | ||
| CONTRIBUTORS | ||
| LICENSE | ||
| PATENTS | ||
| README.md | ||
| favicon.ico | ||
| robots.txt | ||
README.md
The Go Programming Language
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Gopher image by Renee French, licensed under Creative Commons 3.0 Attributions license.
Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.
Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.
Download and Install
Binary Distributions
Official binary distributions are available at https://golang.org/dl/.
After downloading a binary release, visit https://golang.org/doc/install or load doc/install.html in your web browser for installation instructions.
Install From Source
If a binary distribution is not available for your combination of operating system and architecture, visit https://golang.org/doc/install/source or load doc/install-source.html in your web browser for source installation instructions.
Contributing
Go is the work of thousands of contributors. We appreciate your help!
To contribute, please read the contribution guidelines: https://golang.org/doc/contribute.html
Note that the Go project uses the issue tracker for bug reports and proposals only. See https://golang.org/wiki/Questions for a list of places to ask questions about the Go language.