Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Optimize Record() to avoid extra allocations #1267

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
10 changes: 9 additions & 1 deletion stats/benchmark_test.go
Expand Up @@ -109,5 +109,13 @@ func BenchmarkRecord8_8Tags(b *testing.B) {
}

func makeMeasure() *stats.Int64Measure {
return stats.Int64("m", "test measure", "")
m := stats.Int64("m", "test measure", "")
v := &view.View{
Measure: m,
Aggregation: view.Sum(),
}
if err := view.Register(v); err != nil {
panic(err.Error())
}
return m
}
19 changes: 18 additions & 1 deletion stats/record.go
Expand Up @@ -89,7 +89,24 @@ func createRecordOption(ros ...Options) *recordOptions {
// Record records one or multiple measurements with the same context at once.
// If there are any tags in the context, measurements will be tagged with them.
func Record(ctx context.Context, ms ...Measurement) {
RecordWithOptions(ctx, WithMeasurements(ms...))
// Record behaves the same as RecordWithOptions, but because we do not have to handle generic functionality
// (RecordOptions) we can reduce some allocations to speed up this hot path
if len(ms) == 0 {
return
}
recorder := internal.DefaultRecorder
record := false
for _, m := range ms {
if m.desc.subscribed() {
record = true
break
}
}
if !record {
return
}
recorder(tag.FromContext(ctx), ms, nil)
return
}

// RecordWithTags records one or multiple measurements at once.
Expand Down