april-org / april-ann

APRIL-ANN toolkit (A Pattern Recognizer In Lua with ANNs). This toolkit incorporates ANN algorithms (as dropout, stacked denoising auto-encoders, convolutional NNs), with other pattern recognition methods as HMMs among others. Additionally, in experimental stage, it is possible to perform automatic differentiation, for advanced ML research.
http://april-org.github.io/april-ann/
Other
74 stars 12 forks source link

Avoid multiple allocation when pushing same C++ object into Lua #145

Closed pakozm closed 9 years ago

pakozm commented 9 years ago

Currently, when we push a C++ object into Lua, a userdata is allocated to wrap the C++ pointer. Calls to methods which return the caller object end into unnecessary allocations of multiple Lua userdata. It is possible to alleviate this problem using luaL_ref when pushing objects and storing this reference into Referenced class, allowing to retrieve the same reference when it would be required. Some considerations about this approach are:

  1. The Lua reference should be pushed into the Lua registry, but without counting as a new reference.
  2. When calling destructor of a C++ referenced object, in case it is necessary, remember to use luaL_unref to free the reference.
  3. A C++ userdata can be pushed into Lua by using different faces, because of polymorphism. Sometimes it can be pushed as a derived class, others as its super class. Because both, super and derived object instances, are pointing to the same C++ structure, the same reference in registry will be used for both push actions. However, the Lua variable will be set with a different metatable depending in the class of the object (derived or super class). It is a bad idea to change the metatable of a derived object to be the metatable of its super class, because some methods can be declared in the derived class but not in the super class. A way to solve this problem could be to only allow changing the class of objects only in super->derived direction, because it is a harmless change which can be assumed transparently by other Lua variables pointing to the same object. This consideration is very important and needs to be studied further.
pakozm commented 9 years ago

A new branch, related with commit d2971583b80016e7a9cc829875c5f44d193bd05d , implements this solution. Point (3) has been solved as stated above, only allowing to change metatable when casting from base class to derived class, leaving the metatable untouched in the rest of cases. It is safe to convert in this way because of the simple inheritance method implemented in APRIL-ANN classes. I propose to merge this branch as soon as possible, in order to test in devel release. I have performed tests and debug solving minor technical problems and mistakes introduced in the implementation, and it seems pretty clean now.

pakozm commented 9 years ago

Done