Skip to content

Commit eafd379

Browse files
authored
fix: Avoid allocation in the stringifyValue (#3292)
1 parent 2406067 commit eafd379

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

github/strings.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Stringify(message interface{}) string {
2727

2828
func stringifyValue(w *bytes.Buffer, val reflect.Value) {
2929
if val.Kind() == reflect.Ptr && val.IsNil() {
30-
w.Write([]byte("<nil>"))
30+
w.WriteString("<nil>")
3131
return
3232
}
3333

@@ -37,20 +37,20 @@ func stringifyValue(w *bytes.Buffer, val reflect.Value) {
3737
case reflect.String:
3838
fmt.Fprintf(w, `"%s"`, v)
3939
case reflect.Slice:
40-
w.Write([]byte{'['})
40+
w.WriteByte('[')
4141
for i := 0; i < v.Len(); i++ {
4242
if i > 0 {
43-
w.Write([]byte{' '})
43+
w.WriteByte(' ')
4444
}
4545

4646
stringifyValue(w, v.Index(i))
4747
}
4848

49-
w.Write([]byte{']'})
49+
w.WriteByte(']')
5050
return
5151
case reflect.Struct:
5252
if v.Type().Name() != "" {
53-
w.Write([]byte(v.Type().String()))
53+
w.WriteString(v.Type().String())
5454
}
5555

5656
// special handling of Timestamp values
@@ -59,7 +59,7 @@ func stringifyValue(w *bytes.Buffer, val reflect.Value) {
5959
return
6060
}
6161

62-
w.Write([]byte{'{'})
62+
w.WriteByte('{')
6363

6464
var sep bool
6565
for i := 0; i < v.NumField(); i++ {
@@ -75,17 +75,17 @@ func stringifyValue(w *bytes.Buffer, val reflect.Value) {
7575
}
7676

7777
if sep {
78-
w.Write([]byte(", "))
78+
w.WriteString(", ")
7979
} else {
8080
sep = true
8181
}
8282

83-
w.Write([]byte(v.Type().Field(i).Name))
84-
w.Write([]byte{':'})
83+
w.WriteString(v.Type().Field(i).Name)
84+
w.WriteByte(':')
8585
stringifyValue(w, fv)
8686
}
8787

88-
w.Write([]byte{'}'})
88+
w.WriteByte('}')
8989
default:
9090
if v.CanInterface() {
9191
fmt.Fprint(w, v.Interface())

0 commit comments

Comments
 (0)