Skip to content

Commit a816fe3

Browse files
committed
expvar: improve Map.addKey for large number of keys
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 #31414
1 parent a33b076 commit a816fe3

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/expvar/expvar_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ func BenchmarkMapSetDifferent(b *testing.B) {
300300
})
301301
}
302302

303-
// BenchmarkMapSetDifferentRandom simulates that the concerned keys of
304-
// Map.Set are generated dynamically and as a result insertion is out
305-
// of order and the number of the keys may be large.
303+
// BenchmarkMapSetDifferentRandom simulates such a case where the concerned
304+
// keys of Map.Set are generated dynamically and as a result insertion is
305+
// out of order and the number of the keys may be large.
306306
func BenchmarkMapSetDifferentRandom(b *testing.B) {
307307
procKeys := make([][]string, runtime.GOMAXPROCS(0))
308308
for i := range procKeys {
@@ -381,9 +381,9 @@ func BenchmarkMapAddDifferent(b *testing.B) {
381381
})
382382
}
383383

384-
// BenchmarkMapAddDifferentRandom simulates that the concerned keys of
385-
// Map.Add are generated dynamically and as a result insertion is out
386-
// of order and the number of the keys may be large.
384+
// BenchmarkMapAddDifferentRandom simulates such a case where that the concerned
385+
// keys of Map.Add are generated dynamically and as a result insertion is out of
386+
// order and the number of the keys may be large.
387387
func BenchmarkMapAddDifferentRandom(b *testing.B) {
388388
procKeys := make([][]string, runtime.GOMAXPROCS(0))
389389
for i := range procKeys {

0 commit comments

Comments
 (0)