Use Set[Type] instead of map[Type]bool/struct{}. (#26804)

This commit is contained in:
KN4CK3R 2023-08-30 08:55:25 +02:00 committed by GitHub
parent 815d267c80
commit 5315153059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 36 additions and 48 deletions

View file

@ -14,6 +14,7 @@ import (
"sort"
"time"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/util"
@ -130,7 +131,7 @@ func readDir(layer *Layer, name string) ([]fs.FileInfo, error) {
// * false: only directories will be returned.
// The returned files are sorted by name.
func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
fileMap := map[string]bool{}
fileSet := make(container.Set[string])
for _, layer := range l.layers {
infos, err := readDir(layer, name)
if err != nil {
@ -138,14 +139,11 @@ func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
}
for _, info := range infos {
if shouldInclude(info, fileMode...) {
fileMap[info.Name()] = true
fileSet.Add(info.Name())
}
}
}
files := make([]string, 0, len(fileMap))
for file := range fileMap {
files = append(files, file)
}
files := fileSet.Values()
sort.Strings(files)
return files, nil
}
@ -161,7 +159,7 @@ func (l *LayeredFS) ListAllFiles(name string, fileMode ...bool) ([]string, error
}
func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, error) {
fileMap := map[string]bool{}
fileSet := make(container.Set[string])
var list func(dir string) error
list = func(dir string) error {
for _, layer := range layers {
@ -172,7 +170,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
for _, info := range infos {
path := util.PathJoinRelX(dir, info.Name())
if shouldInclude(info, fileMode...) {
fileMap[path] = true
fileSet.Add(path)
}
if info.IsDir() {
if err = list(path); err != nil {
@ -186,10 +184,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
if err := list(name); err != nil {
return nil, err
}
var files []string
for file := range fileMap {
files = append(files, file)
}
files := fileSet.Values()
sort.Strings(files)
return files, nil
}

View file

@ -9,6 +9,7 @@ import (
"html/template"
"reflect"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
)
@ -51,7 +52,7 @@ func dict(args ...any) (map[string]any, error) {
return m, nil
}
func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
func dumpVarMarshalable(v any, dumped container.Set[uintptr]) (ret any, ok bool) {
if v == nil {
return nil, true
}
@ -61,11 +62,10 @@ func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
}
if e.CanAddr() {
addr := e.UnsafeAddr()
if dumped[addr] {
if !dumped.Add(addr) {
return "[dumped]", false
}
dumped[addr] = true
defer delete(dumped, addr)
defer dumped.Remove(addr)
}
switch e.Kind() {
case reflect.Bool, reflect.String,
@ -107,7 +107,7 @@ func dumpVar(v any) template.HTML {
if setting.IsProd {
return "<pre>dumpVar: only available in dev mode</pre>"
}
m, ok := dumpVarMarshalable(v, map[uintptr]bool{})
m, ok := dumpVarMarshalable(v, make(container.Set[uintptr]))
var dumpStr string
jsonBytes, err := json.MarshalIndent(m, "", " ")
if err != nil {