[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11522] branches/2-44-stable/blender/ source/blender/python/api2_2x/Mesh.c: approx 25% speedup in mesh. getVertsFromGroup(), double checked results match previous and no memory leaks.

Campbell Barton cbarton at metavr.com
Fri Aug 10 03:31:30 CEST 2007


Even though this wasn't broken, the way it was working was quite 
sub-optimal and the improvement is logical and easy to verify that it 
works. In normal cases it uses half as much memory as it did before. 
(would make a copy of the list and return it)

However when getting vert weights for a single vert...
me.getVertsFromGroup('group', 0, [i])

The function would create a list the size of the mesh verts, even though 
the size of the list thats returned always matches the one passed to it.

I tested this and the worst case is that you have a large mesh and want 
to get 1 vert weight.

in a loop of 50,000 on a mesh with 98306 verts

before it took average of 5sec, after this change it takes ~0.02,
even though this is contrived, in some cases scripts do only get the 
weight for a small number of verts.

I figured if I double checked the resulting output was no different and 
no memory leaks it would be ok, but can revert if people feel strongly 
about this.


Ken Hughes wrote:
> What bug did ths fix?
> 
> Ken
> 
> Campbell Barton wrote:
>> Revision: 11522
>>           http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11522
>> Author:   campbellbarton
>> Date:     2007-08-09 01:14:03 +0200 (Thu, 09 Aug 2007)
>>
>> Log Message:
>> -----------
>> approx 25% speedup in mesh.getVertsFromGroup(),  double checked results match previous and no memory leaks.
>>
>> Modified Paths:
>> --------------
>>     branches/2-44-stable/blender/source/blender/python/api2_2x/Mesh.c
>>
>> Modified: branches/2-44-stable/blender/source/blender/python/api2_2x/Mesh.c
>> ===================================================================
>> --- branches/2-44-stable/blender/source/blender/python/api2_2x/Mesh.c	2007-08-08 21:10:03 UTC (rev 11521)
>> +++ branches/2-44-stable/blender/source/blender/python/api2_2x/Mesh.c	2007-08-08 23:14:03 UTC (rev 11522)
>> @@ -6152,7 +6152,6 @@
>>  	PyObject *vertexList;
>>  	Object *object;
>>  	Mesh *mesh;
>> -	PyObject *tempVertexList;
>>  
>>  	int num = 0;
>>  	int weightRet = 0;
>> @@ -6191,15 +6190,14 @@
>>  		return EXPP_ReturnPyObjError( PyExc_AttributeError,
>>  					      "no deform groups assigned to mesh" );
>>  
>> -	/* temporary list */
>> -	tempVertexList = PyList_New( mesh->totvert );
>> -	if( !tempVertexList )
>> -		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
>> -					      "getVertsFromGroup: can't create pylist!" );
>> -
>>  	count = 0;
>>  
>>  	if( !listObject ) {	/* do entire group */
>> +		vertexList = PyList_New( mesh->totvert );
>> +		if( !vertexList )
>> +			return EXPP_ReturnPyObjError( PyExc_RuntimeError,
>> +							  "getVertsFromGroup: can't create pylist!" );
>> +		
>>  		dvert = mesh->dvert;
>>  		for( num = 0; num < mesh->totvert; num++, ++dvert ) {
>>  			for( i = 0; i < dvert->totweight; i++ ) {
>> @@ -6210,23 +6208,31 @@
>>  								dvert->dw[i].weight );
>>  					else
>>  						attr = PyInt_FromLong ( num );
>> -					PyList_SetItem( tempVertexList, count, attr );
>> +					PyList_SetItem( vertexList, count, attr );
>>  					count++;
>>  				}
>>  			}
>>  		}
>> +		
>> +		if (count < mesh->totvert)
>> +			PyList_SetSlice(vertexList, count, mesh->totvert, NULL);
>> +		
>>  	} else {			/* do individual vertices */
>> -		for( i = 0; i < PyList_Size( listObject ); i++ ) {
>> +		int listObjectLen = PyList_Size( listObject );
>> +		
>> +		vertexList = PyList_New( listObjectLen );
>> +		for( i = 0; i < listObjectLen; i++ ) {
>>  			PyObject *attr = NULL;
>>  
>> -			if( !PyArg_Parse( PyList_GetItem( listObject, i ), "i", &num ) ) {
>> -				Py_DECREF(tempVertexList);
>> +			num = PyInt_AsLong( PyList_GetItem( listObject, i ) );
>> +			if (num == -1) {/* -1 is an error AND an invalid range, we dont care which */
>> +				Py_DECREF(vertexList);
>>  				return EXPP_ReturnPyObjError( PyExc_TypeError,
>>  							      "python list integer not parseable" );
>>  			}
>>  
>>  			if( num < 0 || num >= mesh->totvert ) {
>> -				Py_DECREF(tempVertexList);
>> +				Py_DECREF(vertexList);
>>  				return EXPP_ReturnPyObjError( PyExc_ValueError,
>>  							      "bad vertex index in list" );
>>  			}
>> @@ -6238,17 +6244,15 @@
>>  								dvert->dw[k].weight );
>>  					else
>>  						attr = PyInt_FromLong ( num );
>> -					PyList_SetItem( tempVertexList, count, attr );
>> +					PyList_SetItem( vertexList, count, attr );
>>  					count++;
>>  				}
>>  			}
>>  		}
>> +		if (count < listObjectLen)
>> +			PyList_SetSlice(vertexList, count, listObjectLen, NULL);
>>  	}
>> -	/* only return what we need */
>> -	vertexList = PyList_GetSlice( tempVertexList, 0, count );
>> -
>> -	Py_DECREF( tempVertexList );
>> -
>> +	
>>  	return vertexList;
>>  }
>>  
>>
>>
>> _______________________________________________
>> Bf-blender-cvs mailing list
>> Bf-blender-cvs at blender.org
>> http://lists.blender.org/mailman/listinfo/bf-blender-cvs
>>
>>   
> 
> _______________________________________________
> Bf-committers mailing list
> Bf-committers at blender.org
> http://lists.blender.org/mailman/listinfo/bf-committers
> 


-- 
Campbell J Barton (ideasman42)


More information about the Bf-committers mailing list