summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2019-12-06 14:58:28 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2019-12-06 14:58:28 -0500
commit74353eb50d17415338c29cadfbdd2224ad1c0def (patch)
tree1492fe3aecbf943d5ab5bc46305d57dd02854519
parent2a869b4cd7d56316a52ed14bf80d3054e0f5d1ca (diff)
downloadpyisda-74353eb50d17415338c29cadfbdd2224ad1c0def.tar.gz
-rw-r--r--pyisda/curve.pxd25
-rw-r--r--pyisda/curve.pyx25
2 files changed, 22 insertions, 28 deletions
diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd
index 88fad34..1c751fb 100644
--- a/pyisda/curve.pxd
+++ b/pyisda/curve.pxd
@@ -44,20 +44,13 @@ cdef extern from "farmhash.h" namespace 'util' nogil:
uint64_t Hash64WithSeed(const char *buff, size_t len, uint64_t len)
cdef inline size_t TCurve_size(const TCurve* curve) nogil:
- return sizeof(int) + sizeof(TDate) + sizeof(double) + \
- sizeof(long) + sizeof(TRatePt) * curve.fNumItems
+ return sizeof(TCurve) + sizeof(TRatePt) * curve.fNumItems
cdef inline unsigned char* serialize(const TCurve* curve, unsigned char* buf) nogil:
- memcpy(buf, &(curve.fNumItems), sizeof(curve.fNumItems))
- buf += sizeof(curve.fNumItems)
+ memcpy(buf, curve, sizeof(TCurve))
+ buf += sizeof(TCurve)
memcpy(buf, curve.fArray, sizeof(TRatePt) * curve.fNumItems)
- buf += sizeof(TRatePt) * curve.fNumItems
- memcpy(buf, &(curve.fBaseDate), sizeof(TDate))
- buf += sizeof(TDate)
- memcpy(buf, &(curve.fBasis), sizeof(double))
- buf += sizeof(double)
- memcpy(buf, &(curve.fDayCountConv), sizeof(long))
- return buf + sizeof(long)
+ return buf + sizeof(TRatePt) * curve.fNumItems
cdef inline void serialize_vector(const vector[TDate]& v, unsigned char* cursor) nogil:
cdef size_t size = v.size()
@@ -67,18 +60,12 @@ cdef inline void serialize_vector(const vector[TDate]& v, unsigned char* cursor)
cdef inline const unsigned char* deserialize(const unsigned char* buf, TCurve* curve) nogil:
- memcpy(&curve.fNumItems, buf, sizeof(curve.fNumItems))
- buf += sizeof(curve.fNumItems)
+ memcpy(curve, buf, sizeof(TCurve))
+ buf += sizeof(TCurve)
cdef size_t array_size = sizeof(TRatePt) * curve.fNumItems
curve.fArray = <TRatePt*>malloc(array_size)
memcpy(curve.fArray, buf, array_size)
buf += array_size
- memcpy(&curve.fBaseDate, buf, sizeof(TDate))
- buf += sizeof(TDate)
- memcpy(&curve.fBasis, buf, sizeof(double))
- buf += sizeof(double)
- memcpy(&curve.fDayCountConv, buf, sizeof(long))
- buf += sizeof(long)
return buf
cdef extern from "isda/cds.h" nogil:
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index b7b94e1..ffbcc94 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -28,11 +28,16 @@ cdef extern from "numpy/arrayobject.h":
int PyArray_TYPE(object)
np.npy_intp PyArray_Size(object)
+cdef extern from "lz4.h":
+ int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity)
+
cdef int SUCCESS = 0
cdef inline void double_free(double* ptr) nogil:
free(ptr)
+cdef inline void char_free(TCurve* ptr) nogil:
+ free(<char*>ptr)
cdef double survival_prob(const TCurve* curve, TDate start_date, TDate maturity_date, double eps) nogil:
cdef:
@@ -353,24 +358,26 @@ cdef class YieldCurve(Curve):
def from_bytes(cls, object state):
cdef:
YieldCurve instance = YieldCurve.__new__(YieldCurve)
- TCurve* curve = <TCurve*>malloc(sizeof(TCurve))
+ TCurve* curve
size_t num_instr
Py_buffer* py_buf
- const unsigned char* cursor
+ const char* cursor
+ int buf_size
+ int pomme
if PyMemoryView_Check(state):
py_buf = PyMemoryView_GET_BUFFER(state)
- cursor = <unsigned char*>py_buf.buf
+ cursor = <char*>py_buf.buf
+ memcpy(&buf_size, cursor, sizeof(int))
+ curve = <TCurve*>malloc(buf_size)
+ cursor += sizeof(int)
+ pomme = LZ4_decompress_safe(cursor, <char*>curve, py_buf.len - 4, buf_size)
+ curve.fArray = <TRatePt*>(curve + sizeof(TCurve))
else:
cursor = <bytes?>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))
+ instance._thisptr.reset(curve, char_free)
return instance
def __hash__(self):