# HG changeset patch # User Paul Boddie # Date 1253211031 -7200 # Node ID 67384f696d3c123ecb45d4ec01439a24a21c7ddb # Parent c5a5954934c636efae2cb4aded431962299c19ad Removed caching since it does not seem to help significantly. Removed bzip2 compression since zlib is supposed to be better at textual compression anyway. diff -r c5a5954934c6 -r 67384f696d3c iixr/files.py --- a/iixr/files.py Wed Sep 16 21:12:17 2009 +0200 +++ b/iixr/files.py Thu Sep 17 20:10:31 2009 +0200 @@ -19,18 +19,10 @@ """ from iixr.data import vint -from cStringIO import StringIO -import bz2, zlib +import zlib # Constants. -WRITE_CACHE_SIZE = 100000 -READ_CACHE_SIZE = 10000 -READ_CACHE_RESIZE = 5000 - -compressors = [("b", bz2.compress), ("z", zlib.compress)] -decompressors = {"b" : bz2.decompress, "z" : zlib.decompress} - class File: "A basic file abstraction." @@ -73,8 +65,6 @@ def __init__(self, f): File.__init__(self, f) - self.cache = StringIO() - self.cache_length = 0 def write_number(self, number): @@ -97,14 +87,13 @@ # Compress the string if requested. if compress: - for flag, fn in compressors: - cs = fn(s) + cs = zlib.compress(s) + + # Take any shorter than the original. - # Take the first string shorter than the original. - - if len(cs) < len(s): - s = cs - break + if len(cs) < len(s): + flag = "z" + s = cs else: flag = "-" @@ -119,17 +108,10 @@ # Cache-affected methods. def write(self, s): - self.cache.write(s) - if self.cache.tell() >= WRITE_CACHE_SIZE: - self.flush() + self.f.write(s) def tell(self): - return self.f.tell() + self.cache.tell() - - def flush(self): - self.cache.seek(0) - self.f.write(self.cache.read()) - self.cache = StringIO() + return self.f.tell() class FileReader(File): @@ -137,20 +119,6 @@ def __init__(self, f): File.__init__(self, f) - self.reset_cache(0) - - def reset_cache(self, offset): - self.cache = "" - self.cache_length = 0 - self.cache_start = 0 - self.cache_offset = offset - self.f.seek(offset) - - def resize_cache(self, next_start): - self.cache = self.cache[next_start:] - self.cache_length = len(self.cache) - self.cache_start = 0 - self.cache_offset += next_start def read_number(self): @@ -194,9 +162,8 @@ # Perform decompression if applicable. - if flag != "-": - fn = decompressors[flag] - s = fn(s) + if flag == "z": + s = zlib.decompress(s) # Convert strings to Unicode objects. @@ -205,53 +172,13 @@ # Cache-affected methods. def read(self, n): - needed = n - (self.cache_length - self.cache_start) - - # Read the needed number of characters, if possible. - - if needed > 0: - s = self.f.read(max(needed, READ_CACHE_SIZE)) - self.cache += s - self.cache_length += len(s) - - # Get the end of the requested block. - - next_start = self.cache_start + n - s = self.cache[self.cache_start:next_start] - - # Reposition the pointer to the cache. - - self._seek_cache(len(s)) - return s + return self.f.read(n) def tell(self): - return self.cache_offset + self.cache_start + return self.f.tell() def seek(self, offset): - current = self.tell() - - # If seeking forward, attempt to navigate the cache. - - if offset >= current: - self._seek_cache(offset - current) - else: - self.reset_cache(offset) - - def _seek_cache(self, delta): - next_start = self.cache_start + delta - - if next_start > 0 and next_start >= len(self.cache): - self.reset_cache(self.cache_offset + next_start) - - # If the cache is too big, resize it. - - elif next_start > READ_CACHE_RESIZE: - self.resize_cache(next_start) - - # Otherwise, just reference the next part of the cache. - - else: - self.cache_start = next_start + self.f.seek(offset) class FileOpener: