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

Commit bf52d9d

Browse files
authored
Remove convTslice calls in Record() (#1268)
* Remove `convTslice` calls in `Record()` This is built upon #1267; that one should likely merge first. I split this out as it has a small public API change (to work around circular imports) to avoid issues on the first PR. Benchmark relative to #1267: ``` me old time/op new time/op delta Record0-6 1.74ns ± 4% 1.79ns ± 2% +2.85% (p=0.238 n=5+5) Record1-6 634ns ± 6% 542ns ± 9% -14.55% (p=0.008 n=5+5) Record8-6 1.21µs ± 5% 1.23µs ± 2% +1.97% (p=0.254 n=5+5) Record8_WithRecorder-6 777ns ± 5% 792ns ± 5% +1.97% (p=0.421 n=5+5) Record8_Parallel-6 1.26µs ±24% 1.22µs ± 2% ~ (p=0.690 n=5+5) Record8_8Tags-6 1.23µs ± 2% 1.25µs ± 3% ~ (p=0.651 n=5+5) name old alloc/op new alloc/op delta Record0-6 0.00B 0.00B ~ (all equal) Record1-6 120B ± 0% 96B ± 0% -20.00% (p=0.008 n=5+5) Record8-6 344B ± 0% 320B ± 0% -6.98% (p=0.008 n=5+5) Record8_WithRecorder-6 424B ± 0% 424B ± 0% ~ (all equal) Record8_Parallel-6 344B ± 0% 320B ± 0% -6.98% (p=0.008 n=5+5) Record8_8Tags-6 344B ± 0% 320B ± 0% -6.98% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Record0-6 0.00 0.00 ~ (all equal) Record1-6 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) Record8-6 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) Record8_WithRecorder-6 4.00 ± 0% 4.00 ± 0% ~ (all equal) Record8_Parallel-6 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) Record8_8Tags-6 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) ``` * Refactor to avoid leaking into public API
1 parent ad0b46e commit bf52d9d

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

stats/internal/record.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ import (
2121
// DefaultRecorder will be called for each Record call.
2222
var DefaultRecorder func(tags *tag.Map, measurement interface{}, attachments map[string]interface{})
2323

24+
// MeasurementRecorder will be called for each Record call. This is the same as DefaultRecorder but
25+
// avoids interface{} conversion.
26+
// This will be a func(tags *tag.Map, measurement []Measurement, attachments map[string]interface{}) type,
27+
// but is interface{} here to avoid import loops
28+
var MeasurementRecorder interface{}
29+
2430
// SubscriptionReporter reports when a view subscribed with a measure.
2531
var SubscriptionReporter func(measure string)

stats/record.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func createRecordOption(ros ...Options) *recordOptions {
8686
return o
8787
}
8888

89+
type measurementRecorder = func(tags *tag.Map, measurement []Measurement, attachments map[string]interface{})
90+
8991
// Record records one or multiple measurements with the same context at once.
9092
// If there are any tags in the context, measurements will be tagged with them.
9193
func Record(ctx context.Context, ms ...Measurement) {
@@ -94,7 +96,7 @@ func Record(ctx context.Context, ms ...Measurement) {
9496
if len(ms) == 0 {
9597
return
9698
}
97-
recorder := internal.DefaultRecorder
99+
recorder := internal.MeasurementRecorder.(measurementRecorder)
98100
record := false
99101
for _, m := range ms {
100102
if m.desc.subscribed() {

stats/view/worker.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func init() {
3333
defaultWorker = NewMeter().(*worker)
3434
go defaultWorker.start()
3535
internal.DefaultRecorder = record
36+
internal.MeasurementRecorder = recordMeasurement
3637
}
3738

3839
type measureRef struct {
@@ -199,11 +200,21 @@ func record(tags *tag.Map, ms interface{}, attachments map[string]interface{}) {
199200
defaultWorker.Record(tags, ms, attachments)
200201
}
201202

203+
func recordMeasurement(tags *tag.Map, ms []stats.Measurement, attachments map[string]interface{}) {
204+
defaultWorker.recordMeasurement(tags, ms, attachments)
205+
}
206+
202207
// Record records a set of measurements ms associated with the given tags and attachments.
203208
func (w *worker) Record(tags *tag.Map, ms interface{}, attachments map[string]interface{}) {
209+
w.recordMeasurement(tags, ms.([]stats.Measurement), attachments)
210+
}
211+
212+
// recordMeasurement records a set of measurements ms associated with the given tags and attachments.
213+
// This is the same as Record but without an interface{} type to avoid allocations
214+
func (w *worker) recordMeasurement(tags *tag.Map, ms []stats.Measurement, attachments map[string]interface{}) {
204215
req := &recordReq{
205216
tm: tags,
206-
ms: ms.([]stats.Measurement),
217+
ms: ms,
207218
attachments: attachments,
208219
t: time.Now(),
209220
}

0 commit comments

Comments
 (0)