refactor: reduce GC pressure by pre-allocating slices

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan
2024-11-19 07:45:24 -05:00
parent 3982ba7258
commit d229ff39e5
10 changed files with 325 additions and 262 deletions
+1 -1
View File
@@ -143,7 +143,7 @@ func (s MediaFileMapper) albumArtistID(md metadata.Tags) string {
func (s MediaFileMapper) mapGenres(genres []string) (string, model.Genres) {
var result model.Genres
unique := map[string]struct{}{}
var all []string
all := make([]string, 0, len(genres)*2)
for i := range genres {
gs := strings.FieldsFunc(genres[i], func(r rune) bool {
return strings.ContainsRune(conf.Server.Scanner.GenreSeparators, r)
+4 -3
View File
@@ -84,7 +84,7 @@ func NewTag(filePath string, fileInfo os.FileInfo, tags ParsedTags) Tags {
func removeDuplicatesAndEmpty(values []string) []string {
encountered := map[string]struct{}{}
empty := true
var result []string
result := make([]string, 0, len(values))
for _, v := range values {
if _, ok := encountered[v]; ok {
continue
@@ -300,7 +300,7 @@ func (t Tags) getFirstTagValue(tagNames ...string) string {
}
func (t Tags) getAllTagValues(tagNames ...string) []string {
var values []string
values := make([]string, 0, len(tagNames)*2)
for _, tag := range tagNames {
if v, ok := t.Tags[tag]; ok {
values = append(values, v...)
@@ -311,7 +311,8 @@ func (t Tags) getAllTagValues(tagNames ...string) []string {
func (t Tags) getSortTag(originalTag string, tagNames ...string) string {
formats := []string{"sort%s", "sort_%s", "sort-%s", "%ssort", "%s_sort", "%s-sort"}
all := []string{originalTag}
all := make([]string, 1, len(tagNames)*len(formats)+1)
all[0] = originalTag
for _, tag := range tagNames {
for _, format := range formats {
name := fmt.Sprintf(format, tag)
+1 -1
View File
@@ -294,7 +294,7 @@ func (s *TagScanner) processChangedDir(ctx context.Context, refresher *refresher
// If track from folder is newer than the one in DB, select for update/insert in DB
log.Trace(ctx, "Processing changed folder", "dir", dir, "tracksInDB", len(currentTracks), "tracksInFolder", len(files))
var filesToUpdate []string
filesToUpdate := make([]string, 0, len(files))
for filePath, entry := range files {
c, inDB := currentTracks[filePath]
if !inDB || fullScan {
+4 -3
View File
@@ -64,7 +64,6 @@ func walkFolder(ctx context.Context, rootPath string, currentFolder string, resu
}
func loadDir(ctx context.Context, dirPath string) ([]string, *dirStats, error) {
var children []string
stats := &dirStats{}
dirInfo, err := os.Stat(dirPath)
@@ -77,11 +76,13 @@ func loadDir(ctx context.Context, dirPath string) ([]string, *dirStats, error) {
dir, err := os.Open(dirPath)
if err != nil {
log.Error(ctx, "Error in Opening directory", "path", dirPath, err)
return children, stats, err
return nil, stats, err
}
defer dir.Close()
for _, entry := range fullReadDir(ctx, dir) {
entries := fullReadDir(ctx, dir)
children := make([]string, 0, len(entries))
for _, entry := range entries {
isDir, err := isDirOrSymlinkToDir(dirPath, entry)
// Skip invalid symlinks
if err != nil {