Preallocate slices when possible, remove unnecessary check when sortOrder is provided

This commit is contained in:
Seednode 2023-02-18 13:00:08 -06:00
parent e638e2709a
commit d05550a991
3 changed files with 23 additions and 39 deletions

View File

@ -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()

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
)
var Version = "0.37"
var Version = "0.38.0"
func init() {
rootCmd.AddCommand(versionCmd)

View File

@ -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, "", " ")