[Pkg-javascript-commits] [pdf.js] 160/210: Terminate getOperationList and getTextContent every 20 ms

David Prévot taffit at moszumanska.debian.org
Thu Jun 5 14:21:13 UTC 2014


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

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

commit 88aa396aca0f4efb8ff85d0ca0d40014316bc332
Author: Yury Delendik <ydelendik at mozilla.com>
Date:   Fri May 9 20:41:03 2014 -0500

    Terminate getOperationList and getTextContent every 20 ms
---
 src/core/evaluator.js | 49 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index 9c2a8dc..5752f64 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -38,6 +38,28 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
     this.fontCache = fontCache;
   }
 
+  // Trying to minimize Date.now() usage and check every 100 time
+  var TIME_SLOT_DURATION_MS = 20;
+  var CHECK_TIME_EVERY = 100;
+  function TimeSlotManager() {
+    this.reset();
+  }
+  TimeSlotManager.prototype = {
+    check: function TimeSlotManager_check() {
+      if (++this.checked < CHECK_TIME_EVERY) {
+        return false;
+      }
+      this.checked = 0;
+      return this.endTime <= Date.now();
+    },
+    reset: function TimeSlotManager_reset() {
+      this.endTime = Date.now() + TIME_SLOT_DURATION_MS;
+      this.checked = 0;
+    }
+  };
+
+  var deferred = Promise.resolve();
+
   var TILING_PATTERN = 1, SHADING_PATTERN = 2;
 
   PartialEvaluator.prototype = {
@@ -569,10 +591,13 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
       var stateManager = new StateManager(initialState || new EvalState());
       var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
       var shading;
+      var timeSlotManager = new TimeSlotManager();
 
       return new Promise(function next(resolve, reject) {
-        var operation, i, ii;
-        while ((operation = preprocessor.read())) {
+        timeSlotManager.reset();
+        var stop, operation, i, ii;
+        while (!(stop = timeSlotManager.check()) &&
+               (operation = preprocessor.read())) {
           var args = operation.args;
           var fn = operation.fn;
 
@@ -735,6 +760,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
           }
           operatorList.addOp(fn, args);
         }
+        if (stop) {
+          deferred.then(function () {
+            next(resolve, reject);
+          });
+          return;
+        }
         // Some PDFs don't close all restores inside object/form.
         // Closing those for them.
         for (i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
@@ -900,8 +931,13 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
         return textChunk;
       }
 
+      var timeSlotManager = new TimeSlotManager();
+
       return new Promise(function next(resolve, reject) {
-        while ((operation = preprocessor.read())) {
+        timeSlotManager.reset();
+        var stop;
+        while (!(stop = timeSlotManager.check()) &&
+               (operation = preprocessor.read())) {
           textState = stateManager.state;
           var fn = operation.fn;
           var args = operation.args;
@@ -1073,7 +1109,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
               break;
           } // switch
         } // while
-
+        if (stop) {
+          deferred.then(function () {
+            next(resolve, reject);
+          });
+          return;
+        }
         resolve(textContent);
       });
     },

-- 
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