Optimize: map or slice initialize allocation size / cap (#1965)

This commit is contained in:
jesse.tang 2022-09-09 20:56:23 +08:00 committed by GitHub
parent b037d11c18
commit 9e8a58bf3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 43 deletions

View File

@ -301,45 +301,46 @@ func main() {
goth.UseProviders(openidConnect) goth.UseProviders(openidConnect)
} }
m := make(map[string]string) m := map[string]string{
m["amazon"] = "Amazon" "amazon": "Amazon",
m["bitbucket"] = "Bitbucket" "bitbucket": "Bitbucket",
m["box"] = "Box" "box": "Box",
m["dailymotion"] = "Dailymotion" "dailymotion": "Dailymotion",
m["deezer"] = "Deezer" "deezer": "Deezer",
m["digitalocean"] = "Digital Ocean" "digitalocean": "Digital Ocean",
m["discord"] = "Discord" "discord": "Discord",
m["dropbox"] = "Dropbox" "dropbox": "Dropbox",
m["facebook"] = "Facebook" "facebook": "Facebook",
m["fitbit"] = "Fitbit" "fitbit": "Fitbit",
m["github"] = "Github" "github": "Github",
m["gitlab"] = "Gitlab" "gitlab": "Gitlab",
m["soundcloud"] = "SoundCloud" "soundcloud": "SoundCloud",
m["spotify"] = "Spotify" "spotify": "Spotify",
m["steam"] = "Steam" "steam": "Steam",
m["stripe"] = "Stripe" "stripe": "Stripe",
m["twitch"] = "Twitch" "twitch": "Twitch",
m["uber"] = "Uber" "uber": "Uber",
m["wepay"] = "Wepay" "wepay": "Wepay",
m["yahoo"] = "Yahoo" "yahoo": "Yahoo",
m["yammer"] = "Yammer" "yammer": "Yammer",
m["gplus"] = "Google Plus" "gplus": "Google Plus",
m["heroku"] = "Heroku" "heroku": "Heroku",
m["instagram"] = "Instagram" "instagram": "Instagram",
m["intercom"] = "Intercom" "intercom": "Intercom",
m["lastfm"] = "Last FM" "lastfm": "Last FM",
m["linkedin"] = "Linkedin" "linkedin": "Linkedin",
m["onedrive"] = "Onedrive" "onedrive": "Onedrive",
m["paypal"] = "Paypal" "paypal": "Paypal",
m["twitter"] = "Twitter" "twitter": "Twitter",
m["salesforce"] = "Salesforce" "salesforce": "Salesforce",
m["slack"] = "Slack" "slack": "Slack",
m["meetup"] = "Meetup.com" "meetup": "Meetup.com",
m["auth0"] = "Auth0" "auth0": "Auth0",
m["openid-connect"] = "OpenID Connect" "openid-connect": "OpenID Connect",
m["xero"] = "Xero" "xero": "Xero",
}
var keys []string keys := make([]string, 0, len(m))
for k := range m { for k := range m {
keys = append(keys, k) keys = append(keys, k)
} }

View File

@ -18,7 +18,7 @@ type MemoryService struct {
func NewMemoryService() *MemoryService { func NewMemoryService() *MemoryService {
return &MemoryService{ return &MemoryService{
items: make(map[string][]Item, 0), items: make(map[string][]Item),
} }
} }

View File

@ -3030,11 +3030,11 @@ func (ctx *Context) ReadMultipartRelated() (MultipartRelated, error) {
return result, nil return result, nil
} }
func distinctStrings(values []string) (result []string) { func distinctStrings(values []string) []string {
seen := make(map[string]struct{}) seen := make(map[string]struct{}, len(values))
result := make([]string, 0, len(values))
for v := range values { for _, val := range values {
val := values[v]
if _, ok := seen[val]; !ok { if _, ok := seen[val]; !ok {
seen[val] = struct{}{} seen[val] = struct{}{}
result = append(result, val) result = append(result, val)