From 1360686907fdca7a73910e4553ec68176090ae5c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 22 May 2020 12:38:25 +0800 Subject: [PATCH 1/2] chore: improve titleCasedName performance Signed-off-by: Bo-Yi Wu --- names/mapper.go | 15 ++++++++------- names/mapper_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/names/mapper.go b/names/mapper.go index 80aa48d5..53db5f81 100644 --- a/names/mapper.go +++ b/names/mapper.go @@ -103,27 +103,28 @@ func (mapper SnakeMapper) Obj2Table(name string) string { } func titleCasedName(name string) string { - newstr := make([]rune, 0) + newstr := make([]byte, 0, len(name)) upNextChar := true name = strings.ToLower(name) - for _, chr := range name { + for i := 0; i < len(name); i++ { + c := name[i] switch { case upNextChar: upNextChar = false - if 'a' <= chr && chr <= 'z' { - chr -= ('a' - 'A') + if 'a' <= c && c <= 'z' { + c += 'a' - 'A' } - case chr == '_': + case c == '_': upNextChar = true continue } - newstr = append(newstr, chr) + newstr = append(newstr, c) } - return string(newstr) + return b2s(newstr) } func (mapper SnakeMapper) Table2Obj(name string) string { diff --git a/names/mapper_test.go b/names/mapper_test.go index 5c6dc5d9..a39cb569 100644 --- a/names/mapper_test.go +++ b/names/mapper_test.go @@ -58,3 +58,13 @@ func BenchmarkSnakeCasedName(b *testing.B) { _ = 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) + } +} -- 2.40.1 From 1637507361f68b90ddfb50825639ecf6d417e31b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 22 May 2020 15:30:01 +0800 Subject: [PATCH 2/2] udpate Signed-off-by: Bo-Yi Wu --- names/mapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/names/mapper.go b/names/mapper.go index 53db5f81..79add76e 100644 --- a/names/mapper.go +++ b/names/mapper.go @@ -114,7 +114,7 @@ func titleCasedName(name string) string { case upNextChar: upNextChar = false if 'a' <= c && c <= 'z' { - c += 'a' - 'A' + c -= 'a' - 'A' } case c == '_': upNextChar = true -- 2.40.1