[Pkg-javascript-commits] [pdf.js] 77/161: Make fnArray always be a plain array.

David Prévot taffit at moszumanska.debian.org
Sat Apr 19 14:16:30 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository pdf.js.

commit 292b96a0bfe7354e8418b194cb6c749aa7abb011
Author: Nicholas Nethercote <nnethercote at mozilla.com>
Date:   Sat Mar 15 06:04:13 2014 -0700

    Make fnArray always be a plain array.
    
    This makes the code much simpler, and the extra memory use is tiny -- a vanilla
    1000 element array is only 4000 bytes larger than a Uint32Array of the same
    size.
---
 src/core/evaluator.js | 69 ++++++++++++++-------------------------------------
 1 file changed, 18 insertions(+), 51 deletions(-)

diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index 5d70a91..c5b5acd 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -1297,17 +1297,10 @@ var OperatorList = (function OperatorListClosure() {
 
   function OperatorList(intent, messageHandler, pageIndex) {
     this.messageHandler = messageHandler;
-    // When there isn't a message handler the fn array needs to be able to grow
-    // since we can't flush the operators.
-    if (messageHandler) {
-      this.fnArray = new Uint8Array(CHUNK_SIZE);
-    } else {
-      this.fnArray = [];
-    }
+    this.fnArray = [];
     this.argsArray = [];
     this.dependencies = {};
     this.pageIndex = pageIndex;
-    this.fnIndex = 0;
     this.intent = intent;
   }
 
@@ -1318,19 +1311,16 @@ var OperatorList = (function OperatorListClosure() {
     },
 
     addOp: function(fn, args) {
+      this.fnArray.push(fn);
+      this.argsArray.push(args);
       if (this.messageHandler) {
-        this.fnArray[this.fnIndex++] = fn;
-        this.argsArray.push(args);
-        if (this.fnIndex >= CHUNK_SIZE) {
+        if (this.fnArray.length >= CHUNK_SIZE) {
           this.flush();
-        } else if (this.fnIndex >= CHUNK_SIZE_ABOUT &&
+        } else if (this.fnArray.length >= CHUNK_SIZE_ABOUT &&
           (fn === OPS.restore || fn === OPS.endText)) {
           // heuristic to flush on boundary of restore or endText
           this.flush();
         }
-      } else {
-        this.fnArray.push(fn);
-        this.argsArray.push(args);
       }
     },
 
@@ -1376,9 +1366,9 @@ var OperatorList = (function OperatorListClosure() {
         pageIndex: this.pageIndex,
         intent: this.intent
       }, null, transfers);
-      this.dependencies = [];
-      this.fnIndex = 0;
-      this.argsArray = [];
+      this.dependencies = {};
+      this.fnArray.length = 0;
+      this.argsArray.length = 0;
     }
   };
 
@@ -1670,22 +1660,6 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessor() {
 })();
 
 var QueueOptimizer = (function QueueOptimizerClosure() {
-  function squash(array, index, howMany, element) {
-    if (isArray(array)) {
-      array.splice(index, howMany, element);
-    } else if (typeof element !== 'undefined') {
-      // Replace the element.
-      array[index] = element;
-      // Shift everything after the element up.
-      var sub = array.subarray(index + howMany);
-      array.set(sub, index + 1);
-    } else {
-      // Shift everything after the element up.
-      var sub = array.subarray(index + howMany);
-      array.set(sub, index);
-    }
-  }
-
   function addState(parentState, pattern, fn) {
     var state = parentState;
     for (var i = 0, ii = pattern.length - 1; i < ii; i++) {
@@ -1709,7 +1683,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
 
       var fnArray = context.fnArray, argsArray = context.argsArray;
       var j = context.currentOperation - 3, i = j + 4;
-      var ii = context.operationsLength;
+      var ii = fnArray.length;
 
       for (; i < ii && fnArray[i - 4] === fnArray[i]; i++) {
       }
@@ -1774,12 +1748,11 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
         }
       }
       // replacing queue items
-      squash(fnArray, j, count * 4, OPS.paintInlineImageXObjectGroup);
+      fnArray.splice(j, count * 4, OPS.paintInlineImageXObjectGroup);
       argsArray.splice(j, count * 4,
         [{width: imgWidth, height: imgHeight, kind: ImageKind.RGBA_32BPP,
           data: imgData}, map]);
       context.currentOperation = j;
-      context.operationsLength -= count * 4 - 1;
     });
 
   addState(InitialState,
@@ -1793,7 +1766,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
 
       var fnArray = context.fnArray, argsArray = context.argsArray;
       var j = context.currentOperation - 3, i = j + 4;
-      var ii = context.operationsLength;
+      var ii = fnArray.length;
 
       for (; i < ii && fnArray[i - 4] === fnArray[i]; i++) {
       }
@@ -1837,12 +1810,11 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
         }
 
         // replacing queue items
-        squash(fnArray, j, count * 4, OPS.paintImageMaskXObjectRepeat);
+        fnArray.splice(j, count * 4, OPS.paintImageMaskXObjectRepeat);
         argsArray.splice(j, count * 4, [argsArray[j + 2][0],
           argsArray[j + 1][0], argsArray[j + 1][3], positions]);
 
         context.currentOperation = j;
-        context.operationsLength -= count * 4 - 1;
       } else {
         count = Math.min(count, MAX_IMAGES_IN_MASKS_BLOCK);
         var images = [];
@@ -1854,11 +1826,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
         }
 
         // replacing queue items
-        squash(fnArray, j, count * 4, OPS.paintImageMaskXObjectGroup);
+        fnArray.splice(j, count * 4, OPS.paintImageMaskXObjectGroup);
         argsArray.splice(j, count * 4, [images]);
 
         context.currentOperation = j;
-        context.operationsLength -= count * 4 - 1;
       }
     });
 
@@ -1873,7 +1844,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
       if (argsArray[j + 1][1] !== 0 || argsArray[j + 1][2] !== 0) {
         return;
       }
-      var ii = context.operationsLength;
+      var ii = fnArray.length;
       for (; i + 3 < ii && fnArray[i - 4] === fnArray[i]; i += 4) {
         if (fnArray[i - 3] !== fnArray[i + 1] ||
             fnArray[i - 2] !== fnArray[i + 2] ||
@@ -1909,11 +1880,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
       var args = [argsArray[j + 2][0], argsArray[j + 1][0],
         argsArray[j + 1][3], positions];
       // replacing queue items
-      squash(fnArray, j, count * 4, OPS.paintImageXObjectRepeat);
+      fnArray.splice(j, count * 4, OPS.paintImageXObjectRepeat);
       argsArray.splice(j, count * 4, args);
 
       context.currentOperation = j;
-      context.operationsLength -= count * 4 - 1;
     });
 
   addState(InitialState,
@@ -1926,7 +1896,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
 
       var fnArray = context.fnArray, argsArray = context.argsArray;
       var j = context.currentOperation - 4, i = j + 5;
-      var ii = context.operationsLength;
+      var ii = fnArray.length;
 
       for (; i < ii && fnArray[i - 5] === fnArray[i]; i++) {
         if (fnArray[i] === OPS.setFont) {
@@ -1962,12 +1932,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
         k += 5;
       }
       var removed = (count - 1) * 3;
-      squash(fnArray, i, removed);
+      fnArray.splice(i, removed);
       argsArray.splice(i, removed);
 
       context.currentOperation = i;
-      context.operationsLength -= removed;
-
     });
 
   function QueueOptimizer() {
@@ -1977,7 +1945,6 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
       var fnArray = queue.fnArray, argsArray = queue.argsArray;
       var context = {
         currentOperation: 0,
-        operationsLength: argsArray.length,
         fnArray: fnArray,
         argsArray: argsArray
       };
@@ -1989,7 +1956,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
           context.currentOperation = i;
           state = state(context);
           i = context.currentOperation;
-          ii = context.operationsLength;
+          ii = context.fnArray.length;
         }
       }
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git



More information about the Pkg-javascript-commits mailing list