Tests: reuse objects already made.
authorjonlee@apple.com <jonlee@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 Feb 2016 03:30:44 +0000 (03:30 +0000)
committerjonlee@apple.com <jonlee@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 Feb 2016 03:30:44 +0000 (03:30 +0000)
Avoid thrash of object creation and removal by maintaining an index that
moves along the array as the adjust values change. If the tune value
requires more objects than the maximum size of the object array, then create
new objects. This means that the object array size never decreases.

* Animometer/tests/master/resources/canvas-stage.js: Maintain a separate
offsetIndex. For these tests, we want to avoid drawing the oldest objects,
so the scene will draw the object at offsetIndex to the end of the array.
(tune): Reverse the logic since "removal" of objects is much simpler and
involves simply changing the offsetIndex.
(animate): Update the for loop to draw from offsetIndex to the end.
(complexity): Update the definition.
* Animometer/tests/master/resources/canvas-tests.js: Maintain a separate
offsetIndex. For these tests, we want to avoid drawing the newest objects,
so the scene will draw the object at index 0 to the object at offsetIndex.
(SimpleCanvasStage.animate): Fly-by removal of local stage variable,
which is unneeded. Update the for loop to draw from offsetIndex to the end.
* Animometer/tests/simple/resources/simple-canvas-paths.js:
(SimpleCanvasStage.animate): Update the for loop to draw from 0 to
offsetIndex.
* Animometer/tests/simple/resources/simple-canvas.js:
(tune): Update logic. Here, offsetIndex represents the boundary of the last
index to render.
(animate): Update the for loop to draw from 0 to offsetIndex.
(complexity): Update the definition.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@196296 268f45cc-cd09-0410-ab3c-d52691b4dbfc

PerformanceTests/Animometer/tests/master/resources/canvas-stage.js
PerformanceTests/Animometer/tests/master/resources/canvas-tests.js
PerformanceTests/Animometer/tests/simple/resources/simple-canvas-paths.js
PerformanceTests/Animometer/tests/simple/resources/simple-canvas.js
PerformanceTests/ChangeLog

index ae647db..2ad63a3 100644 (file)
@@ -4,6 +4,7 @@ SimpleCanvasStage = Utilities.createSubclass(Stage,
         Stage.call(this);
         this._canvasObject = canvasObject;
         this.objects = [];
+        this.offsetIndex = 0;
     }, {
 
     initialize: function(benchmark, options)
@@ -17,27 +18,31 @@ SimpleCanvasStage = Utilities.createSubclass(Stage,
         if (count == 0)
             return;
 
-        if (count > 0) {
-            for (var i = 0; i < count; ++i)
-                this.objects.push(new this._canvasObject(this));
+        if (count < 0) {
+            this.offsetIndex = Math.min(this.offsetIndex - count, this.objects.length);
             return;
         }
 
-        count = Math.min(-count, this.objects.length);
-        this.objects.splice(0, count);
+        var newIndex = this.offsetIndex - count;
+        if (newIndex < 0) {
+            this.offsetIndex = 0;
+            newIndex = -newIndex;
+            for (var i = 0; i < newIndex; ++i)
+                this.objects.push(new this._canvasObject(this));
+        } else
+            this.offsetIndex = newIndex;
     },
 
     animate: function()
     {
         var context = this.context;
         context.clearRect(0, 0, this.size.x, this.size.y);
-        this.objects.forEach(function(object) {
-            object.draw(context);
-        });
+        for (var i = this.offsetIndex, length = this.objects.length; i < length; ++i)
+            this.objects[i].draw(context);
     },
 
     complexity: function()
     {
-        return this.objects.length;
+        return this.objects.length - this.offsetIndex;
     }
 });
index edfbc6f..a9e957b 100644 (file)
@@ -137,7 +137,6 @@ CanvasLineSegmentStage = Utilities.createSubclass(SimpleCanvasStage,
     animate: function()
     {
         var context = this.context;
-        var stage = this;
 
         context.clearRect(0, 0, this.size.x, this.size.y);
         context.lineWidth = 30;
@@ -145,14 +144,13 @@ CanvasLineSegmentStage = Utilities.createSubclass(SimpleCanvasStage,
             context.strokeStyle = ["#e01040", "#10c030", "#e05010"][i];
             context.fillStyle = ["#70051d", "#016112", "#702701"][i];
             context.beginPath();
-                context.arc((0.5 + i) / 3 * stage.size.x, stage.size.y/2, stage.circleRadius, 0, Math.PI*2);
+                context.arc((0.5 + i) / 3 * this.size.x, this.size.y/2, this.circleRadius, 0, Math.PI*2);
             context.stroke();
             context.fill();
         }
 
-        this.objects.forEach(function(object) {
-            object.draw(context);
-        });
+        for (var i = this.offsetIndex, length = this.objects.length; i < length; ++i)
+            this.objects[i].draw(context);
     }
 });
 
@@ -171,12 +169,12 @@ CanvasLinePathStage = Utilities.createSubclass(SimpleCanvasStage,
 
     animate: function() {
         var context = this.context;
-        var stage = this;
 
         context.clearRect(0, 0, this.size.x, this.size.y);
         context.beginPath();
-        this.objects.forEach(function(object, index) {
-            if (index == 0) {
+        for (var i = this.offsetIndex, length = this.objects.length; i < length; ++i) {
+            var object = this.objects[i];
+            if (i == this.offsetIndex) {
                 context.lineWidth = object.width;
                 context.strokeStyle = object.color;
                 context.moveTo(object.point.x, object.point.y);
@@ -194,7 +192,7 @@ CanvasLinePathStage = Utilities.createSubclass(SimpleCanvasStage,
                 if (Math.random() > 0.999)
                     object.isSplit = !object.isSplit;
             }
-        });
+        }
         context.stroke();
     }
 });
index 88c1e88..718f8bc 100644 (file)
@@ -224,9 +224,8 @@ SimpleCanvasPathStrokeStage = Utilities.createSubclass(SimpleCanvasStage,
         context.strokeStyle = Stage.rotatingColor();
         context.beginPath();
         context.moveTo(this.size.x / 2, this.size.y / 2);
-        this.objects.forEach(function(object) {
-            object.draw(context);
-        });
+        for (var i = 0, length = this.offsetIndex; i < length; ++i)
+            this.objects[i].draw(context);
         context.stroke();
     }
 });
@@ -243,9 +242,8 @@ SimpleCanvasPathFillStage = Utilities.createSubclass(SimpleCanvasStage,
         context.fillStyle = Stage.rotatingColor();
         context.beginPath();
         context.moveTo(this.size.x / 2, this.size.y / 2);
-        this.objects.forEach(function(object) {
-            object.draw(context);
-        });
+        for (var i = 0, length = this.offsetIndex; i < length; ++i)
+            this.objects[i].draw(context);
         context.fill();
     }
 });
@@ -298,9 +296,8 @@ CanvasLineDashStage = Utilities.createSubclass(SimpleCanvasStage,
         context.lineDashOffset = this._step++;
         context.beginPath();
         context.moveTo(this.size.x / 2, this.size.y / 2);
-        this.objects.forEach(function(object) {
-            object.draw(context);
-        });
+        for (var i = 0, length = this.offsetIndex; i < length; ++i)
+            this.objects[i].draw(context);
         context.stroke();
     }
 });
index 28342a8..483d535 100644 (file)
@@ -4,16 +4,32 @@ Utilities.extendObject(SimpleCanvasStage.prototype, {
         if (count == 0)
             return;
 
-        if (count > 0) {
+        if (count < 0) {
+            this.offsetIndex = Math.max(this.offsetIndex + count, 0);
+            return;
+        }
+
+        this.offsetIndex = this.offsetIndex + count;
+        if (this.offsetIndex > this.objects.length) {
             // For some tests, it may be easier to see how well the test is going
             // by limiting the range of coordinates in which new objects can reside
             var coordinateMaximumFactor = Math.min(this.objects.length, Math.min(this.size.x, this.size.y)) / Math.min(this.size.x, this.size.y);
-            for (var i = 0; i < count; ++i)
+            var newIndex = this.offsetIndex - this.objects.length;
+            for (var i = 0; i < newIndex; ++i)
                 this.objects.push(new this._canvasObject(this, coordinateMaximumFactor));
-            return;
         }
+    },
 
-        count = Math.min(-count, this.objects.length);
-        this.objects.splice(-count, count);
+    animate: function()
+    {
+        var context = this.context;
+        context.clearRect(0, 0, this.size.x, this.size.y);
+        for (var i = 0, length = this.offsetIndex; i < length; ++i)
+            this.objects[i].draw(context);
+    },
+
+    complexity: function()
+    {
+        return this.offsetIndex;
     }
 });
index 601646b..64d18f4 100644 (file)
@@ -1,5 +1,35 @@
 2016-02-07  Jon Lee  <jonlee@apple.com>
 
+        Tests: reuse objects already made.
+
+        Avoid thrash of object creation and removal by maintaining an index that
+        moves along the array as the adjust values change. If the tune value
+        requires more objects than the maximum size of the object array, then create
+        new objects. This means that the object array size never decreases.
+
+        * Animometer/tests/master/resources/canvas-stage.js: Maintain a separate
+        offsetIndex. For these tests, we want to avoid drawing the oldest objects,
+        so the scene will draw the object at offsetIndex to the end of the array.
+        (tune): Reverse the logic since "removal" of objects is much simpler and
+        involves simply changing the offsetIndex.
+        (animate): Update the for loop to draw from offsetIndex to the end.
+        (complexity): Update the definition.
+        * Animometer/tests/master/resources/canvas-tests.js: Maintain a separate
+        offsetIndex. For these tests, we want to avoid drawing the newest objects,
+        so the scene will draw the object at index 0 to the object at offsetIndex.
+        (SimpleCanvasStage.animate): Fly-by removal of local stage variable,
+        which is unneeded. Update the for loop to draw from offsetIndex to the end.
+        * Animometer/tests/simple/resources/simple-canvas-paths.js:
+        (SimpleCanvasStage.animate): Update the for loop to draw from 0 to
+        offsetIndex.
+        * Animometer/tests/simple/resources/simple-canvas.js:
+        (tune): Update logic. Here, offsetIndex represents the boundary of the last
+        index to render.
+        (animate): Update the for loop to draw from 0 to offsetIndex.
+        (complexity): Update the definition.
+
+2016-02-07  Jon Lee  <jonlee@apple.com>
+
         Tests: refactor and update styles.
 
         * Animometer/tests/resources/main.js: Add helper methods that return