From d05550a991aae27ec935ba8e8279ffe962886027 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 18 Feb 2023 13:00:08 -0600 Subject: [PATCH] Preallocate slices when possible, remove unnecessary check when sortOrder is provided --- cmd/files.go | 46 ++++++++++++++-------------------------------- cmd/version.go | 2 +- cmd/web.go | 14 ++++++++------ 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 8219b98..8be3d80 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -52,7 +52,7 @@ type Dimensions struct { } type Files struct { - mutex sync.Mutex + mutex sync.RWMutex list map[string][]string } @@ -448,7 +448,7 @@ func fileList(paths []string, filters *Filters, sort string, index *Index) ([]st var fileList []string files := &Files{ - mutex: sync.Mutex{}, + mutex: sync.RWMutex{}, list: make(map[string][]string), } @@ -505,45 +505,27 @@ func fileList(paths []string, filters *Filters, sort string, index *Index) ([]st return fileList, false } -func cleanFilename(filename string) string { - return filename[:len(filename)-(len(filepath.Ext(filename))+3)] -} - -func prepareDirectory(directory []string) []string { - _, first := filepath.Split(directory[0]) - first = cleanFilename(first) - - _, last := filepath.Split(directory[len(directory)-1]) - last = cleanFilename(last) - - if first == last { - return append([]string{}, directory[0]) - } else { - return directory - } -} - func prepareDirectories(files *Files, sort string) []string { - directories := []string{} + i, l := 0, 0 + + files.mutex.RLock() keys := make([]string, len(files.list)) - i := 0 for k := range files.list { keys[i] = k i++ + l += len(files.list[k]) } - if sort == "asc" || sort == "desc" { - for i := 0; i < len(keys); i++ { - directories = append(directories, prepareDirectory(files.list[keys[i]])...) - } - } else { - for i := 0; i < len(keys); i++ { - directories = append(directories, files.list[keys[i]]...) - } + directories := make([]string, l) + + for i := 0; i < len(keys); i++ { + copy(directories, files.list[keys[i]]) } + files.mutex.RUnlock() + return directories } @@ -586,7 +568,7 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin } func normalizePaths(args []string) ([]string, error) { - var paths []string + paths := make([]string, len(args)) fmt.Println("Paths:") @@ -607,7 +589,7 @@ func normalizePaths(args []string) ([]string, error) { fmt.Printf("%s\n", args[i]) } - paths = append(paths, absolutePath) + paths[i] = absolutePath } fmt.Println() diff --git a/cmd/version.go b/cmd/version.go index 5166e59..f22ed8d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.37" +var Version = "0.38.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index a2fd475..fcf1088 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -198,7 +198,7 @@ func (s *ServeStats) incrementCounter(image string, timestamp time.Time, filesiz func (s *ServeStats) toExported() *exportedServeStats { stats := &exportedServeStats{ - List: []string{}, + List: make([]string, len(s.list)), Count: make(map[string]uint64), Size: make(map[string]string), Times: make(map[string][]string), @@ -206,7 +206,7 @@ func (s *ServeStats) toExported() *exportedServeStats { s.mutex.RLock() - stats.List = append(stats.List, s.list...) + copy(stats.List, s.list) for k, v := range s.count { stats.Count[k] = v @@ -228,7 +228,9 @@ func (s *ServeStats) toExported() *exportedServeStats { func (s *ServeStats) toImported(stats *exportedServeStats) { s.mutex.Lock() - s.list = append(s.list, stats.List...) + s.list = make([]string, len(stats.List)) + + copy(s.list, stats.List) for k, v := range stats.Count { s.count[k] = v @@ -252,10 +254,10 @@ func (s *ServeStats) ListImages() ([]byte, error) { return stats.List[p] < stats.List[q] }) - a := []timesServed{} + a := make([]timesServed, len(stats.List)) - for _, image := range stats.List { - a = append(a, timesServed{image, stats.Count[image], stats.Size[image], stats.Times[image]}) + for k, v := range stats.List { + a[k] = timesServed{v, stats.Count[v], stats.Size[v], stats.Times[v]} } r, err := json.MarshalIndent(a, "", " ")