From 35e418c603e5307bab705fc526ba9caebced43ba Mon Sep 17 00:00:00 2001 From: p01 <p01@opera.com> Date: Mon, 2 Jun 2014 14:40:33 +0200 Subject: [PATCH] Optimized CanvasGraphics_executeOperatorList 66% faster --- src/display/canvas.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/display/canvas.js b/src/display/canvas.js index 22b027530..34b877aa9 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -394,6 +394,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { // Defines the time the executeOperatorList is going to be executing // before it stops and shedules a continue of execution. var EXECUTION_TIME = 15; + // Defines the number of steps before checking the execution time + var EXECUTION_STEPS = 10; function CanvasGraphics(canvasCtx, commonObjs, objs, imageLayer) { this.ctx = canvasCtx; @@ -726,18 +728,21 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var argsArrayLen = argsArray.length; // Sometimes the OperatorList to execute is empty. - if (argsArrayLen == i) { + if (argsArrayLen === i) { return i; } - var endTime = Date.now() + EXECUTION_TIME; + var chunkOperations = (argsArrayLen - i > EXECUTION_STEPS && + typeof continueCallback === 'function'); + var endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0; + var steps = 0; var commonObjs = this.commonObjs; var objs = this.objs; var fnId; while (true) { - if (stepper && i === stepper.nextBreakPoint) { + if (stepper !== undefined && i === stepper.nextBreakPoint) { stepper.breakIt(i, continueCallback); return i; } @@ -750,16 +755,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var deps = argsArray[i]; for (var n = 0, nn = deps.length; n < nn; n++) { var depObjId = deps[n]; - var common = depObjId.substring(0, 2) == 'g_'; + var common = depObjId[0] === 'g' && depObjId[1] === '_'; + var objsPool = common ? commonObjs : objs; // If the promise isn't resolved yet, add the continueCallback // to the promise and bail out. - if (!common && !objs.isResolved(depObjId)) { - objs.get(depObjId, continueCallback); - return i; - } - if (common && !commonObjs.isResolved(depObjId)) { - commonObjs.get(depObjId, continueCallback); + if (!objsPool.isResolved(depObjId)) { + objsPool.get(depObjId, continueCallback); return i; } } @@ -768,15 +770,18 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { i++; // If the entire operatorList was executed, stop as were done. - if (i == argsArrayLen) { + if (i === argsArrayLen) { return i; } // If the execution took longer then a certain amount of time and // `continueCallback` is specified, interrupt the execution. - if (continueCallback && Date.now() > endTime) { - continueCallback(); - return i; + if (chunkOperations && ++steps > EXECUTION_STEPS) { + if (Date.now() > endTime) { + continueCallback(); + return i; + } + steps = 0; } // If the operatorList isn't executed completely yet OR the execution