-
Notifications
You must be signed in to change notification settings - Fork 453
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
[aggregator] Avoid large copies in entry rollup comparisons by making them more inline-friendly #3195
[aggregator] Avoid large copies in entry rollup comparisons by making them more inline-friendly #3195
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package aggregator | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/m3db/m3/src/metrics/aggregation" | ||
"github.com/m3db/m3/src/metrics/pipeline" | ||
"github.com/m3db/m3/src/metrics/pipeline/applied" | ||
"github.com/m3db/m3/src/metrics/policy" | ||
"github.com/m3db/m3/src/metrics/transformation" | ||
xtime "github.com/m3db/m3/src/x/time" | ||
) | ||
|
||
func BenchmarkAggregationValues(b *testing.B) { | ||
aggregationKeys := []aggregationKey{ | ||
{ | ||
aggregationID: aggregation.DefaultID, | ||
storagePolicy: policy.NewStoragePolicy(10*time.Second, xtime.Second, 48*time.Hour), | ||
pipeline: applied.NewPipeline([]applied.OpUnion{ | ||
{ | ||
Type: pipeline.TransformationOpType, | ||
Transformation: pipeline.TransformationOp{Type: transformation.Absolute}, | ||
}, | ||
{ | ||
Type: pipeline.RollupOpType, | ||
Rollup: applied.RollupOp{ | ||
ID: []byte("foo.baz1234"), | ||
AggregationID: aggregation.DefaultID, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
aggregationID: aggregation.DefaultID, | ||
storagePolicy: policy.NewStoragePolicy(10*time.Second, xtime.Second, 48*time.Hour), | ||
pipeline: applied.NewPipeline([]applied.OpUnion{ | ||
{ | ||
Type: pipeline.TransformationOpType, | ||
Transformation: pipeline.TransformationOp{Type: transformation.Absolute}, | ||
}, | ||
{ | ||
Type: pipeline.RollupOpType, | ||
Rollup: applied.RollupOp{ | ||
ID: []byte("foo.baz55"), | ||
AggregationID: aggregation.DefaultID, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
aggregationID: aggregation.DefaultID, | ||
storagePolicy: policy.NewStoragePolicy(10*time.Second, xtime.Second, 48*time.Hour), | ||
pipeline: applied.NewPipeline([]applied.OpUnion{ | ||
{ | ||
Type: pipeline.TransformationOpType, | ||
Transformation: pipeline.TransformationOp{Type: transformation.Absolute}, | ||
}, | ||
{ | ||
Type: pipeline.RollupOpType, | ||
Rollup: applied.RollupOp{ | ||
ID: []byte("foo.baz45"), | ||
AggregationID: aggregation.DefaultID, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
aggregationID: aggregation.DefaultID, | ||
storagePolicy: policy.NewStoragePolicy(10*time.Second, xtime.Second, 48*time.Hour), | ||
pipeline: applied.NewPipeline([]applied.OpUnion{ | ||
{ | ||
Type: pipeline.TransformationOpType, | ||
Transformation: pipeline.TransformationOp{Type: transformation.Absolute}, | ||
}, | ||
{ | ||
Type: pipeline.RollupOpType, | ||
Rollup: applied.RollupOp{ | ||
ID: []byte("foo.baz1234"), | ||
AggregationID: aggregation.DefaultID, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
aggregationID: aggregation.DefaultID, | ||
storagePolicy: policy.NewStoragePolicy(10*time.Second, xtime.Second, 48*time.Hour), | ||
pipeline: applied.NewPipeline([]applied.OpUnion{ | ||
{ | ||
Type: pipeline.TransformationOpType, | ||
Transformation: pipeline.TransformationOp{Type: transformation.Absolute}, | ||
}, | ||
{ | ||
Type: pipeline.RollupOpType, | ||
Rollup: applied.RollupOp{ | ||
ID: []byte("foo.baz42"), | ||
AggregationID: aggregation.DefaultID, | ||
}, | ||
}, | ||
}), | ||
}, | ||
} | ||
|
||
vals := make(aggregationValues, len(aggregationKeys)) | ||
for i := range aggregationKeys { | ||
vals[i] = aggregationValue{key: aggregationKeys[i]} | ||
} | ||
|
||
b.ResetTimer() | ||
var contains bool | ||
for n := 0; n < b.N; n++ { | ||
contains = vals.contains(aggregationKeys[len(aggregationKeys)-1]) | ||
} | ||
runtime.KeepAlive(contains) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -474,8 +474,8 @@ func (agg *forwardedAggregation) onDone(key aggregationKey) error { | |
} | ||
|
||
func (agg *forwardedAggregation) index(key aggregationKey) int { | ||
for i, k := range agg.byKey { | ||
if k.key.Equal(key) { | ||
for i := range agg.byKey { | ||
if agg.byKey[i].key.Equal(key) { | ||
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. Dumb question: this is faster? I'm surprised haha--more inlining issues? 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. stack copies due to how range works 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. Ah nice, TIL. |
||
return i | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package metadata | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func isDefault(m StagedMetadatas) bool { | ||
return m.IsDefault() | ||
} | ||
|
||
func BenchmarkMetadata_IsDefault(b *testing.B) { | ||
m := testLargeStagedMetadatas | ||
m = append(m, testLargeStagedMetadatas...) | ||
for i := 0; i < b.N; i++ { | ||
m[0].CutoverNanos = int64(i) | ||
if isDefault(m) { | ||
b.Fail() | ||
} | ||
} | ||
runtime.KeepAlive(m) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,25 +91,25 @@ type OpUnion struct { | |
|
||
// Equal determines whether two operation unions are equal. | ||
func (u OpUnion) Equal(other OpUnion) bool { | ||
// keep in sync with Pipeline.Equal as go is terrible at inlining anything with a loop | ||
if u.Type != other.Type { | ||
return false | ||
} | ||
switch u.Type { | ||
case pipeline.TransformationOpType: | ||
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. what about this case? 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. The definition is |
||
return u.Transformation.Equal(other.Transformation) | ||
case pipeline.RollupOpType: | ||
|
||
if u.Type == pipeline.RollupOpType { | ||
return u.Rollup.Equal(other.Rollup) | ||
} | ||
return true | ||
|
||
return u.Transformation.Type == other.Transformation.Type | ||
} | ||
|
||
// Clone clones an operation union. | ||
func (u OpUnion) Clone() OpUnion { | ||
clone := OpUnion{Type: u.Type} | ||
switch u.Type { | ||
case pipeline.TransformationOpType: | ||
clone.Transformation = u.Transformation.Clone() | ||
case pipeline.RollupOpType: | ||
clone := OpUnion{ | ||
Type: u.Type, | ||
Transformation: u.Transformation, | ||
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. does this need a clone as well? if so, two ifs is probably going to put you back in switch land branch wise 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. nope, copying an int > copying the same single int conditionally |
||
} | ||
if u.Type == pipeline.RollupOpType { | ||
clone.Rollup = u.Rollup.Clone() | ||
} | ||
return clone | ||
|
@@ -187,22 +187,36 @@ func (p Pipeline) At(i int) OpUnion { return p.operations[i] } | |
|
||
// Equal determines whether two pipelines are equal. | ||
func (p Pipeline) Equal(other Pipeline) bool { | ||
// keep in sync with OpUnion.Equal as go is terrible at inlining anything with a loop | ||
if len(p.operations) != len(other.operations) { | ||
return false | ||
} | ||
|
||
for i := 0; i < len(p.operations); i++ { | ||
if !p.operations[i].Equal(other.operations[i]) { | ||
if p.operations[i].Type != other.operations[i].Type { | ||
return false | ||
} | ||
//nolint:exhaustive | ||
switch p.operations[i].Type { | ||
case pipeline.RollupOpType: | ||
if !p.operations[i].Rollup.Equal(other.operations[i].Rollup) { | ||
return false | ||
} | ||
case pipeline.TransformationOpType: | ||
if p.operations[i].Transformation.Type != other.operations[i].Transformation.Type { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Clone clones the pipeline. | ||
func (p Pipeline) Clone() Pipeline { | ||
clone := make([]OpUnion, len(p.operations)) | ||
for i, op := range p.operations { | ||
clone[i] = op.Clone() | ||
for i := range p.operations { | ||
clone[i] = p.operations[i].Clone() | ||
} | ||
return Pipeline{operations: clone} | ||
} | ||
|
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.
Hmm dunno if this is possible, but any way to add a fuzz test comparing behavior using equals vs using the manual inline here? Less to test the current version of the code than to ensure against future regressions haha.
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.
Also, maybe worth adding a pointer from
Equal
to here to make sure anyone changing that method also changes this one?Makes one wish for macros...
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.
good point as I was just lazy, but then again I missed something in tests :D