~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""Bzrlib specific gzip tunings. We plan to feed these to the upstream gzip."""
19
19
 
21
21
 
22
22
# make GzipFile faster:
23
23
import gzip
24
 
from gzip import FEXTRA, FCOMMENT, FNAME, FHCRC
 
24
from gzip import U32, LOWU32, FEXTRA, FCOMMENT, FNAME, FHCRC
25
25
import sys
26
26
import struct
27
27
import zlib
32
32
__all__ = ["GzipFile", "bytes_to_gzip"]
33
33
 
34
34
 
35
 
def U32(i):
36
 
    """Return i as an unsigned integer, assuming it fits in 32 bits.
37
 
 
38
 
    If it's >= 2GB when viewed as a 32-bit unsigned int, return a long.
39
 
    """
40
 
    if i < 0:
41
 
        i += 1L << 32
42
 
    return i
43
 
 
44
 
 
45
 
def LOWU32(i):
46
 
    """Return the low-order 32 bits of an int, as a non-negative int."""
47
 
    return i & 0xFFFFFFFFL
48
 
 
49
 
 
50
35
def bytes_to_gzip(bytes, factory=zlib.compressobj,
51
36
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
52
37
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
114
99
        """A tuned version of gzip._write_gzip_header
115
100
 
116
101
        We have some extra constrains that plain Gzip does not.
117
 
        1) We want to write the whole blob at once. rather than multiple
 
102
        1) We want to write the whole blob at once. rather than multiple 
118
103
           calls to fileobj.write().
119
104
        2) We never have a filename
120
105
        3) We don't care about the time
136
121
 
137
122
    def _read(self, size=1024):
138
123
        # various optimisations:
139
 
        # reduces lsprof count from 2500 to
 
124
        # reduces lsprof count from 2500 to 
140
125
        # 8337 calls in 1272, 365 internal
141
126
        if self.fileobj is None:
142
127
            raise EOFError, "Reached EOF"
164
149
 
165
150
        if buf == "":
166
151
            self._add_read_data(self.decompress.flush())
167
 
            if len(self.decompress.unused_data) < 8:
168
 
                raise AssertionError("what does flush do?")
 
152
            assert len(self.decompress.unused_data) >= 8, "what does flush do?"
169
153
            self._gzip_tail = self.decompress.unused_data[0:8]
170
154
            self._read_eof()
171
155
            # tell the driving read() call we have stuffed all the data
191
175
                self._gzip_tail = self.decompress.unused_data[0:8]
192
176
            elif seek_length < 0:
193
177
                # we haven't read enough to check the checksum.
194
 
                if not (-8 < seek_length):
195
 
                    raise AssertionError("too great a seek")
 
178
                assert -8 < seek_length, "too great a seek."
196
179
                buf = self.fileobj.read(-seek_length)
197
180
                self._gzip_tail = self.decompress.unused_data + buf
198
181
            else:
207
190
        """tuned to reduce function calls and eliminate file seeking:
208
191
        pass 1:
209
192
        reduces lsprof count from 800 to 288
210
 
        4168 in 296
 
193
        4168 in 296 
211
194
        avoid U32 call by using struct format L
212
195
        4168 in 200
213
196
        """
214
 
        # We've read to the end of the file, so we should have 8 bytes of
 
197
        # We've read to the end of the file, so we should have 8 bytes of 
215
198
        # unused data in the decompressor. If we don't, there is a corrupt file.
216
199
        # We use these 8 bytes to calculate the CRC and the recorded file size.
217
200
        # We then check the that the computed CRC and size of the
218
201
        # uncompressed data matches the stored values.  Note that the size
219
202
        # stored is the true file size mod 2**32.
220
 
        if not (len(self._gzip_tail) == 8):
221
 
            raise AssertionError("gzip trailer is incorrect length.")
 
203
        assert len(self._gzip_tail) == 8, "gzip trailer is incorrect length."
222
204
        crc32, isize = struct.unpack("<LL", self._gzip_tail)
223
205
        # note that isize is unsigned - it can exceed 2GB
224
206
        if crc32 != U32(self.crc):
228
210
 
229
211
    def _read_gzip_header(self, bytes=None):
230
212
        """Supply bytes if the minimum header size is already read.
231
 
 
 
213
        
232
214
        :param bytes: 10 bytes of header data.
233
215
        """
234
216
        """starting cost: 300 in 3998
271
253
 
272
254
    def readline(self, size=-1):
273
255
        """Tuned to remove buffer length calls in _unread and...
274
 
 
 
256
        
275
257
        also removes multiple len(c) calls, inlines _unread,
276
258
        total savings - lsprof 5800 to 5300
277
259
        phase 2:
281
263
        leading to a drop to:
282
264
        4168 calls in 1977
283
265
        4168 call to read() in 1646
284
 
        - i.e. just reduced the function call overhead. May be worth
 
266
        - i.e. just reduced the function call overhead. May be worth 
285
267
          keeping.
286
268
        """
287
269
        if size < 0: size = sys.maxint
329
311
        # to :
330
312
        # 4168 calls in 417.
331
313
        # Negative numbers result in reading all the lines
332
 
 
 
314
        
333
315
        # python's gzip routine uses sizehint. This is a more efficient way
334
316
        # than python uses to honor it. But it is even more efficient to
335
317
        # just read the entire thing and use cStringIO to split into lines.
342
324
 
343
325
    def _unread(self, buf, len_buf=None):
344
326
        """tuned to remove unneeded len calls.
345
 
 
 
327
        
346
328
        because this is such an inner routine in readline, and readline is
347
329
        in many inner loops, this has been inlined into readline().
348
330
 
349
331
        The len_buf parameter combined with the reduction in len calls dropped
350
 
        the lsprof ms count for this routine on my test data from 800 to 200 -
 
332
        the lsprof ms count for this routine on my test data from 800 to 200 - 
351
333
        a 75% saving.
352
334
        """
353
335
        if len_buf is None:
371
353
            self.offset += data_len
372
354
 
373
355
    def writelines(self, lines):
374
 
        # profiling indicated a significant overhead
 
356
        # profiling indicated a significant overhead 
375
357
        # calling write for each line.
376
358
        # this batch call is a lot faster :).
377
359
        # (4 seconds to 1 seconds for the sample upgrades I was testing).