Responsibility between script and host application

I was trying to decide how to embed Lua in my scripting and extension application. I have a class that processes objects that have structures that resemble Lua tables. (in particular, the boost::any hash map) Lua scripts will interact with these objects and their hash maps.

It becomes clear to me that I can write the whole or most of this class in Lua (and access it from C), but I'm not sure about the consequences of this, especially for memory usage in relation to creating many tables to represent hash maps. The reason for this conclusion is that I would like to store high-level structures from Lua in these C objects, but that would require explicit serialization of the table each time the table is stored or retrieved from object C. Theoretically, this approach would offer less memory usage in compromise for higher latency for every access.

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

+4
source share
2 answers

In the end, I decided to solve by programming most of my application in Lua using LuaJIT , for a number of reasons, including:

  • What I'm trying to achieve with C / C ++ already exists in Lua , hash tables and metafiles, and I basically reinvented the wheel.
  • I did some simplified benchmarking and found that most of my scripts will always distract C calls and return to Lua very often and that I can optimize a lot of this by keeping the focus of the Lua runtime environment using a bytecode compiler.
  • From my limited experience and research on topics related to Just-in-Time compilation, LuaJIT (2.0.0-beta8) turns out to be fast enough for my needs at the moment and very possibly close to C ++ levels of memory usage for similar data structures .
  • LuaJIT also works great as a replacement for replacing Lua vanilla and is easy to build; all I had to do was link his library to run it and run it.

I feel like I'm losing a bit of “power-user” control over my project by doing this, but I believe that this is due to my inexperienced complexity in Lua compared to my C ++ knowledge.

+2
source

If I understand your problem correctly, you want to use Lua simply to avoid having to serialize the data (depending on the file and from the file, I suppose). If so, you can use Lua for this, but for me it is a bit overwhelming.

Answering your last question, Lua will get more memory than native C, and will run slower (Lua VM is slower than native C, and even if you use only C to manipulate “jumps” from Lua to C and Viceversa will have a penalty as a lot of pushing and popping to / from the Lua stack).

If you want to simplify time serialization, it might be better to use a library that serializes to / from C directly. For example, json .

0
source

All Articles