Log histograms in String(Builder)Optimizer if they are indeed logged.

Change-Id: I7f226d270f3e5f856e5bf9cbfbacfc958c4e698d
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/string/StringBuilderOptimizer.java b/src/main/java/com/android/tools/r8/ir/optimize/string/StringBuilderOptimizer.java
index ad890ec..6588fed 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/string/StringBuilderOptimizer.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/string/StringBuilderOptimizer.java
@@ -103,7 +103,7 @@
     this.factory = appView.dexItemFactory();
     this.throwingInfo = ThrowingInfo.defaultForConstString(appView.options());
     this.optimizationConfiguration = new DefaultStringBuilderOptimizationConfiguration();
-    if (Log.ENABLED) {
+    if (Log.ENABLED && Log.isLoggingEnabledFor(StringBuilderOptimizer.class)) {
       histogramOfLengthOfAppendChains = new Object2IntArrayMap<>();
       histogramOfLengthOfEndResult = new Object2IntArrayMap<>();
       histogramOfLengthOfPartialAppendChains = new Object2IntArrayMap<>();
@@ -136,30 +136,35 @@
         "# builders w/ non-deterministic arg: %s", numberOfBuildersWithNonDeterministicArg);
     Log.info(getClass(), "# dead builders : %s", numberOfDeadBuilders);
     Log.info(getClass(), "# builders simplified: %s", numberOfBuildersSimplified);
-    assert histogramOfLengthOfAppendChains != null;
-    Log.info(getClass(), "------ histogram of StringBuilder append chain lengths ------");
-    histogramOfLengthOfAppendChains.forEach((chainSize, count) -> {
+    if (histogramOfLengthOfAppendChains != null) {
+      Log.info(getClass(), "------ histogram of StringBuilder append chain lengths ------");
+      histogramOfLengthOfAppendChains.forEach((chainSize, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", chainSize, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
+    if (histogramOfLengthOfEndResult != null) {
+      Log.info(getClass(), "------ histogram of StringBuilder result lengths ------");
+      histogramOfLengthOfEndResult.forEach((length, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
+    if (histogramOfLengthOfPartialAppendChains != null) {
       Log.info(getClass(),
-          "%s: %s (%s)", chainSize, StringUtils.times("*", Math.min(count, 53)), count);
-    });
-    assert histogramOfLengthOfEndResult != null;
-    Log.info(getClass(), "------ histogram of StringBuilder result lengths ------");
-    histogramOfLengthOfEndResult.forEach((length, count) -> {
-      Log.info(getClass(),
-          "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
-    });
-    assert histogramOfLengthOfPartialAppendChains != null;
-    Log.info(getClass(), "------ histogram of StringBuilder append chain lengths (partial) ------");
-    histogramOfLengthOfPartialAppendChains.forEach((chainSize, count) -> {
-      Log.info(getClass(),
-          "%s: %s (%s)", chainSize, StringUtils.times("*", Math.min(count, 53)), count);
-    });
-    assert histogramOfLengthOfPartialResult != null;
-    Log.info(getClass(), "------ histogram of StringBuilder partial result lengths ------");
-    histogramOfLengthOfPartialResult.forEach((length, count) -> {
-      Log.info(getClass(),
-          "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
-    });
+          "------ histogram of StringBuilder append chain lengths (partial) ------");
+      histogramOfLengthOfPartialAppendChains.forEach((chainSize, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", chainSize, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
+    if (histogramOfLengthOfPartialResult != null) {
+      Log.info(getClass(), "------ histogram of StringBuilder partial result lengths ------");
+      histogramOfLengthOfPartialResult.forEach((length, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
   }
 
   public void computeTrivialStringConcatenation(IRCode code) {
@@ -524,6 +529,9 @@
     }
 
     private void logHistogramOfChains(List<String> contents, boolean isPartial) {
+      if (!Log.ENABLED || !Log.isLoggingEnabledFor(StringOptimizer.class)) {
+        return;
+      }
       if (contents == null || contents.isEmpty()) {
         return;
       }
@@ -531,19 +539,23 @@
       Integer size = Integer.valueOf(contents.size());
       Integer length = Integer.valueOf(result.length());
       if (isPartial) {
+        assert histogramOfLengthOfPartialAppendChains != null;
         synchronized (histogramOfLengthOfPartialAppendChains) {
           int count = histogramOfLengthOfPartialAppendChains.getOrDefault(size, 0);
           histogramOfLengthOfPartialAppendChains.put(size, count + 1);
         }
+        assert histogramOfLengthOfPartialResult != null;
         synchronized (histogramOfLengthOfPartialResult) {
           int count = histogramOfLengthOfPartialResult.getOrDefault(length, 0);
           histogramOfLengthOfPartialResult.put(length, count + 1);
         }
       } else {
+        assert histogramOfLengthOfAppendChains != null;
         synchronized (histogramOfLengthOfAppendChains) {
           int count = histogramOfLengthOfAppendChains.getOrDefault(size, 0);
           histogramOfLengthOfAppendChains.put(size, count + 1);
         }
+        assert histogramOfLengthOfEndResult != null;
         synchronized (histogramOfLengthOfEndResult) {
           int count = histogramOfLengthOfEndResult.getOrDefault(length, 0);
           histogramOfLengthOfEndResult.put(length, count + 1);
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/string/StringOptimizer.java b/src/main/java/com/android/tools/r8/ir/optimize/string/StringOptimizer.java
index 11dd773..be3a112 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/string/StringOptimizer.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/string/StringOptimizer.java
@@ -58,7 +58,7 @@
     this.appView = appView;
     this.factory = appView.dexItemFactory();
     this.throwingInfo = ThrowingInfo.defaultForConstString(appView.options());
-    if (Log.ENABLED) {
+    if (Log.ENABLED && Log.isLoggingEnabledFor(StringOptimizer.class)) {
       numberOfComputedNames = new Object2IntArrayMap<>();
       numberOfDeferredComputationOfNames = new Object2IntArrayMap<>();
       histogramOfLengthOfNames = new Object2IntArrayMap<>();
@@ -77,30 +77,35 @@
         "# trivial operations on const-string: %s", numberOfSimplifiedOperations);
     Log.info(getClass(),
         "# trivial conversions from/to const-string: %s", numberOfSimplifiedConversions);
-    assert numberOfComputedNames != null;
-    Log.info(getClass(), "------ # of get*Name() computation ------");
-    numberOfComputedNames.forEach((kind, count) -> {
+    if (numberOfComputedNames != null) {
+      Log.info(getClass(), "------ # of get*Name() computation ------");
+      numberOfComputedNames.forEach((kind, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", kind, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
+    if (numberOfDeferredComputationOfNames != null) {
+      Log.info(getClass(), "------ # of deferred get*Name() computation ------");
+      numberOfDeferredComputationOfNames.forEach((kind, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", kind, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
+    if (histogramOfLengthOfNames != null) {
+      Log.info(getClass(), "------ histogram of get*Name() result lengths ------");
+      histogramOfLengthOfNames.forEach((length, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
+    if (histogramOfLengthOfDeferredNames != null) {
       Log.info(getClass(),
-          "%s: %s (%s)", kind, StringUtils.times("*", Math.min(count, 53)), count);
-    });
-    assert numberOfDeferredComputationOfNames != null;
-    Log.info(getClass(), "------ # of deferred get*Name() computation ------");
-    numberOfDeferredComputationOfNames.forEach((kind, count) -> {
-      Log.info(getClass(),
-          "%s: %s (%s)", kind, StringUtils.times("*", Math.min(count, 53)), count);
-    });
-    assert histogramOfLengthOfNames != null;
-    Log.info(getClass(), "------ histogram of get*Name() result lengths ------");
-    histogramOfLengthOfNames.forEach((length, count) -> {
-      Log.info(getClass(),
-          "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
-    });
-    assert histogramOfLengthOfDeferredNames != null;
-    Log.info(getClass(), "------ histogram of original type length for deferred get*Name() ------");
-    histogramOfLengthOfDeferredNames.forEach((length, count) -> {
-      Log.info(getClass(),
-          "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
-    });
+          "------ histogram of original type length for deferred get*Name() ------");
+      histogramOfLengthOfDeferredNames.forEach((length, count) -> {
+        Log.info(getClass(),
+            "%s: %s (%s)", length, StringUtils.times("*", Math.min(count, 53)), count);
+      });
+    }
   }
 
   // int String#hashCode()
@@ -427,6 +432,7 @@
 
   private void logNameComputation(ClassNameMapping kind) {
     if (Log.ENABLED && Log.isLoggingEnabledFor(StringOptimizer.class)) {
+      assert numberOfComputedNames != null;
       synchronized (numberOfComputedNames) {
         int count = numberOfComputedNames.getOrDefault(kind, 0);
         numberOfComputedNames.put(kind, count + 1);
@@ -437,6 +443,7 @@
   private void logHistogramOfNames(DexString name) {
     if (Log.ENABLED && Log.isLoggingEnabledFor(StringOptimizer.class)) {
       Integer length = name.size;
+      assert histogramOfLengthOfNames != null;
       synchronized (histogramOfLengthOfNames) {
         int count = histogramOfLengthOfNames.getOrDefault(length, 0);
         histogramOfLengthOfNames.put(length, count + 1);
@@ -446,6 +453,7 @@
 
   private void logDeferredNameComputation(ClassNameMapping kind) {
     if (Log.ENABLED && Log.isLoggingEnabledFor(StringOptimizer.class)) {
+      assert numberOfDeferredComputationOfNames != null;
       synchronized (numberOfDeferredComputationOfNames) {
         int count = numberOfDeferredComputationOfNames.getOrDefault(kind, 0);
         numberOfDeferredComputationOfNames.put(kind, count + 1);
@@ -458,6 +466,7 @@
       assert deferred.getItem().isDexType();
       DexType original = deferred.getItem().asDexType();
       Integer length = original.descriptor.size;
+      assert histogramOfLengthOfDeferredNames != null;
       synchronized (histogramOfLengthOfDeferredNames) {
         int count = histogramOfLengthOfDeferredNames.getOrDefault(length, 0);
         histogramOfLengthOfDeferredNames.put(length, count + 1);