From 5b3bf80dd89ab08abf0f22a7eba636e7127955e2 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 16 Jan 2011 11:26:01 +0000 Subject: [PATCH] detect mathutils types with mathutils_array_parse(), was using PySequence_Fast(), converting into a tuple every time then back to a float array. gives approx 6x speedup with eg mathutils.Vector(some_vector). --- source/blender/python/generic/mathutils.c | 34 +++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/source/blender/python/generic/mathutils.c b/source/blender/python/generic/mathutils.c index f0eca793028..dc7c1811b80 100644 --- a/source/blender/python/generic/mathutils.c +++ b/source/blender/python/generic/mathutils.c @@ -81,8 +81,7 @@ static char M_Mathutils_doc[] = "This module provides access to matrices, eulers, quaternions and vectors."; -/* helper functionm returns length of the 'value', -1 on error */ -int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix) +static int mathutils_array_parse_fast(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix) { PyObject *value_fast= NULL; @@ -117,6 +116,37 @@ int mathutils_array_parse(float *array, int array_min, int array_max, PyObject * return size; } +/* helper functionm returns length of the 'value', -1 on error */ +int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix) +{ +#if 1 /* approx 6x speedup for mathutils types */ + int size; + + if( (VectorObject_Check(value) && (size= ((VectorObject *)value)->size)) || + (EulerObject_Check(value) && (size= 3)) || + (QuaternionObject_Check(value) && (size= 4)) || + (ColorObject_Check(value) && (size= 3)) + ) { + if(!BaseMath_ReadCallback((BaseMathObject *)value)) { + return -1; + } + + if(size > array_max || size < array_min) { + if (array_max == array_min) PyErr_Format(PyExc_ValueError, "%.200s: sequence size is %d, expected %d", error_prefix, size, array_max); + else PyErr_Format(PyExc_ValueError, "%.200s: sequence size is %d, expected [%d - %d]", error_prefix, size, array_min, array_max); + return -1; + } + + memcpy(array, ((BaseMathObject *)value)->data, size * sizeof(float)); + return size; + } + else +#endif + { + return mathutils_array_parse_fast(array, array_min, array_max, value, error_prefix); + } +} + //----------------------------------MATRIX FUNCTIONS--------------------