Use index instead of iterator for child states in minifier

Change-Id: I9c5baa0badf47358676dcffccdba9ce9a61e37b4
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index a20897f..fa693c5 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
 
   // This field is accessed from release scripts using simple pattern matching.
   // Therefore, changing this field could break our release scripts.
-  public static final String LABEL = "1.4.93";
+  public static final String LABEL = "1.4.94";
 
   private Version() {
   }
diff --git a/src/main/java/com/android/tools/r8/naming/NamingState.java b/src/main/java/com/android/tools/r8/naming/NamingState.java
index 8e49563..e6eb741 100644
--- a/src/main/java/com/android/tools/r8/naming/NamingState.java
+++ b/src/main/java/com/android/tools/r8/naming/NamingState.java
@@ -13,7 +13,6 @@
 import com.google.common.collect.Table;
 import java.io.PrintStream;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -70,7 +69,7 @@
         InternalState parentState = parent.getOrCreateInternalStateFor(key);
         result = parentState.createChild();
       } else {
-        result = new InternalState(itemFactory, null, dictionary);
+        result = new InternalState(itemFactory, null);
       }
       usedNames.put(key, result);
     }
@@ -146,6 +145,7 @@
   class InternalState {
 
     private static final int INITIAL_NAME_COUNT = 1;
+    private static final int INITIAL_DICTIONARY_INDEX = 0;
     private final char[] EMPTY_CHAR_ARRAY = new char[0];
 
     protected final DexItemFactory itemFactory;
@@ -153,22 +153,17 @@
     private Set<DexString> reservedNames = null;
     private Table<DexString, KeyType, DexString> renamings = null;
     private int nameCount;
-    private final Iterator<String> dictionaryIterator;
+    private int dictionaryIndex;
 
-    private InternalState(
-        DexItemFactory itemFactory,
-        InternalState parentInternalState,
-        Iterator<String> dictionaryIterator) {
+    private InternalState(DexItemFactory itemFactory, InternalState parentInternalState) {
       this.itemFactory = itemFactory;
       this.parentInternalState = parentInternalState;
       this.nameCount =
           parentInternalState == null ? INITIAL_NAME_COUNT : parentInternalState.nameCount;
-      this.dictionaryIterator = dictionaryIterator;
-    }
-
-    private InternalState(
-        DexItemFactory itemFactory, InternalState parentInternalState, List<String> dictionary) {
-      this(itemFactory, parentInternalState, dictionary.iterator());
+      this.dictionaryIndex =
+          parentInternalState == null
+              ? INITIAL_DICTIONARY_INDEX
+              : parentInternalState.dictionaryIndex;
     }
 
     private boolean isReserved(DexString name) {
@@ -183,7 +178,7 @@
     }
 
     InternalState createChild() {
-      return new InternalState(itemFactory, this, dictionaryIterator);
+      return new InternalState(itemFactory, this);
     }
 
     void reserveName(DexString name) {
@@ -252,8 +247,8 @@
     }
 
     String nextSuggestedName() {
-      if (dictionaryIterator.hasNext()) {
-        return dictionaryIterator.next();
+      if (dictionaryIndex < dictionary.size()) {
+        return dictionary.get(dictionaryIndex++);
       } else {
         return StringUtils.numberToIdentifier(EMPTY_CHAR_ARRAY, incrementAndGet(), false);
       }
diff --git a/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java b/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java
new file mode 100644
index 0000000..27aaf73
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java
@@ -0,0 +1,112 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.naming;
+
+import static junit.framework.TestCase.assertEquals;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.utils.FileUtils;
+import com.android.tools.r8.utils.StringUtils;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class FieldNamingObfuscationDictionaryTest extends TestBase {
+
+  public static class A {
+
+    public String f1;
+
+    public A(String a) {
+      this.f1 = a;
+    }
+  }
+
+  public static class B extends A {
+
+    public int f0;
+    public String f2;
+
+    public B(int f0, String a, String b) {
+      super(a);
+      this.f0 = f0;
+      this.f2 = b;
+    }
+
+    @NeverInline
+    public void print() {
+      System.out.println(f0 + f1 + " " + f2);
+    }
+  }
+
+  public static class C extends A {
+
+    public int f0;
+    public String f3;
+
+    public C(int f0, String a, String c) {
+      super(a);
+      this.f0 = f0;
+      this.f3 = c;
+    }
+
+    @NeverInline
+    public void print() {
+      System.out.println(f0 + f1 + " " + f3);
+    }
+  }
+
+  public static class Runner {
+
+    public static void main(String[] args) {
+      String arg1 = args.length == 0 ? "HELLO" : args[0];
+      String arg2 = args.length == 0 ? "WORLD" : args[1];
+      new B(args.length, arg1, arg2).print();
+      new C(args.length, arg1, arg2).print();
+    }
+  }
+
+  private Backend backend;
+
+  @Parameterized.Parameters(name = "Backend: {0}")
+  public static Backend[] data() {
+    return Backend.values();
+  }
+
+  public FieldNamingObfuscationDictionaryTest(Backend backend) {
+    this.backend = backend;
+  }
+
+  @Test
+  public void testInheritedNamingState()
+      throws IOException, CompilationFailedException, ExecutionException {
+    Path dictionary = temp.getRoot().toPath().resolve("dictionary.txt");
+    FileUtils.writeTextFile(dictionary, "a", "b", "c");
+
+    testForR8(backend)
+        .addInnerClasses(FieldNamingObfuscationDictionaryTest.class)
+        .enableInliningAnnotations()
+        .addKeepRules("-overloadaggressively", "-obfuscationdictionary " + dictionary.toString())
+        .addKeepRules()
+        .addKeepMainRule(Runner.class)
+        .compile()
+        .run(Runner.class)
+        .assertSuccessWithOutput(StringUtils.lines("0HELLO WORLD", "0HELLO WORLD"))
+        .inspect(
+            inspector -> {
+              assertEquals("a", inspector.clazz(A.class).uniqueFieldWithName("f1").getFinalName());
+              assertEquals("a", inspector.clazz(B.class).uniqueFieldWithName("f0").getFinalName());
+              assertEquals("b", inspector.clazz(B.class).uniqueFieldWithName("f2").getFinalName());
+              assertEquals("a", inspector.clazz(C.class).uniqueFieldWithName("f0").getFinalName());
+              assertEquals("b", inspector.clazz(C.class).uniqueFieldWithName("f3").getFinalName());
+            });
+  }
+}