BUG-5410: add minimal IntStack implementation 81/39181/3
authorRobert Varga <rovarga@cisco.com>
Fri, 20 May 2016 13:32:07 +0000 (15:32 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 12 Sep 2016 12:21:29 +0000 (12:21 +0000)
Using Stack<Integer> forces integer boxing, which costing us performance.
This patch adds a specialized primitive-based stack, which provides the
minimal needed implementation.

Change-Id: Iee0e977ab87ce32871ddb468dbda47c4afa4e086
Signed-off-by: Robert Varga <rovarga@cisco.com>
third-party/xsd-regex/src/main/java/org/opendaylight/yangtools/xsd/regex/IntStack.java [new file with mode: 0644]
third-party/xsd-regex/src/main/java/org/opendaylight/yangtools/xsd/regex/RegularExpression.java

diff --git a/third-party/xsd-regex/src/main/java/org/opendaylight/yangtools/xsd/regex/IntStack.java b/third-party/xsd-regex/src/main/java/org/opendaylight/yangtools/xsd/regex/IntStack.java
new file mode 100644 (file)
index 0000000..ca9a970
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.opendaylight.yangtools.xsd.regex;
+
+import java.util.Arrays;
+import java.util.EmptyStackException;
+import javax.annotation.concurrent.NotThreadSafe;
+
+/**
+ * A specialized minimal implementation of Stack<Integer>, which operations on simple ints.
+ */
+@NotThreadSafe
+final class IntStack {
+    private static final int DEFAULT_SIZE = 10;
+    private static final int INCREMENT = 10;
+    private int[] items;
+    private int count = 0;
+
+    IntStack() {
+        this(DEFAULT_SIZE);
+    }
+
+    IntStack(final int size) {
+        items = new int[size];
+    }
+
+    void push(final int item) {
+        if (count == items.length) {
+            items = Arrays.copyOf(items, items.length + INCREMENT);
+        }
+
+        items[count++] = item;
+    }
+
+    int pop() {
+        if (count == 0) {
+            throw new EmptyStackException();
+        }
+
+        return items[--count];
+    }
+}
index 0e5533d919845d6de9b7f2e368a238bdf1812820..17bc1d6be15c36bffda63652d86d2fc35820bcaf 100644 (file)
@@ -1050,7 +1050,7 @@ public class RegularExpression implements java.io.Serializable {
     private int match(Context con, Op op, int offset, int dx, int opts) {
         final ExpressionTarget target = con.target;
         final Stack<Op> opStack = new Stack<>();
-        final Stack<Integer> dataStack = new Stack<>();
+        final IntStack dataStack = new IntStack();
         final boolean isSetIgnoreCase = isSet(opts, IGNORE_CASE);
         int retValue = -1;
         boolean returned = false;