-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Metric histogram aggregator: Swap in SynchronizedMove to avoid allocations #1435
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 5 commits
69fc555
bf72ffb
ec57b79
62bc581
539a2e1
58aff97
2381491
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 |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ type ( | |
| lock sync.Mutex | ||
| boundaries []float64 | ||
| kind number.Kind | ||
| state state | ||
| state *state | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. State has only 40B, not sure you need to use pointer here. |
||
| } | ||
|
|
||
| // state represents the state of a histogram, consisting of | ||
|
|
@@ -78,8 +78,8 @@ func New(cnt int, desc *metric.Descriptor, boundaries []float64) []Aggregator { | |
| aggs[i] = Aggregator{ | ||
| kind: desc.NumberKind(), | ||
| boundaries: sortedBoundaries, | ||
| state: emptyState(sortedBoundaries), | ||
| } | ||
| aggs[i].state = aggs[i].newState() | ||
| } | ||
| return aggs | ||
| } | ||
|
|
@@ -123,20 +123,42 @@ func (c *Aggregator) SynchronizedMove(oa export.Aggregator, desc *metric.Descrip | |
| return aggregator.NewInconsistentAggregatorError(c, oa) | ||
| } | ||
|
|
||
| if o != nil { | ||
| // Swap case: This is the ordinary case for a | ||
| // synchronous instrument, where the SDK allocates two | ||
| // Aggregators and lock contention is anticipated. | ||
| // Reset the target state before swapping it under the | ||
| // lock below. | ||
| o.clearState() | ||
| } | ||
|
|
||
| c.lock.Lock() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the |
||
| if o != nil { | ||
| o.state = c.state | ||
| c.state, o.state = o.state, c.state | ||
| } else { | ||
| // No swap case: This is the ordinary case for an | ||
| // asynchronous instrument, where the SDK allocates a | ||
| // single Aggregator and there is no anticipated lock | ||
| // contention. | ||
| c.clearState() | ||
| } | ||
| c.state = emptyState(c.boundaries) | ||
| c.lock.Unlock() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func emptyState(boundaries []float64) state { | ||
| return state{ | ||
| bucketCounts: make([]uint64, len(boundaries)+1), | ||
| func (c *Aggregator) newState() *state { | ||
| return &state{ | ||
| bucketCounts: make([]uint64, len(c.boundaries)+1), | ||
| } | ||
| } | ||
|
|
||
| func (c *Aggregator) clearState() { | ||
| for i := range c.state.bucketCounts { | ||
| c.state.bucketCounts[i] = 0 | ||
| } | ||
| c.state.sum = 0 | ||
| c.state.count = 0 | ||
| } | ||
|
|
||
| // Update adds the recorded measurement to the current data set. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.