You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2017/09/29 07:25:34 UTC

[3/9] karaf git commit: Clean SimpleFilter up

Clean SimpleFilter up

Profiling OpenDaylight SFT has turned up large memory usage when
a lot of features are created. Some of the memory wasted is in
over-allocated ArrayLists, which are using default pre-allocation
size.

This patch uses Collections.singletonList() to hold NOT operation
arguments and reduces pre-allocation for other compound operations
to 2.

Signed-off-by: Robert Varga <ni...@hq.sk>


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/8fc4d282
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/8fc4d282
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/8fc4d282

Branch: refs/heads/master
Commit: 8fc4d28219c729e09d295313a270c9e34330ee0b
Parents: 2a67918
Author: Robert Varga <ni...@hq.sk>
Authored: Tue Sep 26 16:40:38 2017 +0200
Committer: Robert Varga <ni...@hq.sk>
Committed: Wed Sep 27 09:55:03 2017 +0200

----------------------------------------------------------------------
 .../internal/resolver/SimpleFilter.java         | 59 +++++++++-----------
 1 file changed, 25 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/8fc4d282/features/core/src/main/java/org/apache/karaf/features/internal/resolver/SimpleFilter.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/resolver/SimpleFilter.java b/features/core/src/main/java/org/apache/karaf/features/internal/resolver/SimpleFilter.java
index 70084ab..3464a4e 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/resolver/SimpleFilter.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/resolver/SimpleFilter.java
@@ -17,6 +17,7 @@
 package org.apache.karaf.features.internal.resolver;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Deque;
 import java.util.LinkedList;
 import java.util.List;
@@ -25,7 +26,6 @@ import java.util.Map.Entry;
 
 import org.apache.felix.utils.version.VersionRange;
 
-@SuppressWarnings("rawtypes")
 public class SimpleFilter {
     public static final int MATCH_ALL = 0;
     public static final int AND = 1;
@@ -60,30 +60,29 @@ public class SimpleFilter {
         return op;
     }
 
+    @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
         toString(sb);
         return sb.toString();
     }
 
-    @SuppressWarnings("unchecked")
-    private void toString(StringBuilder sb)
-    {
+    private void toString(StringBuilder sb) {
         switch (op)
         {
         case AND:
             sb.append("(&");
-            toString(sb, (List) value);
+            toString(sb, (List<?>) value);
             sb.append(")");
             break;
         case OR:
             sb.append("(|");
-            toString(sb, (List) value);
+            toString(sb, (List<?>) value);
             sb.append(")");
             break;
         case NOT:
             sb.append("(!");
-            toString(sb, (List) value);
+            toString(sb, (List<?>) value);
             sb.append(")");
             break;
         case EQ:
@@ -109,7 +108,7 @@ public class SimpleFilter {
             break;
         case SUBSTRING:
             sb.append("(").append(name).append("=");
-            unparseSubstring(sb, (List) value);
+            unparseSubstring(sb, (List<?>) value);
             sb.append(")");
             break;
         case PRESENT:
@@ -126,7 +125,7 @@ public class SimpleFilter {
         }
     }
 
-    private static void toString(StringBuilder sb, List list) {
+    private static void toString(StringBuilder sb, List<?> list) {
         for (Object o : list) {
             SimpleFilter sf = (SimpleFilter) o;
             sf.toString(sb);
@@ -195,7 +194,7 @@ public class SimpleFilter {
                     int peek = skipWhitespace(filter, idx + 1);
                     if (filter.charAt(peek) == '(') {
                         idx = peek - 1;
-                        stack.addFirst(new SimpleFilter(null, new ArrayList(), SimpleFilter.AND));
+                        stack.addFirst(new SimpleFilter(null, new ArrayList<>(2), SimpleFilter.AND));
                     } else {
                         stack.addFirst(idx);
                     }
@@ -203,7 +202,7 @@ public class SimpleFilter {
                     int peek = skipWhitespace(filter, idx + 1);
                     if (filter.charAt(peek) == '(') {
                         idx = peek - 1;
-                        stack.addFirst(new SimpleFilter(null, new ArrayList(), SimpleFilter.OR));
+                        stack.addFirst(new SimpleFilter(null, new ArrayList<>(2), SimpleFilter.OR));
                     } else {
                         stack.addFirst(idx);
                     }
@@ -211,7 +210,7 @@ public class SimpleFilter {
                     int peek = skipWhitespace(filter, idx + 1);
                     if (filter.charAt(peek) == '(') {
                         idx = peek - 1;
-                        stack.addFirst(new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT));
+                        stack.addFirst(new SimpleFilter(null, new ArrayList<>(1), SimpleFilter.NOT));
                     } else {
                         stack.addFirst(idx);
                     }
@@ -248,13 +247,11 @@ public class SimpleFilter {
     }
 
     private static SimpleFilter subfilter(String filter, int startIdx, int endIdx) {
-        final String opChars = "=<>~";
-
         // Determine the ending index of the attribute name.
         int attrEndIdx = startIdx;
         for (int i = 0; i < (endIdx - startIdx); i++) {
             char c = filter.charAt(startIdx + i);
-            if (opChars.indexOf(c) >= 0) {
+            if ("=<>~".indexOf(c) >= 0) {
                 break;
             } else if (!Character.isWhitespace(c)) {
                 attrEndIdx = startIdx + i + 1;
@@ -327,7 +324,6 @@ public class SimpleFilter {
     }
 
     public static List<String> parseSubstring(String value) {
-        List<String> pieces = new ArrayList<>();
         int length = value.length();
 
         boolean isSimple = true;
@@ -339,8 +335,7 @@ public class SimpleFilter {
             }
         }
         if (isSimple) {
-            pieces.add(value);
-            return pieces;
+            return Collections.singletonList(value);
         }
 
         StringBuilder ss = new StringBuilder();
@@ -352,6 +347,7 @@ public class SimpleFilter {
         int idx = 0;
 
         // We assume (sub)strings can contain leading and trailing blanks
+        List<String> pieces = new ArrayList<>(2);
         boolean escaped = false;
         for (;;) {
             if (idx >= length) {
@@ -406,7 +402,7 @@ public class SimpleFilter {
         return pieces;
     }
 
-    public static void unparseSubstring(StringBuilder sb, List<String> pieces) {
+    public static void unparseSubstring(StringBuilder sb, List<?> pieces) {
         for (int i = 0; i < pieces.size(); i++) {
             if (i > 0) {
                 sb.append("*");
@@ -491,7 +487,6 @@ public class SimpleFilter {
      * @param attrs Map of attributes to convert to a filter.
      * @return A filter corresponding to the attributes.
      */
-    @SuppressWarnings("unchecked")
     public static SimpleFilter convert(Map<String, Object> attrs) {
         // Rather than building a filter string to be parsed into a SimpleFilter,
         // we will just create the parsed SimpleFilter directly.
@@ -509,14 +504,12 @@ public class SimpleFilter {
                                     SimpleFilter.GTE)
                     );
                 } else {
+                    SimpleFilter val = new SimpleFilter(
+                        entry.getKey(),
+                        vr.getFloor().toString(),
+                        SimpleFilter.LTE);
                     SimpleFilter not =
-                            new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
-                    ((List<Object>) not.getValue()).add(
-                            new SimpleFilter(
-                                    entry.getKey(),
-                                    vr.getFloor().toString(),
-                                    SimpleFilter.LTE)
-                    );
+                            new SimpleFilter(null, Collections.singletonList(val), SimpleFilter.NOT);
                     filters.add(not);
                 }
 
@@ -529,14 +522,12 @@ public class SimpleFilter {
                                         SimpleFilter.LTE)
                         );
                     } else if (!vr.getCeiling().equals(VersionRange.INFINITE_VERSION)) {
+                        SimpleFilter val = new SimpleFilter(
+                            entry.getKey(),
+                            vr.getCeiling().toString(),
+                            SimpleFilter.GTE);
                         SimpleFilter not =
-                                new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
-                        ((List<Object>) not.getValue()).add(
-                                new SimpleFilter(
-                                        entry.getKey(),
-                                        vr.getCeiling().toString(),
-                                        SimpleFilter.GTE)
-                        );
+                                new SimpleFilter(null, Collections.singletonList(val), SimpleFilter.NOT);
                         filters.add(not);
                     }
                 }