Skip to content

Commit

Permalink
perf: preallocate the slice if we know its final length (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
apocelipes committed Jul 21, 2024
1 parent 55ef797 commit 8e5721c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions cmd/badges/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,6 +14,9 @@ import (
"strconv"
"strings"
"time"

"github.com/boyter/scc/v3/processor"
"github.com/rs/zerolog/log"
)

var uniqueCode = "unique_code"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion processor/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/"))
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 8e5721c

Please sign in to comment.