Skip to content

Commit

Permalink
all: make use of sync.Map.Clear
Browse files Browse the repository at this point in the history
Since CL 515015 added sync.Map.Clear method, we can use it to make
the code simpler and clearer.

Change-Id: I29edc969431b4fd95cd5fd864953a71ca1538dd3
Reviewed-on: https://go-review.googlesource.com/c/go/+/582015
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Joedian Reid <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
callthingsoff authored and gopherbot committed Apr 26, 2024
1 parent 0e7f5cf commit b439f32
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
5 changes: 1 addition & 4 deletions src/cmd/go/internal/par/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,7 @@ func (c *Cache[K, V]) Get(key K) (V, bool) {
// TODO(jayconrod): Delete this after the package cache clearing functions
// in internal/load have been removed.
func (c *Cache[K, V]) Clear() {
c.m.Range(func(key, value any) bool {
c.m.Delete(key)
return true
})
c.m.Clear()
}

// Delete removes an entry from the map. It is safe to call Delete for an
Expand Down
5 changes: 1 addition & 4 deletions src/expvar/expvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ func (v *Map) Init() *Map {
v.keysMu.Lock()
defer v.keysMu.Unlock()
v.keys = v.keys[:0]
v.m.Range(func(k, _ any) bool {
v.m.Delete(k)
return true
})
v.m.Clear()
return v
}

Expand Down
13 changes: 3 additions & 10 deletions src/mime/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ var (
extensions sync.Map // map[string][]string; slice values are append-only.
)

func clearSyncMap(m *sync.Map) {
m.Range(func(k, _ any) bool {
m.Delete(k)
return true
})
}

// setMimeTypes is used by initMime's non-test path, and by tests.
func setMimeTypes(lowerExt, mixExt map[string]string) {
clearSyncMap(&mimeTypes)
clearSyncMap(&mimeTypesLower)
clearSyncMap(&extensions)
mimeTypes.Clear()
mimeTypesLower.Clear()
extensions.Clear()

for k, v := range lowerExt {
mimeTypesLower.Store(k, v)
Expand Down

0 comments on commit b439f32

Please sign in to comment.