From 867ad7751c8ce4ebaf3192a5f95c596bbe96a57a Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Sun, 24 Feb 2019 16:33:40 -0500 Subject: try to build the buffer first --- pyisda/credit_index.pyx | 2 +- pyisda/curve.pxd | 17 ++++++----------- pyisda/curve.pyx | 8 +++----- 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 = 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)) + (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 = malloc(curve_size * sizeof(unsigned char)) + unsigned char* buf = malloc(curve_size * sizeof(char)) serialize(self.curve(), buf) - cdef bytes r = buf[:curve_size] - free(buf) - return r + return (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): -- cgit v1.2.3-70-g09d2