Skip to content

Commit 433f564

Browse files
authored
Excessive allocations in Firebase Trace.incrementMetric (#7984)
**Fixes:** #1923 **Quick Summary:** Current PR addresses an optimization at PerfMetricValidator.validateTraceName(), it also gates the Metric logging after this validation succeed within Trace.incrementMetric() (allowing the log building if the flag is enabled) and optimizes this String building process (using String template instead of String.format).
1 parent 25e26e0 commit 433f564

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

firebase-perf/src/main/java/com/google/firebase/perf/metrics/Trace.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,16 @@ public void incrementMetric(@NonNull String metricName, long incrementBy) {
348348
// thread-safer
349349
Counter counter = obtainOrCreateCounterByName(metricName.trim());
350350
counter.increment(incrementBy);
351-
logger.debug(
352-
"Incrementing metric '%s' to %d on trace '%s'", metricName, counter.getCount(), name);
351+
if (logger.isLogcatEnabled()) {
352+
logger.debug(
353+
"Incrementing metric '"
354+
+ metricName
355+
+ "' to "
356+
+ counter.getCount()
357+
+ " on trace '"
358+
+ name
359+
+ "'");
360+
}
353361
}
354362

355363
/**

firebase-perf/src/main/java/com/google/firebase/perf/metrics/validator/PerfMetricValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public abstract class PerfMetricValidator {
3333
private static final Pattern ATTRIBUTE_KEY_PATTERN =
3434
Pattern.compile("^(?!(firebase_|google_|ga_))[A-Za-z][A-Za-z_0-9]*");
3535

36+
private static final Constants.CounterNames[] validCounterNames = Constants.CounterNames.values();
37+
3638
/**
3739
* Creates a list of PerfMetricValidator classes based on the contents of PerfMetric
3840
*
@@ -126,7 +128,6 @@ public static String validateMetricName(@Nullable String str) {
126128
return String.format(
127129
Locale.US, "Metric name must not exceed %d characters", Constants.MAX_COUNTER_ID_LENGTH);
128130
} else if (str.startsWith("_")) {
129-
Constants.CounterNames[] validCounterNames = Constants.CounterNames.values();
130131
for (Constants.CounterNames counterName : validCounterNames) {
131132
if (counterName.toString().equals(str)) {
132133
return null;

0 commit comments

Comments
 (0)