-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #600 from tdakkota/perf/optimize-name-rules
perf(naming): use hashmap to search naming rules
- Loading branch information
Showing
12 changed files
with
140 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
internal/capitalize/capitalize.go → internal/naming/capitalize.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
// Package capitalize contains capitalize function. | ||
package capitalize | ||
package naming | ||
|
||
import ( | ||
"unicode" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package naming contains some utilities for generating names. | ||
package naming |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package naming | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
var ( | ||
rules = [...]string{ | ||
"ACL", "API", "ASCII", "AWS", "CPU", "CSS", "DNS", "EOF", "GB", "GUID", | ||
"HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "KB", "LHS", "MAC", "MB", | ||
"QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SQL", "SSH", "SSO", "TLS", | ||
"TTL", "UI", "UID", "URI", "URL", "UTF8", "UUID", "VM", "XML", "XMPP", | ||
"XSRF", "XSS", "SMS", "CDN", "TCP", "UDP", "DC", "PFS", "P2P", | ||
"SHA256", "SHA1", "MD5", "SRP", "2FA", "OAuth", "OAuth2", | ||
|
||
"PNG", "JPG", "GIF", "MP4", "WEBP", | ||
} | ||
// rulesMap is a map of lowered rules to their canonical form. | ||
// | ||
// NOTE: we're using a map instead of a linear/binary search because | ||
// lowered string allocation is much cheaper than string comparison. | ||
// Also, ToLower doesn't allocate if the string is already in lower case. | ||
rulesMap = func() (r map[string]string) { | ||
r = make(map[string]string) | ||
for _, v := range rules { | ||
r[strings.ToLower(v)] = v | ||
} | ||
return r | ||
}() | ||
) | ||
|
||
// Rule returns the rule for the given part, if any. | ||
// Otherwise, it returns ("", false). | ||
func Rule(part string) (string, bool) { | ||
v, ok := rulesMap[strings.ToLower(part)] | ||
return v, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package naming | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRule(t *testing.T) { | ||
a := require.New(t) | ||
for _, rule := range rules { | ||
testFind := func(key string) { | ||
v, ok := Rule(key) | ||
a.True(ok) | ||
a.Equal(rule, v) | ||
} | ||
testFind(rule) | ||
testFind(strings.ToLower(rule)) | ||
testFind(strings.ToUpper(rule)) | ||
testFind(strings.ToLower(rule[:1]) + rule[1:]) | ||
} | ||
} | ||
|
||
func BenchmarkRule(b *testing.B) { | ||
suite := [...]string{ | ||
"wifi", | ||
"WiFi", | ||
"ASCII", | ||
"mp3", | ||
"Oauth", | ||
"WebP", | ||
"JPEG", | ||
} | ||
|
||
b.Run("Rule", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
var ( | ||
v string | ||
ok bool | ||
) | ||
for i := 0; i < b.N; i++ { | ||
rule := suite[i%len(suite)] | ||
v, ok = Rule(rule) | ||
} | ||
if ok && v == "" { | ||
b.Fatal("sink is empty") | ||
} | ||
}) | ||
|
||
linear := func(s string) (string, bool) { | ||
for _, rule := range &rules { | ||
if strings.EqualFold(s, rule) { | ||
return rule, true | ||
} | ||
} | ||
return "", false | ||
} | ||
b.Run("LinearSearch", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
var ( | ||
v string | ||
ok bool | ||
) | ||
for i := 0; i < b.N; i++ { | ||
rule := suite[i%len(suite)] | ||
v, ok = linear(rule) | ||
} | ||
if ok && v == "" { | ||
b.Fatal("sink is empty") | ||
} | ||
}) | ||
} |