[Pkg-javascript-commits] [pdf.js] 171/207: Improve how DecodeStream handles empty buffers.

David Prévot taffit at moszumanska.debian.org
Mon Jul 28 15:36:45 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 db866945b7650d96c052c184c58856d7df277e5c
Author: Nicholas Nethercote <nnethercote at mozilla.com>
Date:   Wed Jul 2 18:39:23 2014 -0700

    Improve how DecodeStream handles empty buffers.
    
    DecodeStream currently initializes its |buffer| field to |null|, which
    is reasonable, because lots of DecodeStreams never need to instantiate a
    buffer. But this requires various special cases in the code.
    
    This patch change it so DecodeStreamClosure has a single empty
    Uint8Array which gets shared between all buffers upon initialization.
    This avoids the special cases.
    
    DecodeStream.prototype.ensureBuffer() is really hot, and this removes a
    test from the fast path. For one 226 page scanned document this sped up
    rendering by about 2%.
---
 src/core/stream.js | 28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/src/core/stream.js b/src/core/stream.js
index 406d4d9..e8afcc8 100644
--- a/src/core/stream.js
+++ b/src/core/stream.js
@@ -116,11 +116,17 @@ var StringStream = (function StringStreamClosure() {
 
 // super class for the decoding streams
 var DecodeStream = (function DecodeStreamClosure() {
+  // Lots of DecodeStreams are created whose buffers are never used.  For these
+  // we share a single empty buffer. This is (a) space-efficient and (b) avoids
+  // having special cases that would be required if we used |null| for an empty
+  // buffer.
+  var emptyBuffer = new Uint8Array(0);
+
   function DecodeStream(maybeMinBufferLength) {
     this.pos = 0;
     this.bufferLength = 0;
     this.eof = false;
-    this.buffer = null;
+    this.buffer = emptyBuffer;
     this.minBufferLength = 512;
     if (maybeMinBufferLength) {
       // Compute the first power of two that is as big as maybeMinBufferLength.
@@ -139,23 +145,15 @@ var DecodeStream = (function DecodeStreamClosure() {
     },
     ensureBuffer: function DecodeStream_ensureBuffer(requested) {
       var buffer = this.buffer;
-      var current;
-      if (buffer) {
-        current = buffer.byteLength;
-        if (requested <= current) {
-          return buffer;
-        }
-      } else {
-        current = 0;
+      if (requested <= buffer.byteLength) {
+        return buffer;
       }
       var size = this.minBufferLength;
       while (size < requested) {
         size *= 2;
       }
       var buffer2 = new Uint8Array(size);
-      if (buffer) {
-        buffer2.set(buffer);
-      }
+      buffer2.set(buffer);
       return (this.buffer = buffer2);
     },
     getByte: function DecodeStream_getByte() {
@@ -199,12 +197,6 @@ var DecodeStream = (function DecodeStreamClosure() {
           this.readBlock();
         }
         end = this.bufferLength;
-
-        // checking if bufferLength is still 0 then
-        // the buffer has to be initialized
-        if (!end) {
-          this.buffer = new Uint8Array(0);
-        }
       }
 
       this.pos = end;

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