Bug 293

Summary: Could use an API to "stash" the Python wrapper for an object
Product: ns-3 Reporter: Gustavo J. A. M. Carneiro <gjcarneiro>
Component: coreAssignee: ns-bugs <ns-bugs>
Status: RESOLVED INVALID    
Severity: enhancement    
Priority: P3    
Version: pre-release   
Hardware: All   
OS: All   

Description Gustavo J. A. M. Carneiro 2008-08-19 13:24:42 UTC
One problem that I have with the current python bindings is that it is not easy to check for two objects being the same.  Lets see an example:

 device = node.GetDevice(0)
 chan1 = device.GetChannel()
 chan2 = device.GetChannel()
 print (chan1 is chan2) # always prints False

The problem is that chan1 and chan2 are two different Python wrappers for the same C++ object.  They should be two references to the same wrapper.

Anyway, I need to fix this in PyBindGen.  However, the way I'll fix this it won't be very scalable, because I am basically forced to create a global map for each class and look up the wrapper for a C++ object there.  

In PyGtk, from which I copy many of the implementation ideas use in PyBindGen, this problem is solved because GObject has a generic mechanism of stashing arbitrary data into an object, g_object_get/set_data():

 http://library.gnome.org/devel/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-set-qdata-full

This makes it very efficient to stash a python wrapper for an object, and then get it back later.  Any chance something similar could be added to ns3::ObjectBase?
Comment 1 Gustavo J. A. M. Carneiro 2008-08-22 10:47:51 UTC
The pybindgen feature was added:

http://bazaar.launchpad.net/~gjc/pybindgen/trunk/revision/563?start_revid=563

At the moment it uses a std::map<void*, PyObject*>, with O(log(N)) lookup time (about 16 lookups for 100000 objects).  It probably isn't awful performance, considering typical Python scripts do not usually manipulate that many objects.  

But of course O(1) lookup would be even better... ;-)
Comment 2 Mathieu Lacage 2008-08-25 11:52:16 UTC
(In reply to comment #1)
> The pybindgen feature was added:
> 
> http://bazaar.launchpad.net/~gjc/pybindgen/trunk/revision/563?start_revid=563
> 
> At the moment it uses a std::map<void*, PyObject*>, with O(log(N)) lookup time
> (about 16 lookups for 100000 objects).  It probably isn't awful performance,
> considering typical Python scripts do not usually manipulate that many objects. 
> 
> But of course O(1) lookup would be even better... ;-)

What you could conceivably do is automatically aggregate to each object another object which contains the pointer back to the python object.


Comment 3 Gustavo J. A. M. Carneiro 2008-08-25 12:40:26 UTC
(In reply to comment #2)
> (In reply to comment #1)
[...]
> > But of course O(1) lookup would be even better... ;-)
> 
> What you could conceivably do is automatically aggregate to each object another
> object which contains the pointer back to the python object.

Great idea!  Ashamed it didn't occur to me... :P