-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Metric histogram aggregator: Swap in SynchronizedMove to avoid allocations #1435
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69fc555
Move emptyState() allocations outside lock
jmacd bf72ffb
Add more testing
jmacd ec57b79
Re-comment; add CHANGELOG
jmacd 62bc581
Add CHANGELOG PR number
jmacd 539a2e1
Upstream
jmacd 58aff97
Update CHANGELOG.md
2381491
Merge branch 'master' into jmacd/swaphisto
Aneurysm9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
// 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() | ||
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
State has only 40B, not sure you need to use pointer here.