expvar: use slices to simplify the code

No effect on benchmarks.

Change-Id: I7454c21b25d5e44b9c4a39922ed470522f81872d
GitHub-Last-Rev: 5801b30dac
GitHub-Pull-Request: golang/go#66660
Reviewed-on: https://go-review.googlesource.com/c/go/+/575777
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
apocelipes 2024-04-03 17:47:52 +00:00 committed by Gopher Robot
parent 61a3ee5441
commit 885fdfc082

View file

@ -30,7 +30,7 @@ import (
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"sort" "slices"
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -181,13 +181,11 @@ func (v *Map) addKey(key string) {
v.keysMu.Lock() v.keysMu.Lock()
defer v.keysMu.Unlock() defer v.keysMu.Unlock()
// Using insertion sort to place key into the already-sorted v.keys. // Using insertion sort to place key into the already-sorted v.keys.
if i := sort.SearchStrings(v.keys, key); i >= len(v.keys) { i, found := slices.BinarySearch(v.keys, key)
v.keys = append(v.keys, key) if found {
} else if v.keys[i] != key { return
v.keys = append(v.keys, "")
copy(v.keys[i+1:], v.keys[i:])
v.keys[i] = key
} }
v.keys = slices.Insert(v.keys, i, key)
} }
func (v *Map) Get(key string) Var { func (v *Map) Get(key string) Var {
@ -248,10 +246,9 @@ func (v *Map) AddFloat(key string, delta float64) {
func (v *Map) Delete(key string) { func (v *Map) Delete(key string) {
v.keysMu.Lock() v.keysMu.Lock()
defer v.keysMu.Unlock() defer v.keysMu.Unlock()
i := sort.SearchStrings(v.keys, key) i, found := slices.BinarySearch(v.keys, key)
if i < len(v.keys) && key == v.keys[i] { if found {
v.keys = append(v.keys[:i], v.keys[i+1:]...) v.keys = slices.Delete(v.keys, i, i+1)
v.m.Delete(key)
} }
} }
@ -318,7 +315,7 @@ func Publish(name string, v Var) {
vars.keysMu.Lock() vars.keysMu.Lock()
defer vars.keysMu.Unlock() defer vars.keysMu.Unlock()
vars.keys = append(vars.keys, name) vars.keys = append(vars.keys, name)
sort.Strings(vars.keys) slices.Sort(vars.keys)
} }
// Get retrieves a named exported variable. It returns nil if the name has // Get retrieves a named exported variable. It returns nil if the name has