mirror of
https://github.com/golang/go
synced 2024-11-18 10:54:40 -07:00
internal/telemetry: change tag to use type specific storage
Because packing values into interface{} causes allocations this gives us a major improvement on allocation count, but tag is bigger so a significant loss on bytes allocated. It is a minor performance win overall, especially in the null exporter case when it really matters the most. name old time/op new time/op delta /Baseline-8 147ns ± 3% 146ns ± 3% ~ (p=0.349 n=5+5) /StdLog-8 6.83µs ± 1% 6.78µs ± 2% ~ (p=0.548 n=5+5) /LogNoExporter-8 1.15µs ± 1% 1.05µs ± 1% -8.63% (p=0.008 n=5+5) /TraceNoExporter-8 1.01µs ± 4% 0.99µs ± 1% ~ (p=0.087 n=5+5) /StatsNoExporter-8 1.89µs ± 0% 1.95µs ± 3% +3.27% (p=0.016 n=4+5) /Log-8 23.7µs ± 1% 24.4µs ± 6% +3.14% (p=0.032 n=5+5) /Trace-8 42.2µs ± 1% 42.2µs ± 1% ~ (p=0.841 n=5+5) /Stats-8 7.37µs ± 3% 7.10µs ± 1% -3.74% (p=0.008 n=5+5) name old alloc/op new alloc/op delta /Baseline-8 0.00B 0.00B ~ (all equal) /StdLog-8 552B ± 0% 552B ± 0% ~ (all equal) /LogNoExporter-8 680B ± 0% 1024B ± 0% +50.59% (p=0.008 n=5+5) /TraceNoExporter-8 512B ± 0% 512B ± 0% ~ (all equal) /StatsNoExporter-8 1.26kB ± 0% 2.05kB ± 0% +62.03% (p=0.008 n=5+5) /Log-8 5.20kB ± 0% 6.06kB ± 0% +16.46% (p=0.008 n=5+5) /Trace-8 11.8kB ± 0% 11.8kB ± 0% ~ (p=0.167 n=5+5) /Stats-8 2.29kB ± 0% 3.07kB ± 0% +34.27% (p=0.008 n=5+5) name old allocs/op new allocs/op delta /Baseline-8 0.00 0.00 ~ (all equal) /StdLog-8 30.0 ± 0% 30.0 ± 0% ~ (all equal) /LogNoExporter-8 30.0 ± 0% 16.0 ± 0% -46.67% (p=0.008 n=5+5) /TraceNoExporter-8 16.0 ± 0% 16.0 ± 0% ~ (all equal) /StatsNoExporter-8 62.0 ± 0% 32.0 ± 0% -48.39% (p=0.008 n=5+5) /Log-8 172 ± 0% 158 ± 0% -8.14% (p=0.008 n=5+5) /Trace-8 336 ± 0% 336 ± 0% ~ (all equal) /Stats-8 94.0 ± 0% 64.0 ± 0% -31.91% (p=0.008 n=5+5) Change-Id: I81d2443c9a32d25a5b60fffa60759caa33566da2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/225381 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
3db5fc6bac
commit
f8bbb56955
@ -4,6 +4,8 @@
|
||||
|
||||
package event
|
||||
|
||||
import "math"
|
||||
|
||||
var (
|
||||
// Err is a key used to add error values to tag lists.
|
||||
Err = NewErrorKey("error", "")
|
||||
@ -48,10 +50,10 @@ func (k *ValueKey) Get(tags TagMap) interface{} {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *ValueKey) From(t Tag) interface{} { return t.Value }
|
||||
func (k *ValueKey) From(t Tag) interface{} { return t.untyped }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *ValueKey) Of(value interface{}) Tag { return Tag{Key: k, Value: value} }
|
||||
func (k *ValueKey) Of(value interface{}) Tag { return Tag{Key: k, untyped: value} }
|
||||
|
||||
// IntKey represents a key
|
||||
type IntKey struct {
|
||||
@ -68,7 +70,7 @@ func (k *IntKey) Name() string { return k.name }
|
||||
func (k *IntKey) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *IntKey) Of(v int) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *IntKey) Of(v int) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *IntKey) Get(tags TagMap) int {
|
||||
@ -79,7 +81,7 @@ func (k *IntKey) Get(tags TagMap) int {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *IntKey) From(t Tag) int { return t.Value.(int) }
|
||||
func (k *IntKey) From(t Tag) int { return int(t.packed) }
|
||||
|
||||
// Int8Key represents a key
|
||||
type Int8Key struct {
|
||||
@ -96,7 +98,7 @@ func (k *Int8Key) Name() string { return k.name }
|
||||
func (k *Int8Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *Int8Key) Of(v int8) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *Int8Key) Of(v int8) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *Int8Key) Get(tags TagMap) int8 {
|
||||
@ -107,7 +109,7 @@ func (k *Int8Key) Get(tags TagMap) int8 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *Int8Key) From(t Tag) int8 { return t.Value.(int8) }
|
||||
func (k *Int8Key) From(t Tag) int8 { return int8(t.packed) }
|
||||
|
||||
// Int16Key represents a key
|
||||
type Int16Key struct {
|
||||
@ -124,7 +126,7 @@ func (k *Int16Key) Name() string { return k.name }
|
||||
func (k *Int16Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *Int16Key) Of(v int16) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *Int16Key) Of(v int16) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *Int16Key) Get(tags TagMap) int16 {
|
||||
@ -135,7 +137,7 @@ func (k *Int16Key) Get(tags TagMap) int16 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *Int16Key) From(t Tag) int16 { return t.Value.(int16) }
|
||||
func (k *Int16Key) From(t Tag) int16 { return int16(t.packed) }
|
||||
|
||||
// Int32Key represents a key
|
||||
type Int32Key struct {
|
||||
@ -152,7 +154,7 @@ func (k *Int32Key) Name() string { return k.name }
|
||||
func (k *Int32Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *Int32Key) Of(v int32) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *Int32Key) Of(v int32) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *Int32Key) Get(tags TagMap) int32 {
|
||||
@ -163,7 +165,7 @@ func (k *Int32Key) Get(tags TagMap) int32 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *Int32Key) From(t Tag) int32 { return t.Value.(int32) }
|
||||
func (k *Int32Key) From(t Tag) int32 { return int32(t.packed) }
|
||||
|
||||
// Int64Key represents a key
|
||||
type Int64Key struct {
|
||||
@ -180,7 +182,7 @@ func (k *Int64Key) Name() string { return k.name }
|
||||
func (k *Int64Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *Int64Key) Of(v int64) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *Int64Key) Of(v int64) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *Int64Key) Get(tags TagMap) int64 {
|
||||
@ -191,7 +193,7 @@ func (k *Int64Key) Get(tags TagMap) int64 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *Int64Key) From(t Tag) int64 { return t.Value.(int64) }
|
||||
func (k *Int64Key) From(t Tag) int64 { return int64(t.packed) }
|
||||
|
||||
// UIntKey represents a key
|
||||
type UIntKey struct {
|
||||
@ -208,7 +210,7 @@ func (k *UIntKey) Name() string { return k.name }
|
||||
func (k *UIntKey) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *UIntKey) Of(v uint) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *UIntKey) Of(v uint) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *UIntKey) Get(tags TagMap) uint {
|
||||
@ -219,7 +221,7 @@ func (k *UIntKey) Get(tags TagMap) uint {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *UIntKey) From(t Tag) uint { return t.Value.(uint) }
|
||||
func (k *UIntKey) From(t Tag) uint { return uint(t.packed) }
|
||||
|
||||
// UInt8Key represents a key
|
||||
type UInt8Key struct {
|
||||
@ -236,7 +238,7 @@ func (k *UInt8Key) Name() string { return k.name }
|
||||
func (k *UInt8Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *UInt8Key) Of(v uint8) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *UInt8Key) Of(v uint8) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *UInt8Key) Get(tags TagMap) uint8 {
|
||||
@ -247,7 +249,7 @@ func (k *UInt8Key) Get(tags TagMap) uint8 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *UInt8Key) From(t Tag) uint8 { return t.Value.(uint8) }
|
||||
func (k *UInt8Key) From(t Tag) uint8 { return uint8(t.packed) }
|
||||
|
||||
// UInt16Key represents a key
|
||||
type UInt16Key struct {
|
||||
@ -264,7 +266,7 @@ func (k *UInt16Key) Name() string { return k.name }
|
||||
func (k *UInt16Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *UInt16Key) Of(v uint16) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *UInt16Key) Of(v uint16) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *UInt16Key) Get(tags TagMap) uint16 {
|
||||
@ -275,7 +277,7 @@ func (k *UInt16Key) Get(tags TagMap) uint16 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *UInt16Key) From(t Tag) uint16 { return t.Value.(uint16) }
|
||||
func (k *UInt16Key) From(t Tag) uint16 { return uint16(t.packed) }
|
||||
|
||||
// UInt32Key represents a key
|
||||
type UInt32Key struct {
|
||||
@ -292,7 +294,7 @@ func (k *UInt32Key) Name() string { return k.name }
|
||||
func (k *UInt32Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *UInt32Key) Of(v uint32) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *UInt32Key) Of(v uint32) Tag { return Tag{Key: k, packed: uint64(v)} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *UInt32Key) Get(tags TagMap) uint32 {
|
||||
@ -303,7 +305,7 @@ func (k *UInt32Key) Get(tags TagMap) uint32 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *UInt32Key) From(t Tag) uint32 { return t.Value.(uint32) }
|
||||
func (k *UInt32Key) From(t Tag) uint32 { return uint32(t.packed) }
|
||||
|
||||
// UInt64Key represents a key
|
||||
type UInt64Key struct {
|
||||
@ -320,7 +322,7 @@ func (k *UInt64Key) Name() string { return k.name }
|
||||
func (k *UInt64Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *UInt64Key) Of(v uint64) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *UInt64Key) Of(v uint64) Tag { return Tag{Key: k, packed: v} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *UInt64Key) Get(tags TagMap) uint64 {
|
||||
@ -331,7 +333,7 @@ func (k *UInt64Key) Get(tags TagMap) uint64 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *UInt64Key) From(t Tag) uint64 { return t.Value.(uint64) }
|
||||
func (k *UInt64Key) From(t Tag) uint64 { return t.packed }
|
||||
|
||||
// Float32Key represents a key
|
||||
type Float32Key struct {
|
||||
@ -348,7 +350,9 @@ func (k *Float32Key) Name() string { return k.name }
|
||||
func (k *Float32Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *Float32Key) Of(v float32) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *Float32Key) Of(v float32) Tag {
|
||||
return Tag{Key: k, packed: uint64(math.Float32bits(v))}
|
||||
}
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *Float32Key) Get(tags TagMap) float32 {
|
||||
@ -359,7 +363,9 @@ func (k *Float32Key) Get(tags TagMap) float32 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *Float32Key) From(t Tag) float32 { return t.Value.(float32) }
|
||||
func (k *Float32Key) From(t Tag) float32 {
|
||||
return math.Float32frombits(uint32(t.packed))
|
||||
}
|
||||
|
||||
// Float64Key represents a key
|
||||
type Float64Key struct {
|
||||
@ -376,7 +382,9 @@ func (k *Float64Key) Name() string { return k.name }
|
||||
func (k *Float64Key) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *Float64Key) Of(v float64) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *Float64Key) Of(v float64) Tag {
|
||||
return Tag{Key: k, packed: math.Float64bits(v)}
|
||||
}
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *Float64Key) Get(tags TagMap) float64 {
|
||||
@ -387,7 +395,9 @@ func (k *Float64Key) Get(tags TagMap) float64 {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *Float64Key) From(t Tag) float64 { return t.Value.(float64) }
|
||||
func (k *Float64Key) From(t Tag) float64 {
|
||||
return math.Float64frombits(t.packed)
|
||||
}
|
||||
|
||||
// StringKey represents a key
|
||||
type StringKey struct {
|
||||
@ -404,7 +414,7 @@ func (k *StringKey) Name() string { return k.name }
|
||||
func (k *StringKey) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *StringKey) Of(v string) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *StringKey) Of(v string) Tag { return Tag{Key: k, str: v} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *StringKey) Get(tags TagMap) string {
|
||||
@ -415,7 +425,7 @@ func (k *StringKey) Get(tags TagMap) string {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *StringKey) From(t Tag) string { return t.Value.(string) }
|
||||
func (k *StringKey) From(t Tag) string { return t.str }
|
||||
|
||||
// BooleanKey represents a key
|
||||
type BooleanKey struct {
|
||||
@ -432,7 +442,13 @@ func (k *BooleanKey) Name() string { return k.name }
|
||||
func (k *BooleanKey) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *BooleanKey) Of(v bool) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *BooleanKey) Of(v bool) Tag {
|
||||
t := Tag{Key: k}
|
||||
if v {
|
||||
t.packed = 1
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *BooleanKey) Get(tags TagMap) bool {
|
||||
@ -443,7 +459,7 @@ func (k *BooleanKey) Get(tags TagMap) bool {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *BooleanKey) From(t Tag) bool { return t.Value.(bool) }
|
||||
func (k *BooleanKey) From(t Tag) bool { return t.packed > 0 }
|
||||
|
||||
// ErrorKey represents a key
|
||||
type ErrorKey struct {
|
||||
@ -460,7 +476,7 @@ func (k *ErrorKey) Name() string { return k.name }
|
||||
func (k *ErrorKey) Description() string { return k.description }
|
||||
|
||||
// Of creates a new Tag with this key and the supplied value.
|
||||
func (k *ErrorKey) Of(v error) Tag { return Tag{Key: k, Value: v} }
|
||||
func (k *ErrorKey) Of(v error) Tag { return Tag{Key: k, untyped: v} }
|
||||
|
||||
// Get can be used to get a tag for the key from a TagMap.
|
||||
func (k *ErrorKey) Get(tags TagMap) error {
|
||||
@ -471,4 +487,4 @@ func (k *ErrorKey) Get(tags TagMap) error {
|
||||
}
|
||||
|
||||
// From can be used to get a value from a Tag.
|
||||
func (k *ErrorKey) From(t Tag) error { return t.Value.(error) }
|
||||
func (k *ErrorKey) From(t Tag) error { return t.untyped.(error) }
|
||||
|
@ -11,8 +11,10 @@ import (
|
||||
// Tag holds a key and value pair.
|
||||
// It is normally used when passing around lists of tags.
|
||||
type Tag struct {
|
||||
Key Key
|
||||
Value interface{}
|
||||
Key Key
|
||||
packed uint64
|
||||
str string
|
||||
untyped interface{}
|
||||
}
|
||||
|
||||
// TagMap is the interface to a collection of Tags indexed by key.
|
||||
|
Loading…
Reference in New Issue
Block a user