• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ vs Lua (a discussion about optimization)

MutantAssassin

Active Member
Joined
Dec 13, 2009
Messages
43
Solutions
1
Reaction score
29
I need to take some decisions about my project, so I've got questions about optimization. I am wondering about when it is reasonable to choose Lua over source code editing. Lua scripts are usually easier to create and to maintain than c++ code, but it is well known that Lua is a slower than C++. But how much slower is a Lua script compared to the corresponding code in c++? It is surely ok to script things who are called just once in a while, but what if we start scripting nearly everything? As an example, I wonder how bad would it be to let Lua script handle ALL weapons/wands/rods attacks? Or similar things? Could someone with any experience at otservers with heavy usage of Lua scripts like this could provide us some insight?
OBS: Of course, I am assuming that scripts are well written, not poorly optimized spaghetti code.

Cheers,

Assassina Mutante.
 
Last edited:
I have build my prey system in lua, the prey system has defense bonus when monsters attacks a player and has damage bonus when the player attacks the monster which is way easier to implement in C++ (maybe its doable with creaturescripts on onhealthchange), every time a monster damaged the player or the player damaged a monster the C++ code called a lua callback. Imagine AOE spells and the callback being called for each hit, for every player and monster, all the time, everywhere. It worked and my server handled 1500 players online without lags.
So i believe you can implement what you want in lua and worry about performance vs C++ later. Of course, you lua code should be optimized for better results.
Things like getSpectators, GetPathing and other stuff must be inside C++ because their performance in lua would be horrible, but most of the engine could be implemented in lua.
You also should only worry about this if you got many players(that is when performance matters), otherwise pick the easier and fastest way to implements thing which most of time is lua.
 
But how much slower is a Lua script compared to the corresponding code in c++?
Depends on the code. In general Lua is the fastest scripting language available. If you are not going to implement custom pathfinding, world generator or anything heavy on calculations then you are completely fine. Ofc any code written in Lua will be slower than pure C++ but that's not the point. Who cares if the execution time is 1ms vs 2ms (well, as long as you are not executing that code 1000 times every second).
 
Generally the more base kind of system it is, the more it belongs in c++. For example the market is one of core features and is something that doesn't give much room for customization so it's written in c++. I only moved market item descriptions to lua in my own fork because I will be writing some version 12 item bonuses in lua.

Another example is cyclopedia. Most people prefer it in c++ because they won't be customizing it. I wrote it in lua because it's easier to connect achievements to it this way plus my inspect system is in lua already.
 
I think (almost) everything that can be done in Lua, should be done in Lua. I disagree with @zbizu that market would be a good sample for a C++ use-case. The market should be flexible and adaptable, and is a perfect case to write entirely in Lua. The market isn't conceptually a high performance task. The default Lua code should comply with cip client behavior, but make it easy to modify to accomodate interesting and innovative otclient solutions.

I think C++ should be used only where there is a significance performance increase for doing it, such as RSA encrypt/decrypt, thread handling, perhaps path finding, loading big binary objects, working with big chunks of hot memory.

There are lots of developers who has spent lots of time implementing stuff in C++, only to get the code removed and migrated to Lua later. (or get stuck in an obsolete distro, or in maintenance hell) Which is weird, it should be the other way. Start with Lua, if it performs too bad, stop coding like shit, if its still to bad, ask someone else to review your code and tell what is still shit, if its still to bad, look into a C++ solution.

On AcidsOT, I use @Leo32's Rarity Rolls & Custom Attributes Library
I have also rewritten wand and rods attack behavior, as well as paladin weapon behaviors. Entirely in Lua revscript. I'm about to start rewriting the attack behaviors of many knight weapons now. So far I havent had any performance issues on my server. (granted, I havent been able to stress test my server yet, with current online records around 25 players).
 
Last edited:
I have build my prey system in lua, the prey system has defense bonus when monsters attacks a player and has damage bonus when the player attacks the monster which is way easier to implement in C++ (maybe its doable with creaturescripts on onhealthchange), every time a monster damaged the player or the player damaged a monster the C++ code called a lua callback. Imagine AOE spells and the callback being called for each hit, for every player and monster, all the time, everywhere. It worked and my server handled 1500 players online without lags.
So i believe you can implement what you want in lua and worry about performance vs C++ later. Of course, you lua code should be optimized for better results.
Things like getSpectators, GetPathing and other stuff must be inside C++ because their performance in lua would be horrible, but most of the engine could be implemented in lua.
You also should only worry about this if you got many players(that is when performance matters), otherwise pick the easier and fastest way to implements thing which most of time is lua.
That is exactly the kind of reply I was looking for, people who actually have owned otservs with heavy Lua scripts. Thank you.

Depends on the code. In general Lua is the fastest scripting language available. If you are not going to implement custom pathfinding, world generator or anything heavy on calculations then you are completely fine. Ofc any code written in Lua will be slower than pure C++ but that's not the point. Who cares if the execution time is 1ms vs 2ms (well, as long as you are not executing that code 1000 times every second).
Well, my point is exactly about codes which are running all the time. For example, suppose that a server puts all attacks (from monsters and players) scripted in Lua. In this situation, 400 players simultaneously hunting could mean 1600 monsters actives trying to kill them, i.e, 2000 creatures executing attack scripts at the same. Assuming each one attacking once per 2 seconds and you would get that script being executed 1000 times every second.

Generally the more base kind of system it is, the more it belongs in c++. For example the market is one of core features and is something that doesn't give much room for customization so it's written in c++. I only moved market item descriptions to lua in my own fork because I will be writing some version 12 item bonuses in lua.

Another example is cyclopedia. Most people prefer it in c++ because they won't be customizing it. I wrote it in lua because it's easier to connect achievements to it this way plus my inspect system is in lua already.
I was more worried about how expensive is customizing everything in Lua, not actually discussing what should TFS allow customizing.

I think (almost) everything that can be done in Lua, should be done in Lua. I disagree with @zbizu that market would be a good sample for a C++ use-case. The market should be flexible and adaptable, and is a perfect case to write entirely in Lua. The market isn't conceptually a high performance task. The default Lua code should comply with cip client behavior, but make it easy to modify to accomodate interesting and innovative otclient solutions.
I agree. But, in the other hand, I also understand that developers should carefully pick what should be easily customized, because it often means a more complex code, harder to mantain. So they've got to see if the trade off is worthy. Personally, for the Market I would say yes, but that is a subject for a different thread.

I think C++ should be used only where there is a significance performance increase for doing it, such as RSA encrypt/decrypt, thread handling, perhaps path finding, loading big binary objects, working with big chunks of hot memory.

There are lots of developers who has spent lots of time implementing stuff in C++, only to get the code removed and migrated to Lua later. (or get stuck in an obsolete distro, or in maintenance hell) Which is weird, it should be the other way. Start with Lua, if it performs too bad, stop coding like shit, if its still to bad, ask someone else to review your code and tell what is still shit, if its still to bad, look into a C++ solution.
Yes. But anyway it is interesting to foresee what is too heavy for Lua and avoiding the trouble of writing the code twice. I am particularly worried about scripts who might be called thousand of times per second (look at the example I wrote above when replying to @oen432), that is why I felt that it was worthy to ask more experienced OT developers.

On AcidsOT, I use @Leo32's Rarity Rolls & Custom Attributes Library
I have also rewritten wand and rods attack behavior, as well as paladin weapon behaviors. Entirely in Lua revscript. I'm about to start rewriting the attack behaviors of many knight weapons now. So far I havent had any performance issues on my server. (granted, I havent been able to stress test my server yet, with current online records around 25 players).
Even if I was looking for more extreme cases of heavy usage of Lua scripts, it was interesting to know. Thank you.
[offtopic] BTW, I was looking around the site of your OT and it seems very nice. A custom map with several cool custom features... It is a shame for this community that your base player is so small while so many stupid bayak servers easily get hundreds of players. I hope that your project gets more players, that is exactly the kind of server that opentibia needs. [/offtopic]

Cheers,

Assassina Mutante.
 
Well, my point is exactly about codes which are running all the time. For example, suppose that a server puts all attacks (from monsters and players) scripted in Lua. In this situation, 400 players simultaneously hunting could mean 1600 monsters actives trying to kill them, i.e, 2000 creatures executing attack scripts at the same. Assuming each one attacking once per 2 seconds and you would get that script being executed 1000 times every second.
If you are not going to implement custom pathfinding, world generator or anything heavy on calculations then you are completely fine.
Attacks/spells are not even close to what these examples are, even if there are thousands of them.
 
The only thing that could make Lua slow is to concatenate a lot or make calls to very deep tables, anything else is only a consequence of the same interpreter where it is being executed.
So do not worry, any slow factor will be the fault of the engine itself. or ugly scripts!
 
Lua are compiled to bytecode, rather than being native binary code.
The faster the software runs, the better.
C and C++ compile to binary code so they run fast.

LUA / C++ rating:
7iSu1HQ.png
 
I disagree with @zbizu that market would be a good sample for a C++ use-case. The market should be flexible and adaptable, and is a perfect case to write entirely in Lua. The market isn't conceptually a high performance task. The default Lua code should comply with cip client behavior, but make it easy to modify to accomodate interesting and innovative otclient solutions.

I'm pretty sure he meant that most of market stuff are client sided (limited by), that is why there is "no way" to make it customizable... item amount, item prices, currency, item categories, fee tax, etc. everything is limited by the client, we can't quite control them server-side speaking...
 
I'm pretty sure he meant that most of market stuff are client sided (limited by), that is why there is "no way" to make it customizable... item amount, item prices, currency, item categories, fee tax, etc. everything is limited by the client, we can't quite control them server-side speaking...
I don't think so. I would say he meant that migrating market to Lua would still support Cip client by default but also allow server owners that use OTClient to customize market system easier/faster than if it was in sources. Custom features like adding ability to sell/buy using currency other than gold (maybe premium points), adding support for custom attributes and whatever they can think of.
 
Back
Top