Effect of LUA Scripting

Effect of LUA Scripting

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

computerquip
Posts: 42
Joined: 28 Dec 2008, 00:42

Effect of LUA Scripting

Post by computerquip »

LUA scripts are a completely interpreted scripting language that can call bindings in C and vice versa through callin and callout functions (from what I've read) Just yesterday I start doing a bit of research on the LUA script language.

POINT: I started thinking about how slow LUA Scripts are and how they could effect FPS in a game. As a result, I was wondering if there was a more direct way of adding a client side mod to speed it up. Though for the most common of scripts, this isn't needed, but there are a few sketchy widgets out that don't exactly help FPS.

Or do I have the wrong idea?
User avatar
MidKnight
Posts: 2652
Joined: 10 Sep 2008, 03:11

Re: Effect of LUA Scripting

Post by MidKnight »

Lua, not LUA
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Effect of LUA Scripting

Post by lurker »

The two main things that will drop performance are rendering and using a very bad algorithm. Rendering is very hard to optimize, while using a sane algorithm is usually pretty easy. Either way, the relative speed of lua vs. C doesn't come into play. If you do need to run heavy calculations, your first reaction should be to make sure it's really necessary, and then you can make a dll or so and load it from lua.
computerquip
Posts: 42
Joined: 28 Dec 2008, 00:42

Re: Effect of LUA Scripting

Post by computerquip »

Cool, thinks for the heads up. And I read about the name on the Lua website. Sorry :$
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Effect of LUA Scripting

Post by zwzsg »

The "Group AI" use .dll you can write in C++, and hopefully haven't yet been completly removed. They were supposed to do the same thing Lua widgets do nowaday, but never caught on.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: Effect of LUA Scripting

Post by hoijui »

group AIs ARE already compleetly removed. they will not be available in the next release anymore. so you should not use them.
computerquip
Posts: 42
Joined: 28 Dec 2008, 00:42

Re: Effect of LUA Scripting

Post by computerquip »

I'm just curious as to why we are making everything in a high level script that is interpreted! It costs CPU and time in some cases. Plus it's like learning a bad version of Basic. Though I guess it's not always needed...
Last edited by computerquip on 27 Mar 2009, 00:21, edited 1 time in total.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Effect of LUA Scripting

Post by imbaczek »

because it's easy to write and distribute and you don't have to use it if it's too slow or otherwise broken.
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: Effect of LUA Scripting

Post by SpliFF »

computerquip wrote:Or do I have the wrong idea?
Yes, you are optimising prematurely. There are many situations where Lua can be faster than C given the same amount of development time. In other words there are optimisations inherent to the way Lua performs hashing or memory management that are extremely efficient. If you tried to emulate these in C you would not have the benefit of the many years that went into Lua's development. If you spent the time optimising up to Lua's standards that would be time you could have spent optimising your actual bottlenecks (like network code or inefficient opengl).

The whole point of Lua and other interpreted languages is to let you develop your code much more quickly. Then, with the benefit of of actual working code, you can benchmark to locate your actual speed problems and move those functions, and only those functions, into C.

Finally, Lua is MUCH faster than other interpreted languages - it should not be painted with a broad (interpreted=slow) brush until you've tested it within the context it is being used. Keep in mind as well that interpreted or not it is loaded as bytecode (like java) so it does not suffer much after the lua file is first loaded.

The short answer is, if something needs to be fast in Spring it's probably already in the engine as a Lua wrapper around a C++ function or as C++ only code. This is why certain things like pathfinding have limited or no Lua interfaces. If you find something that requires C++ speed and isn't in the engine there's a reasonable chance that it could be a useful addition to the engine itself. If so then the Spring devs are always open to patches, if not then put it in a DLL and import the DLL directly via Lua's package system.
computerquip
Posts: 42
Joined: 28 Dec 2008, 00:42

Re: Effect of LUA Scripting

Post by computerquip »

In that sense, Java could also be faster than C++ or C. But clearly it is not. Is this a result of Java being poorly coded or a result of it being interpreted? If Lua is so well coded then why doesn't it get used more often?

The fact is that even with the great programming, it will still be more efficient in C and C++ for reasons quite clear through the ease of programming of Lua. Because they do not require a type, all variables in Lua I'm assuming allocate a large amount of space. This can clearly be avoided in C and C++. Also, Lua doesn't have the simple use of pointers and references which are available in the native code, C++. This is a MUCH more effecient way of passing variables to a function instead of copying temporary variables all over the place. And I'm sure there is much more that I'm missing.

POINT: Lua is in NO way efficient enough for large projects!
Last edited by computerquip on 27 Mar 2009, 02:17, edited 2 times in total.
User avatar
MidKnight
Posts: 2652
Joined: 10 Sep 2008, 03:11

Re: Effect of LUA Scripting

Post by MidKnight »

I may be wrong, but I'm pretty sure that, rather than being interpreted, Java is actually compiled into .class files, then run inside a virtual machine. :P

PS: Thanks for listening. That's one less LUA in the world! :mrgreen:
User avatar
Peet
Malcontent
Posts: 4384
Joined: 27 Feb 2006, 22:04

Re: Effect of LUA Scripting

Post by Peet »

computerquip wrote:If Lua is so well coded then why doesn't it get used more often?
It is used as the scripting language for many games, including large-scale commercial ones. As for your original question, native code as a part of a mod is more or less infeasible, at least in the sense of maintaining spring's cross-platform compatibility.
computerquip
Posts: 42
Joined: 28 Dec 2008, 00:42

Re: Effect of LUA Scripting

Post by computerquip »

I apologize for posting a premature post. Please read my post above.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Effect of LUA Scripting

Post by zwzsg »

SpliFF wrote:if not then put it in a DLL and import the DLL directly via Lua's package system.
Does not work in Gadgets, does not work in Linux and will not work in Windows, afaik.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Effect of LUA Scripting

Post by lurker »

computerquip wrote:POINT: Lua is in NO way efficient enough for large projects!
The interesting thing about optimization is that you can always, always make it faster. Using a language that's a factor slower doesn't matter nearly at all for most applications.
User avatar
Evil4Zerggin
Posts: 557
Joined: 16 May 2007, 06:34

Re: Effect of LUA Scripting

Post by Evil4Zerggin »

Semi-related question: Is there anything that is faster in COB than it is in Lua (excluding, of course, things that can only be done in one or the other)?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Effect of LUA Scripting

Post by FLOZi »

This argument is as idiotic and wrong-end-of-the-sticky as those saying that the lobby server should not be written in Java/Python. :roll:
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Effect of LUA Scripting

Post by lurker »

Doubtful. My highly inaccurate for loop test said "Lua for the love of god Lua"
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Effect of LUA Scripting

Post by imbaczek »

e4z: depends on the jit being active or not; actually i don't know if luajit is working. if it is, lua should be faster pretty much always, otherwise cob will be faster at the stuff cob does, which is moving ints around and nothing more. can't get faster than that without a jit, it simply makes the cpu do less for the same result.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Effect of LUA Scripting

Post by lurker »

My empty for loop was 20x faster in lua (once I made it use numbers that fit into 24 bits so it wouldn't hang). I don't know why, but it was. I think I'll make a test of a few simple programs.
Post Reply

Return to “Engine”