Various speedups to StructReader/Builder

This commit is contained in:
Jason Paryani 2014-09-03 16:50:10 -07:00
parent ea1be42252
commit b11e461a9e
2 changed files with 28 additions and 14 deletions

View file

@ -11,9 +11,12 @@ cdef class _DynamicStructReader:
cdef public object _parent cdef public object _parent
cdef public bint is_root cdef public bint is_root
cdef object _obj_to_pin cdef object _obj_to_pin
cdef object _schema
cdef _init(self, C_DynamicStruct.Reader other, object parent, bint isRoot=?) cdef _init(self, C_DynamicStruct.Reader other, object parent, bint isRoot=?)
cpdef _get(self, field)
cpdef _has(self, field)
cpdef _which(self) cpdef _which(self)
cpdef as_builder(self, num_first_segment_words=?) cpdef as_builder(self, num_first_segment_words=?)

View file

@ -725,13 +725,13 @@ cdef _to_dict(msg, bint verbose):
ret = {} ret = {}
try: try:
which = msg.which() which = msg.which()
ret[which] = _to_dict(getattr(msg, which), verbose) ret[which] = _to_dict(msg._get(which), verbose)
except ValueError: except ValueError:
pass pass
for field in msg.schema.non_union_fields: for field in msg.schema.non_union_fields:
if verbose or msg._has(field): if verbose or msg._has(field):
ret[field] = _to_dict(getattr(msg, field), verbose) ret[field] = _to_dict(msg._get(field), verbose)
return ret return ret
@ -867,15 +867,19 @@ cdef class _DynamicStructReader:
self.thisptr = other self.thisptr = other
self._parent = parent self._parent = parent
self.is_root = isRoot self.is_root = isRoot
self._schema = None
return self return self
cpdef _get(self, field):
return to_python_reader(self.thisptr.get(field), self._parent)
def __getattr__(self, field): def __getattr__(self, field):
return to_python_reader(self.thisptr.get(field), self._parent) return to_python_reader(self.thisptr.get(field), self._parent)
def _get_by_field(self, _StructSchemaField field): def _get_by_field(self, _StructSchemaField field):
return to_python_reader(self.thisptr.getByField(field.thisptr), self._parent) return to_python_reader(self.thisptr.getByField(field.thisptr), self._parent)
def _has(self, field): cpdef _has(self, field):
return self.thisptr.has(field) return self.thisptr.has(field)
def _has_by_field(self, _StructSchemaField field): def _has_by_field(self, _StructSchemaField field):
@ -910,7 +914,9 @@ cdef class _DynamicStructReader:
property schema: property schema:
"""A property that returns the _StructSchema object matching this reader""" """A property that returns the _StructSchema object matching this reader"""
def __get__(self): def __get__(self):
return _StructSchema()._init(self.thisptr.getSchema()) if self._schema is None:
self._schema = _StructSchema()._init(self.thisptr.getSchema())
return self._schema
def __dir__(self): def __dir__(self):
return list(self.schema.fieldnames) return list(self.schema.fieldnames)
@ -945,6 +951,7 @@ cdef class _DynamicStructReader:
def __reduce_ex__(self, proto): def __reduce_ex__(self, proto):
return _struct_reducer, (self.schema.node.id, self.as_builder().to_bytes()) return _struct_reducer, (self.schema.node.id, self.as_builder().to_bytes())
cdef class _DynamicStructBuilder: cdef class _DynamicStructBuilder:
"""Builds Cap'n Proto structs """Builds Cap'n Proto structs
@ -962,11 +969,13 @@ cdef class _DynamicStructBuilder:
cdef public object _parent cdef public object _parent
cdef public bint is_root cdef public bint is_root
cdef bint _is_written cdef bint _is_written
cdef object _schema
cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot = False): cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot = False):
self.thisptr = other self.thisptr = other
self._parent = parent self._parent = parent
self.is_root = isRoot self.is_root = isRoot
self._is_written = False self._is_written = False
self._schema = None
return self return self
cdef _check_write(self): cdef _check_write(self):
@ -1049,18 +1058,18 @@ cdef class _DynamicStructBuilder:
self._is_written = True self._is_written = True
return ret return ret
cdef _get(self, field): cpdef _get(self, field):
cdef C_DynamicValue.Builder value = self.thisptr.get(field) return to_python_builder(self.thisptr.get(field), self._parent)
return to_python_builder(value, self._parent)
def _get_by_field(self, _StructSchemaField field): def _get_by_field(self, _StructSchemaField field):
return to_python_builder(self.thisptr.getByField(field.thisptr), self._parent) return to_python_builder(self.thisptr.getByField(field.thisptr), self._parent)
def __getattr__(self, field): def __getattr__(self, field):
return self._get(field) cdef C_DynamicValue.Builder value = self.thisptr.get(field)
cdef _set(self, field, value): return to_python_builder(value, self._parent)
cpdef _set(self, field, value):
_setDynamicField(self.thisptr, field, value, self._parent) _setDynamicField(self.thisptr, field, value, self._parent)
def _set_by_field(self, _StructSchemaField field, value): def _set_by_field(self, _StructSchemaField field, value):
@ -1068,12 +1077,12 @@ cdef class _DynamicStructBuilder:
_setDynamicField(self.thisptr, field.proto.name, value, self._parent) _setDynamicField(self.thisptr, field.proto.name, value, self._parent)
def __setattr__(self, field, value): def __setattr__(self, field, value):
self._set(field, value) _setDynamicField(self.thisptr, field, value, self._parent)
def _has(self, field): cpdef _has(self, field):
return self.thisptr.has(field) return self.thisptr.has(field)
def _has_by_field(self, _StructSchemaField field): cpdef _has_by_field(self, _StructSchemaField field):
return self.thisptr.hasByField(field.thisptr) return self.thisptr.hasByField(field.thisptr)
cpdef init(self, field, size=None): cpdef init(self, field, size=None):
@ -1214,7 +1223,9 @@ cdef class _DynamicStructBuilder:
property schema: property schema:
"""A property that returns the _StructSchema object matching this writer""" """A property that returns the _StructSchema object matching this writer"""
def __get__(self): def __get__(self):
return _StructSchema()._init(self.thisptr.getSchema()) if self._schema is None:
self._schema = _StructSchema()._init(self.thisptr.getSchema())
return self._schema
def __dir__(self): def __dir__(self):
return list(self.schema.fieldnames) return list(self.schema.fieldnames)