Skip to content

Commit 76ecec2

Browse files
committed
add benchmark for many strings case + string case in writeValueFast (only saves ~1 byte per op)
Signed-off-by: Callum Styan <[email protected]>
1 parent 1212760 commit 76ecec2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

internal/entryhuman/entry.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func bracketedLevel(l slog.Level) string {
113113
// It returns (handled, error). When handled is false, the caller should fall back to formatValue.
114114
func writeValueFast(w io.Writer, v interface{}) (bool, error) {
115115
switch x := v.(type) {
116+
case string:
117+
_, err := w.Write([]byte(quote(x)))
118+
return true, err
116119
case bool:
117120
if x {
118121
_, err := w.Write([]byte("true"))

sloggers/sloghuman/sloghuman_bench_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ func BenchmarkHumanSinkLogEntry(b *testing.B) {
7575
Fields: nil,
7676
},
7777
},
78+
{
79+
"StringHeavy",
80+
slog.SinkEntry{
81+
Time: time.Unix(0, 0),
82+
Level: slog.LevelInfo,
83+
Message: "user action",
84+
Fields: slog.M(
85+
slog.F("user_id", "abc-123-def-456-ghi-789"),
86+
slog.F("action", "login"),
87+
slog.F("ip_address", "192.168.1.100"),
88+
slog.F("user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"),
89+
slog.F("session_id", "sess_9d8f7a6b5c4e3d2a1b0c"),
90+
slog.F("request_id", "req_1a2b3c4d5e6f7g8h9i0j"),
91+
slog.F("endpoint", "/api/v1/users/login"),
92+
slog.F("method", "POST"),
93+
),
94+
},
95+
},
7896
}
7997
for _, tc := range testcases {
8098
b.Run(tc.name, func(b *testing.B) {

0 commit comments

Comments
 (0)