Skip to content

Commit 7e1fad2

Browse files
Give the StringBuilder a default buffer size to avoid memory copying in logging module (alibaba#288)
1 parent d36315f commit 7e1fad2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

logging/logging.go

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727
// RecordLogFileName represents the default file name of the record log.
2828
RecordLogFileName = "sentinel-record.log"
2929
GlobalCallerDepth = 4
30+
31+
defaultLogMsgBufferSize = 256
3032
)
3133

3234
var (
@@ -117,6 +119,8 @@ func caller(depth int) (file string, line int) {
117119

118120
func AssembleMsg(depth int, logLevel, msg string, err error, keysAndValues ...interface{}) string {
119121
sb := strings.Builder{}
122+
sb.Grow(defaultLogMsgBufferSize)
123+
120124
file, line := caller(depth)
121125
timeStr := time.Now().Format("2006-01-02 15:04:05.520")
122126
caller := fmt.Sprintf("%s:%d", file, line)

logging/logging_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,10 @@ func Benchmark_LoggingDebug_With_Precheck(b *testing.B) {
109109
}
110110
}
111111
}
112+
113+
func BenchmarkAssembleMsg(b *testing.B) {
114+
b.ReportAllocs()
115+
for i := 0; i < b.N; i++ {
116+
AssembleMsg(1, "INFO", "test msg", nil, "k1", "v1", "k2", "v2")
117+
}
118+
}

0 commit comments

Comments
 (0)