Skip to content

Commit e75c899

Browse files
committed
reflect: optimize (reflect.Type).Name
Improves JSON decoding on linux/amd64. name old time/op new time/op delta CodeUnmarshal-40 89.3ms ± 2% 86.3ms ± 2% -3.31% (p=0.000 n=22+22) name old speed new speed delta CodeUnmarshal-40 21.7MB/s ± 2% 22.5MB/s ± 2% +3.44% (p=0.000 n=22+22) Updates golang#16117 Change-Id: I52acf31d7729400cfe6693e46292d41e1addba3d Reviewed-on: https://go-review.googlesource.com/24410 Run-TryBot: David Crawshaw <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e369490 commit e75c899

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/reflect/type.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -891,25 +891,30 @@ func hasPrefix(s, prefix string) bool {
891891

892892
func (t *rtype) Name() string {
893893
s := t.String()
894-
if hasPrefix(s, "map[") {
895-
return ""
896-
}
897-
if hasPrefix(s, "struct {") {
898-
return ""
899-
}
900-
if hasPrefix(s, "chan ") {
901-
return ""
902-
}
903-
if hasPrefix(s, "chan<-") {
904-
return ""
905-
}
906-
if hasPrefix(s, "func(") {
907-
return ""
908-
}
909-
if hasPrefix(s, "interface {") {
910-
return ""
911-
}
912894
switch s[0] {
895+
case 'm':
896+
if hasPrefix(s, "map[") {
897+
return ""
898+
}
899+
case 's':
900+
if hasPrefix(s, "struct {") {
901+
return ""
902+
}
903+
case 'c':
904+
if hasPrefix(s, "chan ") {
905+
return ""
906+
}
907+
if hasPrefix(s, "chan<-") {
908+
return ""
909+
}
910+
case 'f':
911+
if hasPrefix(s, "func(") {
912+
return ""
913+
}
914+
case 'i':
915+
if hasPrefix(s, "interface {") {
916+
return ""
917+
}
913918
case '[', '*', '<':
914919
return ""
915920
}

0 commit comments

Comments
 (0)