changeset 6783:f966543d105f

[project @ 2007-07-20 16:13:31 by dbateman]
author dbateman
date Fri, 20 Jul 2007 16:13:31 +0000
parents e3f06290847c
children ad4dd4124c16
files src/ChangeLog src/zfstream.cc
diffstat 2 files changed, 23 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Jul 20 16:09:01 2007 +0000
+++ b/src/ChangeLog	Fri Jul 20 16:13:31 2007 +0000
@@ -1,3 +1,9 @@
+2007-07-20  David Bateman  <dbateman@free.fr>
+
+        * zfstream.cc (BUFSIZE): Increase default buffer size to 256kB
+        (gzfilebuf::underflow): Stash the last 16 characters read, so as
+        to avoid calling pbackfail as much as possible.
+
 2007-07-18  David Bateman  <dbateman@free.fr>
 
          * zfstream.cc (int_type gzfilebuf::pbackfail (int_type)): New
--- a/src/zfstream.cc	Fri Jul 20 16:09:01 2007 +0000
+++ b/src/zfstream.cc	Fri Jul 20 16:13:31 2007 +0000
@@ -40,7 +40,8 @@
 #include <cstdio>           // for BUFSIZ
 
 // Internal buffer sizes (default and "unbuffered" versions)
-#define BIGBUFSIZE BUFSIZ
+#define STASHED_CHARACTERS 16
+#define BIGBUFSIZE (256 * 1024 + STASHED_CHARACTERS) 
 #define SMALLBUFSIZE 1
 
 /*****************************************************************************/
@@ -262,9 +263,21 @@
   if (!this->is_open() || !(io_mode & std::ios_base::in))
     return traits_type::eof();
 
+  // Copy the final characters to the front of the buffer
+  int stash = 0;
+  if (this->eback() && buffer && buffer_size > STASHED_CHARACTERS)
+    {
+      char_type *ptr1 = buffer;
+      char_type *ptr2 = this->egptr() - STASHED_CHARACTERS + 1;
+      if (ptr2 > this->eback())
+	while (stash++ <= STASHED_CHARACTERS)
+	  *ptr1++ = *ptr2++;
+    }
+
   // Attempt to fill internal buffer from gzipped file
   // (buffer must be guaranteed to exist...)
-  int bytes_read = gzread(file, buffer, buffer_size);
+  int bytes_read = gzread(file, buffer + stash, buffer_size - stash);
+
   // Indicates error or EOF
   if (bytes_read <= 0)
   {
@@ -272,8 +285,8 @@
     this->setg(buffer, buffer, buffer);
     return traits_type::eof();
   }
-  // Make all bytes read from file available as get area
-  this->setg(buffer, buffer, buffer + bytes_read);
+  // Make all bytes read from file plus the stash available as get area
+  this->setg(buffer, buffer + stash, buffer + bytes_read + stash);
 
   // Return next character in get area
   return traits_type::to_int_type(*(this->gptr()));