Mercurial > touhou
diff pytouhou/utils/lzss.pyx @ 252:b5c7369abd7c
Improve data reading perfs
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 22 Jan 2012 15:54:51 +0100 |
parents | pytouhou/utils/lzss.py@ab826bc29aa2 |
children | 2674c789e0c3 |
line wrap: on
line diff
copy from pytouhou/utils/lzss.py copy to pytouhou/utils/lzss.pyx --- a/pytouhou/utils/lzss.py +++ b/pytouhou/utils/lzss.pyx @@ -12,12 +12,24 @@ ## GNU General Public License for more details. ## -def decompress(bitstream, size, dictionary_size=0x2000, - offset_size=13, length_size=4, minimum_match_length=3): - out_data = [] - dictionary = [0] * dictionary_size - dictionary_head = 1 - while len(out_data) < size: +from libc.stdlib cimport calloc, malloc, free + + +cpdef bytes decompress(object bitstream, + Py_ssize_t size, + unsigned int dictionary_size=0x2000, + unsigned int offset_size=13, + unsigned int length_size=4, + unsigned int minimum_match_length=3): + cdef unsigned int i, ptr, dictionary_head, offset, length + cdef unsigned char flag, byte, *out_data, *dictionary + cdef bytes _out_data + + out_data = <unsigned char*> malloc(size) + dictionary = <unsigned char*> calloc(dictionary_size, 1) + dictionary_head, ptr = 1, 0 + + while ptr < size: flag = bitstream.read_bit() if flag: # The `flag` bit is set, indicating the upcoming chunk of data is a literal @@ -25,15 +37,22 @@ def decompress(bitstream, size, dictiona byte = bitstream.read(8) dictionary[dictionary_head] = byte dictionary_head = (dictionary_head + 1) % dictionary_size - out_data.append(byte) + out_data[ptr] = byte + ptr += 1 else: # The `flag` bit is not set, the upcoming chunk is a (offset, length) tuple offset = bitstream.read(offset_size) length = bitstream.read(length_size) + minimum_match_length - if (offset, length) == (0, 0): + if offset == 0 and length == 0: break for i in range(offset, offset + length): - out_data.append(dictionary[i % dictionary_size]) + out_data[ptr % size] = dictionary[i % dictionary_size] + ptr += 1 dictionary[dictionary_head] = dictionary[i % dictionary_size] dictionary_head = (dictionary_head + 1) % dictionary_size - return b''.join(chr(byte) for byte in out_data) + + _out_data = out_data[:size] + free(out_data) + free(dictionary) + return _out_data +