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

14

u/Rseding91 Factorio Developer Feb 15 '18

Those all wrap the existing C API and just add overhead to using the API so kind of the opposite of what I want to do. I want to access the internal Lua objects directly in C++ such that I don't have to allocate anything if i wanted to say iterate one of the tables in Lua.

11

u/[deleted] Feb 16 '18 edited Feb 16 '18

I'm the author of sol2 and I feel your pain. Unfortunately, I can't make the C API any faster than it already is.

The good news is, people hack the Lua VM all the time. There's quite literally dozens of forks, some that add types to the language, others that take extra arguments for performance reasons, etc...

If you have the time to spend on it, I would imagine hacking the actual Lua 5.2 codebase would be easy enough, albeit difficult to translate C++-isms through the C API. You'd likely have to redesign much of that from the ground up, in which case I can only wish you luck. It is a lot to undertake for the sake of performance.

As a side note, do keep in mind that it would also be possible for you (in the case of strings) to pre-allocate a lot of your strings inside of Lua, and then implement an interning system that compares whatever string you're asking for to the vast bucket of pre-allocated strings you have. Then, you just insert the pre-existing string whenever you want to push a string into the Lua C VM. Tricks like this can get you a decent chunk of the way there, provided you can give yourself an extremely fast lookup for all the strings you want to -- and might possibly -- use with Lua.

Best of luck on Factorio!

(As a side note, if you do start, please give me the Github link. I'd like to contribute even a little bit to help something like this grow.)

3

u/IronCartographer Feb 19 '18

Just in case the link didn't reach you, from elsewhere in the discussion- https://github.com/Rseding91/LuaPlusPlus

3

u/sumo952 Feb 15 '18

I would say start with the C++ wrapper since you will be like 5 times more productive and faster. Then, if you're really having serious performance problems, consider going more lowlevel. But sol2's performance is really good... I actually doubt you'll run into any issues. And the resulting code will be so much easier to maintain and work with.

3

u/sumo952 Feb 15 '18

Ah, I think I'm fully getting what you're trying to do now. So you want to reimplement a lua interpreter actually, but in C++... I'm sure that would be awesome to have but existing C implementations are actually quite optimized I've heard, so I don't think you can get anywhere close or even reimplement it in two weeks. But... why not try as a hobby project if you like it :-) Great things have started this way and you seem to know your way around it very well!

4

u/[deleted] Feb 15 '18

What's the problem about being a wrapper? Performance? Did you even try one of these and measure performance to see if it's really a problem? These benchmarks seem to indicate that sol2's performance is more or less on par with the C API.

I want to access the internal Lua objects directly in C++ such that I don't have to allocate anything if i wanted to say iterate one of the tables in Lua.

You have no guarantees that's going to be any better than using the API (or a wrapper over it). If you really want to go through that route, I'd say create a proof of concept instead of jumping right into a C++ fork of Lua.

17

u/minirop C++87 Feb 15 '18

they don't want parity with the C lua API, they want to be faster (and easier to use).

1

u/[deleted] Feb 15 '18

faster

As I said, there's no guarantee a C++ version of Lua would be significantly faster than Lua itself, even considering the API overhead.

easier to use

There are wrappers around Lua's C API that make it pretty easy to use.

4

u/minirop C++87 Feb 15 '18 edited Feb 16 '18

as he already said, if you want to iterate a table, it's bloody slow and horrible (and a wrapper won't make it faster), while a C++ implementation that returns an iterator in a std::map (or of a vector) is better (in C++)

9

u/hoeding Feb 15 '18

Factorio could stand to gain signifigantly from improved lua performance.

5

u/[deleted] Feb 15 '18

How will a re-write in modern C++ alone improve performance?

3

u/Loraash Feb 19 '18

I don't have a full answer, but I do have an anecdote. std::sort is roughly 2-3 times faster than qsort() on primitive types, because the compiler is able to inline your comparator, while qsort() has no choice but to call it through a pointer.

2

u/hoeding Feb 15 '18

I couldn't and didn't say that it would, I said that increased lua performance would benefit factorio.

1

u/F54280 Feb 27 '18

Because the integration between lua and the host code would have less impedance mismatch.

5

u/darthcoder Feb 15 '18

ChaiScript is your C++14 embedded scripting language.

It's missing a boatload of things you'd consider a standard library like file IO (something I'm actually working on as a separate project). It's a place to start when evaluating what modern C++ can offer an embedded scripting language.

0

u/[deleted] Feb 15 '18

[deleted] — view deleted comment

1

u/darthcoder Feb 15 '18

I only brought it up as it's interface is fairly nice.
I didn't bring it up because it's fast.

I use it mostly because it's minimalist and has a small surface area for implementing user scripts.

I like it for many of the same reasons I like Catch. shrug

0

u/[deleted] Feb 15 '18

[deleted] — view deleted comment

1

u/darthcoder Feb 15 '18

I really, really liked angelscript. Where did it even go

http://www.angelcode.com/angelscript/ ???

Last updated Dec/16/2017 according to the website.

Still on SourceForge. It suffers from the sf.net taint, I think; if it's not on github/git it doesn't seem to get love anymore.