summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2023-01-24 14:32:29 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2023-01-24 14:32:29 -0500
commit972c4ec8976c51e275ab8c5b01067e26bfe13a79 (patch)
treeebd0a9cee50e75f2b6e22ae53407207f1e1a37dd
parent3ce9f208162e9b041841009a2684d345b705cf2d (diff)
downloadpyisda-972c4ec8976c51e275ab8c5b01067e26bfe13a79.tar.gz
cleanup
-rw-r--r--pyisda/curve.pxd3
-rw-r--r--pyisda/curve.pyx42
2 files changed, 18 insertions, 27 deletions
diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd
index 39e2e8b..5d597b3 100644
--- a/pyisda/curve.pxd
+++ b/pyisda/curve.pxd
@@ -285,6 +285,8 @@ cdef class Curve:
cdef readonly size_t buf_size
cdef inline const TCurve* get_TCurve(self) nogil
+ cpdef bytes as_bytes(self, bint compressed)
+
cdef class YieldCurve(Curve):
pass
@@ -293,7 +295,6 @@ cdef class SpreadCurve(Curve):
cdef uint16_t offset_recovery_rates
cdef uint16_t offset_name
- cpdef bytes as_buffer(self, bint compressed)
cdef inline double* recovery_rates_ptr(self) nogil
cdef inline char* name(self) nogil
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index 5f98f4a..97c642b 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -69,17 +69,26 @@ cdef class Curve(object):
cdef inline const TCurve* get_TCurve(self) nogil:
return <TCurve*>self.buf.get()
- def __getstate__(self):
+ cpdef bytes as_bytes(self, bint compressed):
cdef:
- size_t state_size = self.buf_size - sizeof(TRatePt*)
+ bytes r
int dst_capacity, compressed_size
- dst_capacity = LZ4_compressBound(state_size)
- dst = <char*>malloc(dst_capacity)
- compressed_size = LZ4_compress_default(self.buf.get() + sizeof(TRatePt*), dst, state_size, dst_capacity)
- cdef bytes r = dst[:compressed_size]
- free(dst)
+ char* dst
+ int size = self.buf_size - sizeof(TRatePt*)
+
+ if compressed:
+ dst_capacity = LZ4_compressBound(size)
+ dst = <char*>malloc(dst_capacity)
+ compressed_size = LZ4_compress_default(self.buf.get() + sizeof(TRatePt*), dst, size, dst_capacity)
+ r = dst[:compressed_size]
+ free(dst)
+ else:
+ r = self.buf.get()[sizeof(TRatePt*):self.buf_size]
return r
+ def __getstate__(self):
+ return self.as_bytes(True)
+
def __setstate__(self, bytes state not None):
cdef:
const char* src = state
@@ -806,25 +815,6 @@ cdef class SpreadCurve(Curve):
pydate_to_TDate(d2))
- def __getstate__(self):
- return self.as_buffer(True)
-
- cpdef bytes as_buffer(self, bint compressed):
- cdef:
- bytes r
- int dst_capacity, compressed_size
- char* dst
- int size = self.buf_size - sizeof(TRatePt*)
-
- if compressed:
- dst_capacity = LZ4_compressBound(size)
- dst = <char*>malloc(dst_capacity)
- compressed_size = LZ4_compress_default(self.buf.get() + sizeof(TRatePt*), dst, size, dst_capacity)
- r = dst[:compressed_size]
- free(dst)
- else:
- r = self.buf.get()[sizeof(TRatePt*):self.buf_size]
- return r
def __setstate__(self, bytes state):
super().__setstate__(state)