Thursday, May 29, 2008

Making a patch for boost.python (accepted)

I made a patch for boost::python and sent it to the boost developers.
Currently with boost 1.35.0, you can write a code like this to access to an attribute in an object. A:

object attrobject = obj.attr("position");

With my patch, you will also be able to write the code like this. B:

object attrnameobject("position");
object attrobject = obj.attr(attrnameobject);

Internally, A does roughly three things.
A-1) Create a Python string object which has "position".
A-2) Access to an object's attribute using the string object.
A-3) Delete the string object
So if you execute A many times, each time you create a string object and delete it. If there are lots of objects and you need to access to "position" attribute of all of them, it's a waste of time to create and delete a string object "position" each time.

If you use B, you create a string object once by
object attrnameobject("position");
and you can reuse it to access to "position" attribute of every object by

object attrobject = obj.attr(attrnameobject);

and it's more efficient.
According to my test, it could cut off about 30% of time to set value and 45% to get value.

it's been accepted and committed to the boost svn trunk (revision 45918), so you will be able to use this feature at the next boost release(1.36.0).

No comments: