From a816fe3f62498481500a0ce9695de9fd3aa9f7b7 Mon Sep 17 00:00:00 2001 From: ShiKaiWi Date: Mon, 15 Apr 2019 01:05:16 +0800 Subject: [PATCH] expvar: improve Map.addKey for large number of keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/expvar/expvar_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go index 191fef52e7..5e22d21614 100644 --- a/src/expvar/expvar_test.go +++ b/src/expvar/expvar_test.go @@ -300,9 +300,9 @@ func BenchmarkMapSetDifferent(b *testing.B) { }) } -// BenchmarkMapSetDifferentRandom simulates that the concerned keys of -// Map.Set are generated dynamically and as a result insertion is out -// of order and the number of the keys may be large. +// BenchmarkMapSetDifferentRandom simulates such a case where the concerned +// keys of Map.Set are generated dynamically and as a result insertion is +// out of order and the number of the keys may be large. func BenchmarkMapSetDifferentRandom(b *testing.B) { procKeys := make([][]string, runtime.GOMAXPROCS(0)) for i := range procKeys { @@ -381,9 +381,9 @@ func BenchmarkMapAddDifferent(b *testing.B) { }) } -// BenchmarkMapAddDifferentRandom simulates that the concerned keys of -// Map.Add are generated dynamically and as a result insertion is out -// of order and the number of the keys may be large. +// BenchmarkMapAddDifferentRandom simulates such a case where that the concerned +// keys of Map.Add are generated dynamically and as a result insertion is out of +// order and the number of the keys may be large. func BenchmarkMapAddDifferentRandom(b *testing.B) { procKeys := make([][]string, runtime.GOMAXPROCS(0)) for i := range procKeys {