Skip to content
This repository
Browse code

New optimized CatalogMap.values() method that is faster than using an…

… iterator
  • Loading branch information...
commit f2659fd1592b041a375a8f480f3b3baa1d4a211d 1 parent 66a4bc6
Andy Pavlo authored February 06, 2012

Showing 1 changed file with 10 additions and 3 deletions. Show diff stats Hide diff stats

  1. 13  src/catgen/in/javasrc/CatalogMap.java
13  src/catgen/in/javasrc/CatalogMap.java
@@ -41,6 +41,7 @@
41 41
 public final class CatalogMap<T extends CatalogType> implements Iterable<T>, Collection<T> {
42 42
 
43 43
     TreeMap<String, T> m_items = new TreeMap<String, T>();
  44
+    T m_fastArray[];
44 45
     Class<T> m_cls;
45 46
     Catalog m_catalog;
46 47
     CatalogType m_parent;
@@ -145,13 +146,14 @@ public T get(int index) {
145 146
     
146 147
     @SuppressWarnings("unchecked")
147 148
     public T[] values() {
  149
+        if (m_fastArray != null) return (m_fastArray);
148 150
         int capacity = this.size();
149  
-        final T a[] =(T[])Array.newInstance(this.m_cls, capacity);
  151
+        m_fastArray =(T[])Array.newInstance(this.m_cls, capacity);
150 152
         int i = 0;
151 153
         for (T t : m_items.values()) {
152  
-            a[i++] = t;
  154
+            m_fastArray[i++] = t;
153 155
         }
154  
-        return a;
  156
+        return m_fastArray;
155 157
     }
156 158
 
157 159
     public int getSubTreeVersion() {
@@ -188,6 +190,7 @@ public T add(String name) {
188 190
                 e.getValue().m_relativeIndex = index++;
189 191
             }
190 192
 
  193
+            m_fastArray = null;
191 194
             return x;
192 195
         } catch (Exception ex) {
193 196
             throw new RuntimeException(ex);
@@ -226,6 +229,8 @@ public boolean add(T x, boolean initialize) {
226 229
         for (Entry<String, T> e : m_items.entrySet()) {
227 230
             e.getValue().m_relativeIndex = index++;
228 231
         }
  232
+        
  233
+        m_fastArray = null;
229 234
         return (true);
230 235
     }
231 236
 
@@ -252,6 +257,8 @@ public boolean delete(String name) {
252 257
         } catch (Exception ex) {
253 258
             throw new RuntimeException(ex);
254 259
         }
  260
+        
  261
+        m_fastArray = null;
255 262
         return (true);
256 263
     }
257 264
 

0 notes on commit f2659fd

Please sign in to comment.
Something went wrong with that request. Please try again.