Bläddra i källkod

perf: preallocate slices

Frédéric Guillot 5 månader sedan
förälder
incheckning
ec93656ef5

+ 1 - 0
.golangci.yml

@@ -10,6 +10,7 @@ linters:
     - loggercheck
     - misspell
     - perfsprint
+    - prealloc
     - sqlclosecheck
     - staticcheck
     - whitespace

+ 3 - 3
internal/googlereader/request_modifier.go

@@ -29,19 +29,19 @@ func (r RequestModifiers) String() string {
 
 	results = append(results, fmt.Sprintf("UserID: %d", r.UserID))
 
-	var streamStr []string
+	streamStr := make([]string, 0, len(r.Streams))
 	for _, s := range r.Streams {
 		streamStr = append(streamStr, s.String())
 	}
 	results = append(results, fmt.Sprintf("Streams: [%s]", strings.Join(streamStr, ", ")))
 
-	var exclusions []string
+	exclusions := make([]string, 0, len(r.ExcludeTargets))
 	for _, s := range r.ExcludeTargets {
 		exclusions = append(exclusions, s.String())
 	}
 	results = append(results, fmt.Sprintf("Exclusions: [%s]", strings.Join(exclusions, ", ")))
 
-	var filters []string
+	filters := make([]string, 0, len(r.FilterTargets))
 	for _, s := range r.FilterTargets {
 		filters = append(filters, s.String())
 	}

+ 2 - 2
internal/integration/matrixbot/matrixbot.go

@@ -23,8 +23,8 @@ func PushEntries(feed *model.Feed, entries model.Entries, matrixBaseURL, matrixU
 		return err
 	}
 
-	var textMessages []string
-	var formattedTextMessages []string
+	textMessages := make([]string, 0, len(entries))
+	formattedTextMessages := make([]string, 0, len(entries))
 
 	for _, entry := range entries {
 		textMessages = append(textMessages, fmt.Sprintf(`[%s] %s - %s`, feed.Title, entry.Title, entry.URL))

+ 1 - 1
internal/integration/webhook/webhook.go

@@ -73,7 +73,7 @@ func (c *Client) SendNewEntriesWebhookEvent(feed *model.Feed, entries model.Entr
 		return nil
 	}
 
-	var webhookEntries []*WebhookEntry
+	webhookEntries := make([]*WebhookEntry, 0, len(entries))
 	for _, entry := range entries {
 		webhookEntries = append(webhookEntries, &WebhookEntry{
 			ID:          entry.ID,

+ 1 - 1
internal/reader/itunes/itunes.go

@@ -23,7 +23,7 @@ type ItunesChannelElement struct {
 }
 
 func (i *ItunesChannelElement) GetItunesCategories() []string {
-	var categories []string
+	categories := make([]string, 0, len(i.ItunesCategories))
 	for _, category := range i.ItunesCategories {
 		categories = append(categories, category.Text)
 		if category.SubCategory != nil {

+ 2 - 2
internal/reader/processor/youtube.go

@@ -43,8 +43,8 @@ func fetchYouTubeWatchTimeForSingleEntry(websiteURL string) (int, error) {
 }
 
 func fetchYouTubeWatchTimeInBulk(entries []*model.Entry) {
-	var videosEntriesMapping = make(map[string]*model.Entry, len(entries))
-	var videoIDs []string
+	videosEntriesMapping := make(map[string]*model.Entry, len(entries))
+	videoIDs := make([]string, 0, len(entries))
 
 	for _, entry := range entries {
 		if !isYouTubeVideoURL(entry.URL) {

+ 1 - 2
internal/reader/readability/readability.go

@@ -62,11 +62,10 @@ func (c *candidate) String() string {
 type candidateList map[*html.Node]*candidate
 
 func (c candidateList) String() string {
-	var output []string
+	output := make([]string, 0, len(c))
 	for _, candidate := range c {
 		output = append(output, candidate.String())
 	}
-
 	return strings.Join(output, ", ")
 }
 

+ 2 - 1
internal/reader/sanitizer/sanitizer.go

@@ -305,7 +305,8 @@ func SanitizeHTML(baseURL, rawHTML string, sanitizerOptions *SanitizerOptions) s
 }
 
 func sanitizeAttributes(parsedBaseUrl *url.URL, tagName string, attributes []html.Attribute, sanitizerOptions *SanitizerOptions) ([]string, string) {
-	var htmlAttrs, attrNames []string
+	htmlAttrs := make([]string, 0, len(attributes))
+	attrNames := make([]string, 0, len(attributes))
 	var err error
 	var isAnchorLink bool