From e1bc7b62f6114dd4960ceaaebd5310dd29de91dd Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 12 Aug 2016 21:10:46 +0200 Subject: [PATCH] lz4: reuse helpers.Buffer --- borg/compress.pyx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/borg/compress.pyx b/borg/compress.pyx index 13955a86b..f0dd0b1b3 100644 --- a/borg/compress.pyx +++ b/borg/compress.pyx @@ -1,25 +1,18 @@ -import threading import zlib try: import lzma except ImportError: lzma = None +from .helpers import Buffer + cdef extern from "lz4.h": int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize) nogil int LZ4_decompress_safe(const char* source, char* dest, int inputSize, int maxOutputSize) nogil int LZ4_compressBound(int inputSize) nogil -thread_local = threading.local() -thread_local.buffer = bytes() - - -cdef char *get_buffer(size): - size = int(size) - if len(thread_local.buffer) < size: - thread_local.buffer = bytes(size) - return thread_local.buffer +buffer = Buffer(bytearray, size=0) cdef class CompressorBase: @@ -88,7 +81,8 @@ class LZ4(CompressorBase): cdef char *source = idata cdef char *dest osize = LZ4_compressBound(isize) - dest = get_buffer(osize) + buf = buffer.get(osize) + dest = buf with nogil: osize = LZ4_compress_limitedOutput(source, dest, isize, osize) if not osize: @@ -108,7 +102,8 @@ class LZ4(CompressorBase): # allocate more if isize * 3 is already bigger, to avoid having to resize often. osize = max(int(1.1 * 2**23), isize * 3) while True: - dest = get_buffer(osize) + buf = buffer.get(osize) + dest = buf with nogil: rsize = LZ4_decompress_safe(source, dest, isize, osize) if rsize >= 0: