Skip to content

Commit

Permalink
[improve][fn] Optimize string concatenation in user metrics gen (#20499)
Browse files Browse the repository at this point in the history
### Motivation

Minor optimization for metrics generation in functions. String concatenation is faster than string format. This is valuable in the context of metrics where we can generate many strings in a tight loop.

### Modifications

* Replace `String.format` with string concatenation in several locations.
* Only create metrics prefix once.

### Verifying this change

This change is a trivial rework / code cleanup without any test coverage.

### Documentation

- [x] `doc-not-needed`
  • Loading branch information
michaeljmarshall committed Jun 5, 2023
1 parent 4a89f02 commit b1822ed
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,13 @@ public Map<String, Double> getMetrics() {
String metricName = userMetricsLabelsEntry.getKey();
String[] labels = userMetricsLabelsEntry.getValue();
Summary.Child.Value summary = userMetricsSummary.labels(labels).get();
metricsMap.put(String.format("%s%s_sum", USER_METRIC_PREFIX, metricName), summary.sum);
metricsMap.put(String.format("%s%s_count", USER_METRIC_PREFIX, metricName), summary.count);
String prefix = USER_METRIC_PREFIX + metricName + "_";
metricsMap.put(prefix + "sum", summary.sum);
metricsMap.put(prefix + "count", summary.count);
for (Map.Entry<Double, Double> entry : summary.quantiles.entrySet()) {
Double quantile = entry.getKey();
Double value = entry.getValue();
metricsMap.put(String.format("%s%s_%s", USER_METRIC_PREFIX, metricName, quantile), value);
metricsMap.put(prefix + quantile, value);
}
}
return metricsMap;
Expand Down

0 comments on commit b1822ed

Please sign in to comment.