Revision: 8301
Author: rchan...@google.com
Date: Mon Jun 14 11:58:23 2010
Log: Optimized MutableArray creation. Added Benchmark to facilitate comparison using a single graph.

Review at http://gwt-code-reviews.appspot.com/613801

Review by: r...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=8301

Added:
/branches/lwc-gwt-migration/bikeshed/test/com/google/gwt/collections/MutableArrayComparisonBenchmark.java
Modified:
/branches/lwc-gwt-migration/bikeshed/src/com/google/gwt/collections/CollectionFactory.java /branches/lwc-gwt-migration/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/CollectionFactory.java /branches/lwc-gwt-migration/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java

=======================================
--- /dev/null
+++ /branches/lwc-gwt-migration/bikeshed/test/com/google/gwt/collections/MutableArrayComparisonBenchmark.java Mon Jun 14 11:58:23 2010
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed 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 com.google.gwt.collections;
+
+import com.google.gwt.benchmarks.client.Benchmark;
+import com.google.gwt.benchmarks.client.IntRange;
+import com.google.gwt.benchmarks.client.Operator;
+import com.google.gwt.benchmarks.client.RangeField;
+
+import java.util.ArrayList;
+
+/**
+ * Benchmarks the performance of various MutableArray methods.
+ */
+public class MutableArrayComparisonBenchmark extends Benchmark {
+
+  final IntRange elemRange = new IntRange(5, 30005, Operator.ADD, 5000);
+  final ArrayBuildType[] testKind = {ArrayBuildType.GWT_ADD,
+      ArrayBuildType.GWT_SET, ArrayBuildType.GWT_SET_INIT,
+      ArrayBuildType.JRE_ARRAY, ArrayBuildType.JRE_LIST};
+
+  /**
+   * Type of array construction to benchmark
+   */
+  protected enum ArrayBuildType {
+    GWT_ADD("GWT Array add()"), GWT_SET("GWT sized create"),
+ GWT_SET_INIT("GWT sized create + initialization"), JRE_ARRAY("JRE Array"),
+    JRE_LIST("JRE ArrayList");
+
+    public String description;
+
+    private ArrayBuildType(String description) {
+      this.description = description;
+    }
+  }
+
+  @Override
+  public String getModuleName() {
+    return "com.google.gwt.collections.Collections";
+  }
+
+  /**
+   * Required by benchmarking framework.
+   */
+  public void testComparativePerformance() {
+  }
+
+  public void testComparativePerformance(
+      @RangeField("elemRange") Integer numElements,
+      @RangeField("testKind") ArrayBuildType test) {
+    switch (test) {
+      case GWT_ADD:
+        gwtCollectionsArrayAddGrowth(numElements);
+        break;
+
+      case GWT_SET:
+        gwtCollectionsArraySetSizeGrowth(numElements);
+        break;
+
+      case GWT_SET_INIT:
+        gwtCollectionsArraySetSizeInitGrowth(numElements);
+        break;
+
+      case JRE_ARRAY:
+        javaArraySetGrowth(numElements);
+        break;
+
+      case JRE_LIST:
+        jreArrayListAddGrowth(numElements);
+        break;
+
+      default:
+        break;
+    }
+  }
+
+  private void gwtCollectionsArrayAddGrowth(Integer numElements) {
+    MutableArray<Integer> ma = CollectionFactory.createMutableArray();
+
+    for (int i = 0; i < numElements; i++) {
+      ma.add(i);
+    }
+  }
+
+  private void gwtCollectionsArraySetSizeGrowth(Integer numElements) {
+ MutableArray<Integer> ma = CollectionFactory.createMutableArray(numElements);
+
+    for (int i = 0; i < numElements; i++) {
+      ma.set(i, i);
+    }
+  }
+
+  private void gwtCollectionsArraySetSizeInitGrowth(Integer numElements) {
+    MutableArray<Integer> ma =
+      CollectionFactory.createMutableArray(numElements, new Integer(0));
+
+    for (int i = 0; i < numElements; i++) {
+      ma.set(i, i);
+    }
+  }
+
+  private void javaArraySetGrowth(Integer numElements) {
+    Integer[] ia = new Integer[numElements];
+
+    for (int i = 0; i < numElements; i++) {
+      ia[i] = i;
+    }
+  }
+
+  private void jreArrayListAddGrowth(Integer numElements) {
+    ArrayList<Integer> al = new ArrayList<Integer>();
+
+    for (int i = 0; i < numElements; i++) {
+      al.add(i);
+    }
+  }
+
+}
=======================================
--- /branches/lwc-gwt-migration/bikeshed/src/com/google/gwt/collections/CollectionFactory.java Mon Jun 14 06:05:16 2010 +++ /branches/lwc-gwt-migration/bikeshed/src/com/google/gwt/collections/CollectionFactory.java Mon Jun 14 11:58:23 2010
@@ -29,6 +29,18 @@
   public static <E> MutableArray<E> createMutableArray() {
     return new MutableArray<E>();
   }
+
+  /**
+   * Creates a {...@link MutableArray} with an initial {...@code size} and null
+   * elements.
+   *
+   * @param <E> type of elements in the array
+   * @param size size of the array
+   * @return a {...@code MutableArray}
+   */
+  public static <E> MutableArray<E> createMutableArray(Integer size) {
+    return createMutableArray(size, null);
+  }

   /**
    * Creates a {...@link MutableArray} with {...@code size} references to 
{...@code
=======================================
--- /branches/lwc-gwt-migration/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/CollectionFactory.java Mon Jun 14 06:05:16 2010 +++ /branches/lwc-gwt-migration/bikeshed/super/com/google/gwt/collections/super/com/google/gwt/collections/CollectionFactory.java Mon Jun 14 11:58:23 2010
@@ -15,22 +15,29 @@
  */
 package com.google.gwt.collections;

-import com.google.gwt.core.client.JavaScriptObject;
-
 /**
* Made to be switched out using super source even while Collections itself isn't.
  */
 public class CollectionFactory {

-  public static <E> MutableArray<E> createMutableArray() {
-    return JavaScriptObject.createArray().<MutableArray<E>>cast();
-  }
-
- public static <E> MutableArray<E> createMutableArray(int size, E fillValue) {
-    MutableArray<E> r = createMutableArray();
-    r.setSize(size, fillValue);
+  public static native <E> MutableArray<E> createMutableArray() /*-{
+    return Array();
+  }-*/;
+
+ public static native <E> MutableArray<E> createMutableArray(int size) /*-{
+    return Array(size);
+  }-*/;
+
+  public static native <E> MutableArray<E> createMutableArray(int size,
+      E fillValue) /*-{
+    var r = Array(size);
+    if (fillValue != null) {
+      for (i = 0; i < size; ++i) {
+        r[i] = fillValue;
+      }
+    }
     return r;
-  }
+  }-*/;

   public static <V> MutableStringMap<V> createMutableStringMap() {
     return new MutableStringMap<V>();
=======================================
--- /branches/lwc-gwt-migration/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java Tue Jun 22 12:49:14 2010 +++ /branches/lwc-gwt-migration/bikeshed/test/com/google/gwt/collections/MutableArrayBenchmarkTest.java Mon Jun 14 11:58:23 2010
@@ -49,7 +49,8 @@
   }

public void testGwtCollectionsArraySetSizeGrowth(@RangeField("elemRange") Integer numElements) {
-    MutableArray<Integer> ma = CollectionFactory.createMutableArray();
+    MutableArray<Integer> ma =
+      CollectionFactory.createMutableArray(numElements);

     ma.setSize(numElements, null);
     for (int i = 0; i < numElements; i++) {
@@ -62,9 +63,9 @@

   public void testGwtCollectionsArraySetSizeInitGrowth(
       @RangeField("elemRange") Integer numElements) {
-    MutableArray<Integer> ma = CollectionFactory.createMutableArray();
-
-    ma.setSize(numElements, new Integer(0));
+    MutableArray<Integer> ma =
+      CollectionFactory.createMutableArray(numElements, new Integer(0));
+
     for (int i = 0; i < numElements; i++) {
       ma.set(i, i);
     }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to