refactor(general): leverage maps.Clone (#4905)

This commit is contained in:
Julio Lopez
2025-10-23 19:45:29 -07:00
committed by GitHub
parent c5cd3c5651
commit 4cbd7026a6
9 changed files with 12 additions and 38 deletions

View File

@@ -135,8 +135,7 @@ func errorPrefix() string {
}
func (cv *cacheVerifier) reset() {
cv.lastCallCounter = make(map[string]int)
maps.Copy(cv.lastCallCounter, cv.cacheSource.callCounter)
cv.lastCallCounter = maps.Clone(cv.cacheSource.callCounter)
}
type lockState struct {

View File

@@ -92,10 +92,7 @@ func (s *perKindTracker) active() map[any]string {
s.mu.Lock()
defer s.mu.Unlock()
res := map[any]string{}
maps.Copy(res, s.items)
return res
return maps.Clone(s.items)
}
var (

View File

@@ -708,8 +708,7 @@ func (s *Server) syncSourcesLocked(ctx context.Context) error {
// copy existing sources to a map, from which we will remove sources that are found
// in the repository
oldSourceManagers := map[snapshot.SourceInfo]*sourceManager{}
maps.Copy(oldSourceManagers, s.sourceManagers)
oldSourceManagers := maps.Clone(s.sourceManagers)
for src := range sources {
if sm, ok := oldSourceManagers[src]; ok {
@@ -1045,11 +1044,7 @@ func (s *Server) snapshotAllSourceManagers() map[snapshot.SourceInfo]*sourceMana
s.serverMutex.RLock()
defer s.serverMutex.RUnlock()
result := map[snapshot.SourceInfo]*sourceManager{}
maps.Copy(result, s.sourceManagers)
return result
return maps.Clone(s.sourceManagers)
}
func (s *Server) getSchedulerItems(ctx context.Context, now time.Time) []scheduler.Item {

View File

@@ -41,11 +41,7 @@ func (s *Server) listMounts() map[object.ID]mount.Controller {
s.serverMutex.RLock()
defer s.serverMutex.RUnlock()
result := map[object.ID]mount.Controller{}
maps.Copy(result, s.mounts)
return result
return maps.Clone(s.mounts)
}
func (s *Server) deleteMount(oid object.ID) {

View File

@@ -4,6 +4,7 @@
"bytes"
"context"
"encoding/json"
"maps"
"sync"
"time"
@@ -121,7 +122,7 @@ func (t *runningTaskInfo) ReportCounters(c map[string]CounterValue) {
t.mu.Lock()
defer t.mu.Unlock()
t.Counters = cloneCounters(c)
t.Counters = maps.Clone(c)
}
// info returns a copy of task information while holding a lock.
@@ -130,7 +131,7 @@ func (t *runningTaskInfo) info() Info {
defer t.mu.Unlock()
i := t.Info
i.Counters = cloneCounters(i.Counters)
i.Counters = maps.Clone(i.Counters)
return i
}

View File

@@ -1,7 +1,5 @@
package uitask
import "maps"
// CounterValue describes the counter value reported by task with optional units for presentation.
type CounterValue struct {
Value int64 `json:"value"`
@@ -48,10 +46,3 @@ func ErrorBytesCounter(v int64) CounterValue {
func ErrorCounter(v int64) CounterValue {
return CounterValue{v, "", "error"}
}
func cloneCounters(c map[string]CounterValue) map[string]CounterValue {
newCounters := map[string]CounterValue{}
maps.Copy(newCounters, c)
return newCounters
}

View File

@@ -352,8 +352,8 @@ func() {
oldEnv := e.Environment
e.Environment = map[string]string{}
maps.Copy(e.Environment, oldEnv)
maps.Copy(e.Environment, oldEnv)
maps.Copy(e.Environment, snapshotCreateEnv)
defer func() { e.Environment = oldEnv }()

View File

@@ -148,8 +148,8 @@ func (fw *FileWriter) WriteRandomFiles(ctx context.Context, opts map[string]stri
log.Printf("Writing files at depth %v (fileSize: %v-%v, numFiles: %v, blockSize: %v, dedupPcnt: %v, ioLimit: %v)\n", dirDepth, minFileSizeB, maxFileSizeB, numFiles, blockSize, dedupPcnt, ioLimit)
retOpts := make(map[string]string, len(opts))
maps.Copy(retOpts, opts)
maps.Copy(retOpts, opts)
maps.Copy(retOpts, fioOpts)
retOpts["dirDepth"] = strconv.Itoa(dirDepth)
@@ -176,9 +176,7 @@ func (fw *FileWriter) DeleteRandomSubdirectory(ctx context.Context, opts map[str
log.Printf("Deleting directory at depth %v\n", dirDepth)
retOpts := make(map[string]string, len(opts))
maps.Copy(retOpts, opts)
retOpts := maps.Clone(opts)
retOpts["dirDepth"] = strconv.Itoa(dirDepth)
err := fw.Runner.DeleteDirAtDepth("", dirDepth)
@@ -207,9 +205,7 @@ func (fw *FileWriter) DeleteDirectoryContents(ctx context.Context, opts map[stri
log.Printf("Deleting %d%% of directory contents at depth %v\n", pcnt, dirDepth)
retOpts := make(map[string]string, len(opts))
maps.Copy(retOpts, opts)
retOpts := maps.Clone(opts)
retOpts["dirDepth"] = strconv.Itoa(dirDepth)
retOpts["percent"] = strconv.Itoa(pcnt)

View File

@@ -37,7 +37,6 @@ func (o Options) Merge(other Options) Options {
out := make(map[string]string, len(o)+len(other))
maps.Copy(out, o)
maps.Copy(out, other)
return out