]> Cypherpunks repositories - gostls13.git/commitdiff
expvar: use slices to simplify the code
authorapocelipes <seve3r@outlook.com>
Wed, 3 Apr 2024 17:47:52 +0000 (17:47 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 3 Apr 2024 21:36:09 +0000 (21:36 +0000)
No effect on benchmarks.

Change-Id: I7454c21b25d5e44b9c4a39922ed470522f81872d
GitHub-Last-Rev: 5801b30dac33712c2e3d282b2d3a9fbe04779b2e
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>

src/expvar/expvar.go

index 5e9034e5d7a976462dab7861763c1a460dca3000..a30963c5a8aac8f9d20b34c215fbc953cc63bfe2 100644 (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