开发者

Responsibility between script and host application

I've been trying to decide how to embed Lua into my application for scripting and extension purposes. I have a class which handles objects that have structures which resemble Lua tables. (specifically a hash map of boost::any) Lua scripts would interact w开发者_如何学编程ith these objects and their hash maps.

It is becoming clear to me that I could write the entirety or a large chunk of this class in Lua (and access it from C), but I am uncertain of the consequences of doing so, particularly memory usage with regard to creating the many tables to represent the hash maps. The reason for coming to this conclusion is that I would like to store high-level structures from Lua in these C objects, but doing so would require an explicit table serialization every time a table is stored into or retrieved from a C object. Theoretically, this approach would offer less memory usage in a trade-off for higher latency per access.

What are the possible courses of action in this situation and their advantages and disadvantages?


I eventually decided to settle on programming the majority of my application in Lua using LuaJIT, for a number of reasons, including:

  • What I was trying to achieve through C/C++ is already there in Lua, the hash tables and metafacilities specifically, and I was basically re-inventing the wheel.
  • I did some simplistic benchmarking and discovered that most of my scripts would invariably end up bouncing calls to C and back to Lua very frequently, and that I could optimize away a lot of this by keeping the runtime focus limited to Lua by leveraging the bytecode compiler.
  • From my limited experience and research on topics surrounding Just-in-Time compilation, LuaJIT (2.0.0-beta8) proves to be quite fast enough for my needs at this point and very possibly near to C++ levels of memory usage for similar data structures.
  • LuaJIT also works excellently as a drop-in replacement for vanilla Lua and is easy to build; all I had to do was link to its library to get it up and running.

I feel that I am losing a little of "power-user" control over my project by doing this, but I consider this to be due to my inexperience with the intricacies of Lua versus my knowledge of C++.


If I have understood your issue correctly, you want to use Lua simply to avoid having to serialize data (to and from a file, I presume). If this is the case, you can use Lua for that, but it seems a bit overkill to me.

Answering your last question, Lua will take more memory than the native C, and will run slower (the Lua VM is slower than native C, and even if you use just C to manipulate the "jumping" from Lua to C and viceversa will have a penalty in the form of lots of pushing and popping in/from the Lua stack).

If you want to have an easier time serializing, probably it'll be better to use a library that serializes to/from C directly. For example, json.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜