-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Replace InitEmptyWithCapacity with EnsureCapacity and Clear #2845
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 |
---|---|---|
|
@@ -238,7 +238,7 @@ func setDataPoints(ocMetric *ocmetrics.Metric, metric pdata.Metric) { | |
} | ||
} | ||
|
||
func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics.LabelValue, labelsMap pdata.StringMap) { | ||
func fillLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics.LabelValue, labelsMap pdata.StringMap) { | ||
if len(ocLabelsKeys) == 0 || len(ocLabelValues) == 0 { | ||
return | ||
} | ||
|
@@ -250,7 +250,8 @@ func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics | |
lablesCount = len(ocLabelValues) | ||
} | ||
|
||
labelsMap.InitEmptyWithCapacity(lablesCount) | ||
labelsMap.Clear() | ||
labelsMap.EnsureCapacity(lablesCount) | ||
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. Change in behvaior if 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. Returning is not a good pattern for the way we want users to use our pdata. Returning means I need to have a In this case, as in all the other cases we start with an empty object during conversion, so there is no problem with having previous values. 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. In contrib I plan to replace with Clear + EnsureCapacity so I don't have to manually check all usages. 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. It is not apparent from setLabelsMap signature that labelsMap is empty. By looking at setLabelsMap code only it is not possible to tell if the code is correct. To know that the code is correct you have to analyse the call site where setLabelsMap. What do you think about calling Clear() here too (like you want to do in contrib), or declare the requirement in setLabelsMap doccomment that labelsMap must be empty? I think calling Clear() is fine, it should have very little cost. 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. Ok, just to make the reviewer happy :) 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. :-) |
||
for i := 0; i < lablesCount; i++ { | ||
if !ocLabelValues[i].GetHasValue() { | ||
continue | ||
|
@@ -280,7 +281,7 @@ func fillIntDataPoint(ocMetric *ocmetrics.Metric, dps pdata.IntDataPointSlice) { | |
|
||
dp.SetStartTimestamp(startTimestamp) | ||
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime())) | ||
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
dp.SetValue(point.GetInt64Value()) | ||
} | ||
} | ||
|
@@ -308,7 +309,7 @@ func fillDoubleDataPoint(ocMetric *ocmetrics.Metric, dps pdata.DoubleDataPointSl | |
|
||
dp.SetStartTimestamp(startTimestamp) | ||
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime())) | ||
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
dp.SetValue(point.GetDoubleValue()) | ||
} | ||
} | ||
|
@@ -336,7 +337,7 @@ func fillDoubleHistogramDataPoint(ocMetric *ocmetrics.Metric, dps pdata.Histogra | |
|
||
dp.SetStartTimestamp(startTimestamp) | ||
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime())) | ||
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
distributionValue := point.GetDistributionValue() | ||
dp.SetSum(distributionValue.GetSum()) | ||
dp.SetCount(uint64(distributionValue.GetCount())) | ||
|
@@ -368,7 +369,7 @@ func fillDoubleSummaryDataPoint(ocMetric *ocmetrics.Metric, dps pdata.SummaryDat | |
|
||
dp.SetStartTimestamp(startTimestamp) | ||
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime())) | ||
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap()) | ||
summaryValue := point.GetSummaryValue() | ||
dp.SetSum(summaryValue.GetSum().GetValue()) | ||
dp.SetCount(uint64(summaryValue.GetCount().GetValue())) | ||
|
@@ -414,12 +415,13 @@ func exemplarToMetrics(ocExemplar *ocmetrics.DistributionValue_Exemplar, exempla | |
if ocExemplar.GetTimestamp() != nil { | ||
exemplar.SetTimestamp(pdata.TimestampFromTime(ocExemplar.GetTimestamp().AsTime())) | ||
} | ||
exemplar.SetValue(ocExemplar.GetValue()) | ||
attachments := exemplar.FilteredLabels() | ||
ocAttachments := ocExemplar.GetAttachments() | ||
attachments.InitEmptyWithCapacity(len(ocAttachments)) | ||
exemplar.SetValue(ocExemplar.GetValue()) | ||
filteredLabels := exemplar.FilteredLabels() | ||
filteredLabels.Clear() | ||
filteredLabels.EnsureCapacity(len(ocAttachments)) | ||
for k, v := range ocAttachments { | ||
attachments.Upsert(k, v) | ||
filteredLabels.Upsert(k, v) | ||
} | ||
} | ||
|
||
|
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.
Append may be faster to increase the capacity, assuming memory manager has a way to extend the allocated block without reallocation (but I did not verify): https://github.com/golang/go/wiki/SliceTricks#extend
It may be worth benchmarking.
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.
We can play with the internal implementation.