Browse Source

Use ArrayDeque instead of the synchronized Stack in XContentTestUtils (#95278)

Simon Cooper 2 years ago
parent
commit
f1b94ee017

+ 9 - 7
test/framework/src/main/java/org/elasticsearch/test/XContentTestUtils.java

@@ -24,7 +24,9 @@ import org.elasticsearch.xcontent.json.JsonXContent;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Deque;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
@@ -204,7 +206,7 @@ public final class XContentTestUtils {
             )
         ) {
             parser.nextToken();
-            List<String> possiblePaths = XContentTestUtils.getInsertPaths(parser, new Stack<>());
+            List<String> possiblePaths = XContentTestUtils.getInsertPaths(parser, new ArrayDeque<>());
             if (excludeFilter == null) {
                 insertPaths = possiblePaths;
             } else {
@@ -268,17 +270,17 @@ public final class XContentTestUtils {
      *  <li>"foo3.foo4</li>
      * </ul>
      */
-    static List<String> getInsertPaths(XContentParser parser, Stack<String> currentPath) throws IOException {
+    static List<String> getInsertPaths(XContentParser parser, Deque<String> currentPath) throws IOException {
         assert parser.currentToken() == XContentParser.Token.START_OBJECT || parser.currentToken() == XContentParser.Token.START_ARRAY
             : "should only be called when new objects or arrays start";
         List<String> validPaths = new ArrayList<>();
         // parser.currentName() can be null for root object and unnamed objects in arrays
         if (parser.currentName() != null) {
             // dots in randomized field names need to be escaped, we use that character as the path separator
-            currentPath.push(parser.currentName().replaceAll("\\.", "\\\\."));
+            currentPath.addLast(parser.currentName().replaceAll("\\.", "\\\\."));
         }
         if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
-            validPaths.add(String.join(".", currentPath.toArray(String[]::new)));
+            validPaths.add(String.join(".", currentPath));
             while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                 if (parser.currentToken() == XContentParser.Token.START_OBJECT
                     || parser.currentToken() == XContentParser.Token.START_ARRAY) {
@@ -290,15 +292,15 @@ public final class XContentTestUtils {
             while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                 if (parser.currentToken() == XContentParser.Token.START_OBJECT
                     || parser.currentToken() == XContentParser.Token.START_ARRAY) {
-                    currentPath.push(Integer.toString(itemCount));
+                    currentPath.addLast(Integer.toString(itemCount));
                     validPaths.addAll(getInsertPaths(parser, currentPath));
-                    currentPath.pop();
+                    currentPath.removeLast();
                 }
                 itemCount++;
             }
         }
         if (parser.currentName() != null) {
-            currentPath.pop();
+            currentPath.removeLast();
         }
         return validPaths;
     }

+ 2 - 2
test/framework/src/test/java/org/elasticsearch/test/XContentTestUtilsTests.java

@@ -19,11 +19,11 @@ import org.elasticsearch.xcontent.XContentType;
 import org.elasticsearch.xcontent.json.JsonXContent;
 
 import java.io.IOException;
+import java.util.ArrayDeque;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Stack;
 import java.util.function.Predicate;
 
 import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
@@ -71,7 +71,7 @@ public class XContentTestUtilsTests extends ESTestCase {
             )
         ) {
             parser.nextToken();
-            List<String> insertPaths = XContentTestUtils.getInsertPaths(parser, new Stack<>());
+            List<String> insertPaths = XContentTestUtils.getInsertPaths(parser, new ArrayDeque<>());
             assertEquals(5, insertPaths.size());
             assertThat(insertPaths, hasItem(equalTo("")));
             assertThat(insertPaths, hasItem(equalTo("list1.2")));