From 8e5721c939a7a25e44c9b83fdcbf9b6099680e5f Mon Sep 17 00:00:00 2001 From: apocelipes Date: Mon, 22 Jul 2024 07:38:37 +0800 Subject: [PATCH] perf: preallocate the slice if we know its final length (#487) --- cmd/badges/main.go | 7 ++++--- main.go | 2 +- processor/detector.go | 2 +- processor/formatters.go | 10 +++++----- processor/processor.go | 4 ++-- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/cmd/badges/main.go b/cmd/badges/main.go index 675ce330..35af8b09 100644 --- a/cmd/badges/main.go +++ b/cmd/badges/main.go @@ -5,8 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/boyter/scc/v3/processor" - "github.com/rs/zerolog/log" "math" "net/http" "net/url" @@ -16,6 +14,9 @@ import ( "strconv" "strings" "time" + + "github.com/boyter/scc/v3/processor" + "github.com/rs/zerolog/log" ) var uniqueCode = "unique_code" @@ -299,7 +300,7 @@ func processPath(s string) string { return "" } - sp := []string{} + sp := make([]string, 0, len(split)) for _, s := range split { sp = append(sp, cleanString(s)) diff --git a/main.go b/main.go index be691bbf..f48cfd93 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,7 @@ func main() { } args := strings.Split(string(b), "\n") - var newArgs []string + newArgs := make([]string, 0, len(args)) for _, x := range args { newArgs = append(newArgs, strings.TrimSpace(x)) } diff --git a/processor/detector.go b/processor/detector.go index bbf39054..0e55ef2b 100644 --- a/processor/detector.go +++ b/processor/detector.go @@ -175,7 +175,7 @@ func DetermineLanguage(filename string, fallbackLanguage string, possibleLanguag primary := "" - toSort := []languageGuess{} + toSort := make([]languageGuess, 0, len(possibleLanguages)) for _, lan := range possibleLanguages { LanguageFeaturesMutex.Lock() langFeatures := LanguageFeatures[lan] diff --git a/processor/formatters.go b/processor/formatters.go index b2e13ae5..f827eac0 100644 --- a/processor/formatters.go +++ b/processor/formatters.go @@ -297,7 +297,7 @@ func toCSVSummary(input chan *FileJob) string { language := aggregateLanguageSummary(input) language = sortLanguageSummary(language) - records := [][]string{} + records := make([][]string, 0, len(language)) for _, result := range language { records = append(records, []string{ @@ -606,7 +606,7 @@ func toHtmlTable(input chan *FileJob) string { } } - language := []LanguageSummary{} + language := make([]LanguageSummary, 0, len(languages)) for _, summary := range languages { language = append(language, summary) } @@ -958,7 +958,7 @@ func fileSummarizeLong(input chan *FileJob) string { } } - language := []LanguageSummary{} + language := make([]LanguageSummary, 0, len(langs)) for _, summary := range langs { language = append(language, summary) } @@ -1174,7 +1174,7 @@ func fileSummarizeShort(input chan *FileJob) string { } } - language := []LanguageSummary{} + language := make([]LanguageSummary, 0, len(lang)) for _, summary := range lang { language = append(language, summary) } @@ -1488,7 +1488,7 @@ func aggregateLanguageSummary(input chan *FileJob) []LanguageSummary { } } - language := []LanguageSummary{} + language := make([]LanguageSummary, 0, len(languages)) for _, summary := range languages { language = append(language, summary) } diff --git a/processor/processor.go b/processor/processor.go index 11dd8a01..ac501b3a 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -310,7 +310,7 @@ func ProcessConstants() { } // Fix for https://github.com/boyter/scc/issues/250 - fixedPath := []string{} + fixedPath := make([]string, 0, len(PathDenyList)) for _, path := range PathDenyList { fixedPath = append(fixedPath, strings.TrimRight(path, "/")) } @@ -532,8 +532,8 @@ func loadDatabase() map[string]Language { func printLanguages() { database := loadDatabase() - var names []string + names := make([]string, 0, len(database)) for key := range database { names = append(names, key) }