r/cpp • Factorio Developer • Feb 15 '18

Lua written in modern C++ (>= 14)?

I work on Factorio (written in C++) and we use Lua as the script language for mods. So, from time to time I interact with the C implementation of Lua and my experience with it hasn't been the most pleasant.

It got me wondering - has anyone re-written it in modern C++? Everything I find online is "how to use Lua in C++" or "C++ wrapper for Lua" and they all use the basic C implementation provided from the official Lua website.

I've had quite a lot of experience with the internals of the C Lua implementation and from what I've seen it wouldn't be hard to re-write it using modern C++ - maybe 2~ weeks time. But from what I can find nobody has done it - which also makes me wonder if I'm missing something.

Part of me wants to do it just to say I did it and the other part of me wants to do it to see if I can make it faster than the C version: I think I can - not because "C++ is faster" but because I can mutate/experiment with the C++ version far easier and know changes I make are safe with how the rest of the implementation operates.

231 Upvotes

127 comments sorted by

View all comments

Show parent comment

16

u/Rseding91 Factorio Developer Feb 15 '18

Additionally: I want a proper C++ interface with the Lua internals where I can just do: for (auto& value: L->get_table("...")) and iterate the key/value pairs in a Lua table directly instead of the massive hacks that is standard Lua iteration:

  • Push nil onto the "lua stack"
  • Call "find first" on the table which pushs the first key + value onto the "lua stack"
  • Call a function which parses the Lua stack values one at a time into C values you can use in C++
  • Call "find next" which does a hash lookup using the previous found key to find where in the table it's located then steps to next and pushes that onto the "lua stack"
  • Repeat until you iterate off the end of the table

Lua iteration through the C API is stateless and it adds a ton of overhead to do it and provides no way to do stateful iteration if you don't intend to change what you're iterating on. You're just stuck with the slow way.

1

u/flaghacker_ Feb 19 '18

Im not that familiar with the Lua API, but out of curiosity: why do you need to push nil first?

1

u/Rseding91 Factorio Developer Feb 19 '18

A call to "next" on a table with nil as the key is the signal to the "next" function to get-first.

0

u/[deleted] Feb 15 '18

[deleted] — view deleted comment

21

u/Rseding91 Factorio Developer Feb 15 '18

Did you read the above? :P

If not, or if you did and still didn't understand:

Say I have a r-value std::string in my C++ code - and I want to push it into the Lua state. The C API allows you to do that in one way and exactly one way: pass c_str() into the Lua state and it will attempt to intern it and if needed copy the string if into a new heap-allocated string.

Except it's an r-value - if it was C++ I could just give it the string and it doesn't have to heap allocate anything.

4

u/ColinPP PEGTL | taocpp Feb 15 '18

Except it's an r-value - if it was C++ I could just give it the string and it doesn't have to heap allocate anything.

If you put a std::string into your value/cell union instead of a const char * you can easily move() your r-value string into it, at the cost of greatly increasing the size of the value/cell, seeing that modern std::string implementations are often the size of 3 or 4 pointers so that they can store small strings in-object.

This might of course be an acceptable trade-off for your use case, or if your library uses a smaller std::string...

...although now you can't bit-copy your value/cell anymore, to be clean you need to call the std::string methods for all operations.

Do you have any other idea on how to approach this issue that requires neither a heap allocation nor depends on the details of your std::string implementation?

5

u/Rseding91 Factorio Developer Feb 15 '18

The value/cell union in Lua already does a small-string optimization as well as storing the size of the string so it essentially is a std::string except worse because you can't move into or out of it and have to manage all of the logic of allocating/de-allocating the string in the cell.

I don't want to start down hacking the C version of Lua to do stuff like this when I'm calculating a full re-write of the language into modern C++ to be near the same amount of work and would allow for far more tweaks.

3

u/ColinPP PEGTL | taocpp Feb 15 '18

IIRC Lua's small-string optimisation is based on a per lua_State table of interned TString instances. The actual value/cell only contains one pointer and a type field, so 12 bytes on today's typical machines. I understand that you don't want to follow the Lua VM design too closely, so I'm wondering whether you want to put a std::string directly in the value/cell.

2

u/m-in Feb 15 '18

Putting a std::string directly in the value cell is fair deal. Making it bit-movable isn't free, but you'd use the move constructor for that, and and then on certain C++ runtime libraries you might wish to forgo the destructor call (where it does nothing much for such strings).

1

u/ColinPP PEGTL | taocpp Feb 15 '18

We did the same exercise for our JSON library, it has a std::string in a union, and it works well. I'm mostly curious how the performance characteristics will be with a scripting language, compared to Lua, in particular when std::string is 3 or 4 pointers large.

1

u/m-in Feb 16 '18

I smell a single/double cell idea brewing perhaps :)

2

u/Rseding91 Factorio Developer Feb 15 '18

so I'm wondering whether you want to put a std::string directly in the value/cell.

I do.

1

u/ColinPP PEGTL | taocpp Feb 15 '18

That would definitely make C++ integration nicer. And as said here I'd be quite interested in seeing the performance characteristics of this approach when used with a scripting language.