summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2017-06-07 16:18:56 -0400
committerGuillaume Horel <guillaume.horel@gmail.com>2017-06-07 16:18:56 -0400
commite166075af303a0ddbddcc4dfba1a0f194101e023 (patch)
treec624801500ae5f2a8e9dcddd86352321153d3b72
parent011237c2f48381f8341633d84d5ba8f13c80d7a6 (diff)
downloadpyisda-e166075af303a0ddbddcc4dfba1a0f194101e023.tar.gz
zero copy construction from memoryview
-rw-r--r--pyisda/curve.pyx36
1 files changed, 31 insertions, 5 deletions
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index 0650af1..24014c3 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -15,6 +15,11 @@ cimport numpy as np
import numpy as np
np.import_array()
import pandas as pd
+from cpython cimport Py_buffer
+
+cdef extern from "Python.h":
+ int PyMemoryView_Check(object)
+ Py_buffer *PyMemoryView_GET_BUFFER(object)
cdef extern from "numpy/arrayobject.h":
void PyArray_ENABLEFLAGS(np.ndarray arr, int flags)
@@ -48,11 +53,17 @@ cdef class Curve(object):
self._thisptr = make_shared(curve)
@classmethod
- def from_bytes(cls, bytes state):
+ def from_bytes(cls, object state):
cdef:
Curve instance = Curve.__new__(Curve)
- unsigned char* cursor = state
TCurve* curve = <TCurve*>malloc(sizeof(TCurve))
+ Py_buffer* py_buf
+ unsigned char* cursor
+ if PyMemoryView_Check(state):
+ py_buf = PyMemoryView_GET_BUFFER(state)
+ cursor = <unsigned char*>py_buf.buf
+ else:
+ cursor = <bytes?>state
deserialize(cursor, curve)
instance._thisptr = make_shared(curve)
return instance
@@ -301,12 +312,20 @@ cdef class YieldCurve(Curve):
memcpy(self.dates.data(), cursor, num_instr * sizeof(TDate))
@classmethod
- def from_bytes(cls, bytes state):
+ def from_bytes(cls, object state):
cdef:
YieldCurve instance = YieldCurve.__new__(YieldCurve)
- unsigned char* cursor = state
TCurve* curve = <TCurve*>malloc(sizeof(TCurve))
size_t num_instr
+ Py_buffer* py_buf
+ unsigned char* cursor
+
+ if PyMemoryView_Check(state):
+ py_buf = PyMemoryView_GET_BUFFER(state)
+ cursor = <unsigned char*>py_buf.buf
+ else:
+ cursor = <bytes?>state
+
cursor = deserialize(cursor, curve)
instance._thisptr = make_shared(curve)
@@ -522,9 +541,16 @@ cdef class SpreadCurve(Curve):
def from_bytes(cls, bytes state):
cdef:
SpreadCurve instance = SpreadCurve.__new__(SpreadCurve)
- unsigned char* cursor = state
+ unsigned char* cursor
TCurve* curve = <TCurve*>malloc(sizeof(TCurve))
size_t ticker_length
+ Py_buffer* py_buf
+
+ if PyMemoryView_Check(state):
+ py_buf = PyMemoryView_GET_BUFFER(state)
+ cursor = <unsigned char*>py_buf.buf
+ else:
+ cursor = <bytes?>state
cursor = deserialize(cursor, curve)
instance._thisptr = make_shared(curve)