chore: improve titleCasedName performance #1691

Merged
lunny merged 2 commits from performance into master 2020-05-23 02:24:10 +00:00
2 changed files with 18 additions and 7 deletions

View File

@ -103,27 +103,28 @@ func (mapper SnakeMapper) Obj2Table(name string) string {
} }
func titleCasedName(name string) string { func titleCasedName(name string) string {
newstr := make([]rune, 0) newstr := make([]byte, 0, len(name))
upNextChar := true upNextChar := true
name = strings.ToLower(name) name = strings.ToLower(name)
for _, chr := range name { for i := 0; i < len(name); i++ {
c := name[i]
switch { switch {
case upNextChar: case upNextChar:
upNextChar = false upNextChar = false
if 'a' <= chr && chr <= 'z' { if 'a' <= c && c <= 'z' {
chr -= ('a' - 'A') c -= 'a' - 'A'

I will change to c -= 'a' - 'A'

I will change to `c -= 'a' - 'A'`
} }
case chr == '_': case c == '_':
upNextChar = true upNextChar = true
continue continue
} }
newstr = append(newstr, chr) newstr = append(newstr, c)
} }
return string(newstr) return b2s(newstr)
} }
func (mapper SnakeMapper) Table2Obj(name string) string { func (mapper SnakeMapper) Table2Obj(name string) string {

View File

@ -58,3 +58,13 @@ func BenchmarkSnakeCasedName(b *testing.B) {
_ = snakeCasedName(s) _ = snakeCasedName(s)
} }
} }
func BenchmarkTitleCasedName(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
s := strings.Repeat("foo_bar", 32)
for i := 0; i < b.N; i++ {
_ = titleCasedName(s)
}
}