1
0
mirror of https://github.com/golang/go synced 2024-07-08 12:18:55 +00:00

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"
"os"
"runtime"
"sort"
"slices"
"strconv"
"sync"
"sync/atomic"
@ -181,13 +181,11 @@ func (v *Map) addKey(key string) {
v.keysMu.Lock()
defer v.keysMu.Unlock()
// Using insertion sort to place key into the already-sorted v.keys.
if i := sort.SearchStrings(v.keys, key); i >= len(v.keys) {
v.keys = append(v.keys, key)
} else if v.keys[i] != key {
v.keys = append(v.keys, "")
copy(v.keys[i+1:], v.keys[i:])
v.keys[i] = key
i, found := slices.BinarySearch(v.keys, key)
if found {
return
}
v.keys = slices.Insert(v.keys, i, key)
}
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) {
v.keysMu.Lock()
defer v.keysMu.Unlock()
i := sort.SearchStrings(v.keys, key)
if i < len(v.keys) && key == v.keys[i] {
v.keys = append(v.keys[:i], v.keys[i+1:]...)
v.m.Delete(key)
i, found := slices.BinarySearch(v.keys, key)
if found {
v.keys = slices.Delete(v.keys, i, i+1)
}
}
@ -318,7 +315,7 @@ func Publish(name string, v Var) {
vars.keysMu.Lock()
defer vars.keysMu.Unlock()
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