-
Notifications
You must be signed in to change notification settings - Fork 24
perf: human log sink performance bump via reduction of allocs/total allocated memory #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
b05c3fd
873ed35
5816218
6d17803
0ca41cc
bbd432e
ab9bf95
5838eb4
1212760
76ecec2
034384c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,60 +88,168 @@ func formatValue(v interface{}) string { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const tab = " " | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Fmt returns a human readable format for ent. | ||||||||||||||||||||||||||||||||||||||||||
| // bracketedLevel is an optimization to avoid extra allocations and calls to strings.ToLower | ||||||||||||||||||||||||||||||||||||||||||
| // when we want to translate/print the lowercase version of a log level. | ||||||||||||||||||||||||||||||||||||||||||
| func bracketedLevel(l slog.Level) string { | ||||||||||||||||||||||||||||||||||||||||||
| switch l { | ||||||||||||||||||||||||||||||||||||||||||
| case slog.LevelDebug: | ||||||||||||||||||||||||||||||||||||||||||
| return "[debu]" | ||||||||||||||||||||||||||||||||||||||||||
| case slog.LevelInfo: | ||||||||||||||||||||||||||||||||||||||||||
| return "[info]" | ||||||||||||||||||||||||||||||||||||||||||
| case slog.LevelWarn: | ||||||||||||||||||||||||||||||||||||||||||
| return "[warn]" | ||||||||||||||||||||||||||||||||||||||||||
| case slog.LevelError: | ||||||||||||||||||||||||||||||||||||||||||
| return "[erro]" | ||||||||||||||||||||||||||||||||||||||||||
| case slog.LevelCritical: | ||||||||||||||||||||||||||||||||||||||||||
| return "[crit]" | ||||||||||||||||||||||||||||||||||||||||||
| case slog.LevelFatal: | ||||||||||||||||||||||||||||||||||||||||||
| return "[fata]" | ||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||
| return "[unkn]" | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Optimization to avoid allocation of heap allocations/temporary strings via formatValue when dealing with primitive types. | ||||||||||||||||||||||||||||||||||||||||||
| // It returns (handled, error). When handled is false, the caller should fall back to formatValue. | ||||||||||||||||||||||||||||||||||||||||||
| func writeValueFast(w io.Writer, v interface{}) (bool, error) { | ||||||||||||||||||||||||||||||||||||||||||
| switch x := v.(type) { | ||||||||||||||||||||||||||||||||||||||||||
| case string: | ||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write([]byte(quote(x))) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
| case bool: | ||||||||||||||||||||||||||||||||||||||||||
| if x { | ||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write([]byte("true")) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write([]byte("false")) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // signed ints | ||||||||||||||||||||||||||||||||||||||||||
| case int: | ||||||||||||||||||||||||||||||||||||||||||
| var a [20]byte | ||||||||||||||||||||||||||||||||||||||||||
Emyrk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
| case int8: | ||||||||||||||||||||||||||||||||||||||||||
| var a [20]byte | ||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
| case int16: | ||||||||||||||||||||||||||||||||||||||||||
| var a [20]byte | ||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
| case int32: | ||||||||||||||||||||||||||||||||||||||||||
| var a [20]byte | ||||||||||||||||||||||||||||||||||||||||||
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | ||||||||||||||||||||||||||||||||||||||||||
| return true, err | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| case int: | |
| var a [20]byte | |
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | |
| return true, err | |
| case int8: | |
| var a [20]byte | |
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | |
| return true, err | |
| case int16: | |
| var a [20]byte | |
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | |
| return true, err | |
| case int32: | |
| var a [20]byte | |
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | |
| return true, err | |
| case int, int8, int16, int32: | |
| var a [20]byte | |
| _, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10)) | |
| return true, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately we can't do it quite like this, the compiler still treats each type as distinct even if they're all in the same case like this, which means we'd either have to type assert OR reflect within the case itself
we can at least move the byte slice declaration, write call, and return, into reusable writeSignedInt and writeUnsignedInt functions
cstyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow this old code was kinda dumb in comparison.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2000-02-05 04:04:04.000 [crit] critical |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2000-02-05 04:04:04.000 [fata] fatal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2000-02-05 04:04:04.000 [debu] mixed types count=100 name=test enabled=true ratio=0.95 data="bytes" nil_val=<nil> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2000-02-05 04:04:04.000 [warn] edge cases zero_int=0 neg_int=-999 max_int64=9223372036854775807 min_int64=-9223372036854775808 max_uint64=18446744073709551615 zero_float=0 neg_float=-123.456 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2000-02-05 04:04:04.000 [info] primitives bool_true=true bool_false=false int=42 int8=-8 int16=-16 int32=-32 int64=-64 uint=42 uint8=8 uint16=16 uint32=32 uint64=64 float32=3.14 float64=2.71828 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍