diff options
| -rw-r--r-- | pyisda/curve.pyx | 77 |
1 files changed, 56 insertions, 21 deletions
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx index b38dfd8..aaecddc 100644 --- a/pyisda/curve.pyx +++ b/pyisda/curve.pyx @@ -327,26 +327,45 @@ cdef class YieldCurve(Curve): cdef: const TCurve* curve = get_TCurve(self) size_t buf_size = self.size() - unsigned char* buf = <unsigned char*>malloc(buf_size) - unsigned char* cursor = serialize(curve, buf) + char* buf = <char*>malloc(buf_size) + unsigned char* cursor = serialize(curve, <unsigned char*>buf) + int dst_capacity, compressed_size + char* dst + bytes r serialize_vector(self.dates, cursor) - cdef bytes r = buf[:buf_size] + + dst_capacity = LZ4_compressBound(buf_size) + dst = <char*>malloc(dst_capacity) + compressed_size = LZ4_compress_default(buf, dst, buf_size, dst_capacity) + r = dst[:compressed_size] + free(dst) free(buf) return r - def __setstate__(self, bytes state): + def __setstate__(self, bytes state not None): cdef: TCurve* curve = <TCurve*>malloc(sizeof(TCurve)) - const unsigned char* cursor = state + const char* src = state + const unsigned char* cursor + char* dst size_t num_instr + size_t size = PyBytes_GET_SIZE(state) - cursor = deserialize(cursor, curve) - self._thisptr.reset(curve, JpmcdsFreeTCurve) - memcpy(&num_instr, cursor, sizeof(size_t)) - cursor += sizeof(size_t) - self.dates = vector[TDate](num_instr) - memcpy(self.dates.data(), cursor, num_instr * sizeof(TDate)) + with nogil: + dst = <char*>malloc(500) + if LZ4_decompress_safe(src, dst, size, 500) < 0: + free(dst) + raise MemoryError("something went wrong") + else: + cursor = <unsigned char*>dst + cursor = deserialize(cursor, curve) + self._thisptr.reset(curve, JpmcdsFreeTCurve) + memcpy(&num_instr, cursor, sizeof(size_t)) + cursor += sizeof(size_t) + self.dates = vector[TDate](num_instr) + memcpy(self.dates.data(), cursor, num_instr * sizeof(TDate)) + free(dst) def __deepcopy__(self, dict memo): cdef YieldCurve yc = YieldCurve.__new__(YieldCurve) @@ -359,24 +378,40 @@ cdef class YieldCurve(Curve): def from_bytes(cls, object state): cdef: YieldCurve instance = YieldCurve.__new__(YieldCurve) - TCurve* curve = <TCurve*>malloc(sizeof(TCurve)) size_t num_instr - Py_buffer* py_buf const unsigned char* cursor + const char* src + char* dst + TCurve* curve = <TCurve*>malloc(sizeof(TCurve)) + int size = 0 + Py_buffer* py_buf + if PyMemoryView_Check(state): py_buf = PyMemoryView_GET_BUFFER(state) - cursor = <unsigned char*>py_buf.buf + src = <char*>py_buf.buf + size = py_buf.len else: - cursor = <bytes?>state + src = <bytes?>state + size = PyBytes_GET_SIZE(state) - cursor = deserialize(cursor, curve) - instance._thisptr.reset(curve, JpmcdsFreeTCurve) - memcpy(&num_instr, cursor, sizeof(size_t)) - cursor += sizeof(size_t) - instance.dates = vector[TDate](num_instr) - memcpy(instance.dates.data(), cursor, num_instr * sizeof(TDate)) + with nogil: + dst = <char*>malloc(600) + if LZ4_decompress_safe(src, dst, size, 600) < 0: + free(dst) + raise MemoryError("something went wrong") + else: + cursor = <unsigned char*>dst + + cursor = deserialize(cursor, curve) + instance._thisptr.reset(curve, JpmcdsFreeTCurve) + memcpy(&num_instr, cursor, sizeof(size_t)) + cursor += sizeof(size_t) + instance.dates = vector[TDate](num_instr) + memcpy(instance.dates.data(), cursor, num_instr * sizeof(TDate)) + free(dst) + return instance def __hash__(self): |
