Using plain array instead of ArrayDeque exceptions
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 09 Dec 2012 16:25:36 +0100
branchexceptions
changeset 289a2ca83ddc521
parent 288 6d1e8eccdc98
child 290 57dff70280c7
Using plain array instead of ArrayDeque
javap/src/main/java/org/apidesign/javap/TrapDataIterator.java
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
     1.1 --- a/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java	Sun Dec 09 16:16:19 2012 +0100
     1.2 +++ b/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java	Sun Dec 09 16:25:36 2012 +0100
     1.3 @@ -11,7 +11,8 @@
     1.4  public final class TrapDataIterator {
     1.5      private final java.util.Map<Short, TrapData> exStart = new java.util.HashMap<Short, TrapData>();
     1.6      private final java.util.Map<Short, TrapData> exStop = new java.util.HashMap<Short, TrapData>();
     1.7 -    private final java.util.Deque<TrapData> current = new java.util.ArrayDeque<TrapData>();
     1.8 +    private TrapData[] current = new TrapData[10];
     1.9 +    private int currentCount;
    1.10      
    1.11      TrapDataIterator(Vector exceptionTable) {
    1.12          for (int i=0 ; i < exceptionTable.size(); i++) {
    1.13 @@ -24,19 +25,45 @@
    1.14      public void advanceTo(int i) {
    1.15          TrapData e = exStart.get((short) i);
    1.16          if (e != null) {
    1.17 -            current.addFirst(e);
    1.18 +            add(e);
    1.19          }
    1.20          e = exStop.get((short) i);
    1.21          if (e != null) {
    1.22 -            current.remove(e);
    1.23 +            remove(e);
    1.24          }
    1.25      }
    1.26  
    1.27      public boolean useTry() {
    1.28 -        return !current.isEmpty();
    1.29 +        return currentCount > 0;
    1.30      }
    1.31  
    1.32      public TrapData[] current() {
    1.33 -        return current.toArray(new TrapData[0]);
    1.34 +        return current;
    1.35 +    }
    1.36 +
    1.37 +    private void add(TrapData e) {
    1.38 +        if (currentCount == current.length) {
    1.39 +            TrapData[] data = new TrapData[currentCount * 2];
    1.40 +            for (int i = 0; i < currentCount; i++) {
    1.41 +                data[i] = current[i];
    1.42 +            }
    1.43 +            current = data;
    1.44 +        }
    1.45 +        current[currentCount++] = e;
    1.46 +    }
    1.47 +
    1.48 +    private void remove(TrapData e) {
    1.49 +        int from = 0;
    1.50 +        while (from < currentCount) {
    1.51 +            if (e == current[from++]) {
    1.52 +                break;
    1.53 +            }
    1.54 +        }
    1.55 +        while (from < currentCount) {
    1.56 +            current[from - 1] = current[from];
    1.57 +            current[from] = null;
    1.58 +            from++;
    1.59 +        }
    1.60 +        currentCount--;
    1.61      }
    1.62  }
     2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Dec 09 16:16:19 2012 +0100
     2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Dec 09 16:25:36 2012 +0100
     2.3 @@ -798,6 +798,9 @@
     2.4              if (trap.useTry()) {
     2.5                  out.append("} catch (e) {");
     2.6                  for (TrapData e : trap.current()) {
     2.7 +                    if (e == null) {
     2.8 +                        break;
     2.9 +                    }
    2.10                      if (e.catch_cpx != 0) { //not finally
    2.11                          final String classInternalName = jc.getClassName(e.catch_cpx);
    2.12                          addReference(classInternalName);