summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2019-02-24 16:33:40 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2019-02-24 16:33:40 -0500
commit867ad7751c8ce4ebaf3192a5f95c596bbe96a57a (patch)
tree576a99af7c5f53bc2b79ac521a0fd08f82f0ca39
parent1ba4c793531c551d5e3c5744c6307422c93162ce (diff)
downloadpyisda-867ad7751c8ce4ebaf3192a5f95c596bbe96a57a.tar.gz
try to build the buffer firstbuffer
-rw-r--r--pyisda/credit_index.pyx2
-rw-r--r--pyisda/curve.pxd17
-rw-r--r--pyisda/curve.pyx8
3 files changed, 10 insertions, 17 deletions
diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx
index 8d2bf34..bd062b1 100644
--- a/pyisda/credit_index.pyx
+++ b/pyisda/credit_index.pyx
@@ -265,7 +265,7 @@ cdef class CreditIndex(CurveList):
def __hash__(self):
cdef:
TCurve* curve = self._curves[0].get()
- size_t size = TCurve_size(curve.fNumItems)
+ size_t size = TCurve_size(curve)
size_t size_recovery = curve.fNumItems * sizeof(double)
size_t buf_size = size + size_recovery + 8 + sizeof(TDate)
unsigned char* buf = <unsigned char*>malloc(buf_size)
diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd
index d996a68..4d09094 100644
--- a/pyisda/curve.pxd
+++ b/pyisda/curve.pxd
@@ -43,21 +43,16 @@ cdef extern from "farmhash.h" namespace 'util' nogil:
uint64_t Hash64(const char *buff, size_t len)
uint64_t Hash64WithSeed(const char *buff, size_t len, uint64_t len)
-cdef inline size_t TCurve_size(int num_items) nogil:
- return sizeof(int) + sizeof(TDate) + sizeof(double) + \
- sizeof(long) + sizeof(TRatePt) * num_items
+cdef inline size_t TCurve_size(const TCurve* curve) nogil:
+ 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))
+ (<TCurve*>buf).fArray = NULL
+ 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
cdef inline void serialize_vector(const vector[TDate]& v, unsigned char* cursor) nogil:
cdef size_t size = v.size()
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index 7c9a8f3..f6c938b 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -61,11 +61,9 @@ cdef class Curve(object):
def __getstate__(self):
cdef:
size_t curve_size = self.size()
- unsigned char* buf = <unsigned char*>malloc(curve_size * sizeof(unsigned char))
+ unsigned char* buf = <unsigned char*>malloc(curve_size * sizeof(char))
serialize(self.curve(), buf)
- cdef bytes r = buf[:curve_size]
- free(buf)
- return r
+ return <unsigned char[:curve_size]>(buf)
def __setstate__(self, bytes state):
cdef:
@@ -75,7 +73,7 @@ cdef class Curve(object):
self._thisptr = make_shared(curve)
cdef size_t size(self):
- return TCurve_size(self._thisptr.get().fNumItems)
+ return TCurve_size(self._thisptr.get())
@classmethod
def from_bytes(cls, object state):