Skip to content
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

Do not cache reflect.ValueOf() in metrics Labels #649

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions sdk/metric/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,38 @@ func BenchmarkBatchRecord_8Labels_4Instruments(b *testing.B) {
func BenchmarkBatchRecord_8Labels_8Instruments(b *testing.B) {
benchmarkBatchRecord8Labels(b, 8)
}

// LabelIterator
func BenchmarkLabelIterator(b *testing.B) {
const labelCount = 1024
ctx := context.Background()
fix := newFixture(b)

var rec export.Record
fix.pcb = func(_ context.Context, processRec export.Record) error {
rec = processRec
return nil
}

keyValues := makeLabels(labelCount)
counter := fix.meter.NewInt64Counter("test.counter")
counter.Add(ctx, 1, keyValues...)

fix.sdk.Collect(ctx)

b.ResetTimer()

labels := rec.Labels()
iter := labels.Iter()
var val core.KeyValue
for i := 0; i < b.N; i++ {
if !iter.Next() {
iter = labels.Iter()
iter.Next()
}
val = iter.Label()
}
if false {
fmt.Println(val)
}
}
14 changes: 5 additions & 9 deletions sdk/metric/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ type (
// the labels, copied into an array of the correct
// size for use as a map key.
ordered orderedLabels

// cachedValue contains a `reflect.Value` of the `ordered`
// member
cachedValue reflect.Value
}

// mapkey uniquely describes a metric instrument in terms of
Expand Down Expand Up @@ -174,8 +170,7 @@ var (
kvType = reflect.TypeOf(core.KeyValue{})

emptyLabels = labels{
ordered: [0]core.KeyValue{},
cachedValue: reflect.ValueOf([0]core.KeyValue{}),
ordered: [0]core.KeyValue{},
}
)

Expand Down Expand Up @@ -393,13 +388,15 @@ func (m *SDK) makeLabels(kvs []core.KeyValue, sortSlice *sortedLabels) labels {
// NumLabels is a part of an implementation of the export.LabelStorage
// interface.
func (ls *labels) NumLabels() int {
return ls.cachedValue.Len()
return reflect.ValueOf(ls.ordered).Len()
}

// GetLabel is a part of an implementation of the export.LabelStorage
// interface.
func (ls *labels) GetLabel(idx int) core.KeyValue {
return ls.cachedValue.Index(idx).Interface().(core.KeyValue)
// Note: The Go compiler successfully avoids an allocation for
// the interface{} conversion here:
return reflect.ValueOf(ls.ordered).Index(idx).Interface().(core.KeyValue)
}

// Iter is a part of an implementation of the export.Labels interface.
Expand Down Expand Up @@ -459,7 +456,6 @@ func computeOrderedLabels(kvs []core.KeyValue) labels {
if ls.ordered == nil {
ls.ordered = computeOrderedReflect(kvs)
}
ls.cachedValue = reflect.ValueOf(ls.ordered)
return ls
}

Expand Down