Does Lua need a JIT?

classic Classic list List threaded Threaded
31 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Coda Highland
On Thu, Feb 23, 2017 at 7:35 AM, Dibyendu Majumdar
<[hidden email]> wrote:

> On 23 February 2017 at 15:30, Coda Highland <[hidden email]> wrote:
>> On Thu, Feb 23, 2017 at 6:41 AM, Sergey Rozhenko <[hidden email]> wrote:
>>> I would certainly never want to write any
>>> game logic in C/C++.
>>>
>>
>> I'm a pretty die-hard C++ weenie, and I'll say that it's not as bad as
>> it seems on the surface. Kinda boilerplate-y, yes, but I've never
>> found myself lamenting that I would have an easier time of it in some
>> other language.
>>
>
> This may be of interest:
> Jonathan Blow is creating a new language called JAI for games
> programming (https://www.youtube.com/playlist?list=PLmV5I2fxaiCKfxMBrNsU1kgKJXD3PkyxO).
>

Not text; couldn't read. I don't suppose the guy has any non-video content?

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Dibyendu Majumdar
On 23 February 2017 at 16:48, Coda Highland <[hidden email]> wrote:
> On Thu, Feb 23, 2017 at 7:35 AM, Dibyendu Majumdar
>> This may be of interest:
>> Jonathan Blow is creating a new language called JAI for games
>> programming (https://www.youtube.com/playlist?list=PLmV5I2fxaiCKfxMBrNsU1kgKJXD3PkyxO).
>>
>
> Not text; couldn't read. I don't suppose the guy has any non-video content?
>

Unfortunately no as he hasn't yet made the compiler public. But some
other folks have tried to document:

https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md

Regards

Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Marc Balmer
In reply to this post by 云风 Cloud Wu


Am 22.02.17 um 04:40 schrieb 云风 Cloud Wu:

> Dibyendu Majumdar <[hidden email]
> <mailto:[hidden email]>>于2017年2月22日周三 上午5:12写道:
>
>     I am not a games programmer and do not know whether interpreted Lua is
>     good enough in this case.
>
>
> I have been a game programmer for 20 years, and use lua since 4.0 . I
> think lua is good enough for game project because it's easy to embed to
> game engine. We need a flexible dynamic language for  gameplay  and GUI
> , and the performance only exists in game engine, which is written by C
> or C++.   I remember that I have tried to embed lua, python, javascript
> to my game engine for my first MMORPG project in 2001. lua is the best
> choice, because its less memory overhead, and we can easily manipulate
> the memory usage (We should run the game client in 64M Ram).
>  
>
>     In my opinion, having worked on Ravi and in my usage of it, Lua is not
>     a general purpose language that you would use to build a large scale
>     application. This is not because of any deficiency in Lua, it is more
>     that large scale development is better done in statically typed
>     languages like Java, C#, Go, Swift etc. The cost of developing complex
>     applications in a dynamic language is just too high. I know that many
>     folks on this list will probably disagree with this view, but leaving
>     aside your love for Lua and your wish to use Lua everywhere, would you
>     still use Lua as a general purpose application development platform?
>
>
> We now use lua both in game client and server. For our game server,
> there are about 10K C loc (https://github.com/cloudwu/skynet) for the
> framework , and more than 100K lua loc for gameplay.
>
> For mmo game server, Lua is still the best choice rather than go,erlang,
> python or nodejs because we choose the actor model for the game server
> framework like erlang. We can run thousands lua vm in one process that
> python can't (because of GIL). LuaJIT is not suitable too because of 2G
> memory limit. I think our framework (skynet) has better performance than
> nodejs even nodejs use v8 JIT, because skynet can use all the cpu cores.
> I have tried luaJIT for my framework , only 10%~15% performance
> improvements.
>
> And coroutine in lua can be a better solution for asynchronous
> programming than callback model in nodejs.
>
> I agree lua is not suitable for large program, but a large scale
> application can be build with many small parts. We can run many lua vm
> in one process like skynet does.

Why should Lua not be suitable for large programs?  It absolutely is.


Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Tobias Kieslich

Quoting Marc Balmer <[hidden email]>:

> Am 22.02.17 um 04:40 schrieb 云风 Cloud Wu:
>> Dibyendu Majumdar <[hidden email]
>> <mailto:[hidden email]>>于2017年2月22日周三 上午5:12写道:
>>
>>     I am not a games programmer and do not know whether interpreted Lua is
>>     good enough in this case.
>>
>>
>> I have been a game programmer for 20 years, and use lua since 4.0 . I
>> think lua is good enough for game project because it's easy to embed to
>> game engine. We need a flexible dynamic language for  gameplay  and GUI
>> , and the performance only exists in game engine, which is written by C
>> or C++.   I remember that I have tried to embed lua, python, javascript
>> to my game engine for my first MMORPG project in 2001. lua is the best
>> choice, because its less memory overhead, and we can easily manipulate
>> the memory usage (We should run the game client in 64M Ram).
>>
>>
>>     In my opinion, having worked on Ravi and in my usage of it, Lua is not
>>     a general purpose language that you would use to build a large scale
>>     application. This is not because of any deficiency in Lua, it is more
>>     that large scale development is better done in statically typed
>>     languages like Java, C#, Go, Swift etc. The cost of developing complex
>>     applications in a dynamic language is just too high. I know that many
>>     folks on this list will probably disagree with this view, but leaving
>>     aside your love for Lua and your wish to use Lua everywhere, would you
>>     still use Lua as a general purpose application development platform?
>>
>>
>> We now use lua both in game client and server. For our game server,
>> there are about 10K C loc (https://github.com/cloudwu/skynet) for the
>> framework , and more than 100K lua loc for gameplay.
>>
>> For mmo game server, Lua is still the best choice rather than go,erlang,
>> python or nodejs because we choose the actor model for the game server
>> framework like erlang. We can run thousands lua vm in one process that
>> python can't (because of GIL). LuaJIT is not suitable too because of 2G
>> memory limit. I think our framework (skynet) has better performance than
>> nodejs even nodejs use v8 JIT, because skynet can use all the cpu cores.
>> I have tried luaJIT for my framework , only 10%~15% performance
>> improvements.
>>
>> And coroutine in lua can be a better solution for asynchronous
>> programming than callback model in nodejs.
>>
>> I agree lua is not suitable for large program, but a large scale
>> application can be build with many small parts. We can run many lua vm
>> in one process like skynet does.
>
> Why should Lua not be suitable for large programs?  It absolutely is.

Absolutely agree.  I don't think static typing is a must for a bigger
application.  Good module/package handling however is and the ability to split
functionality into dedicated classes/pices of code.  IMHO Lua doe very well in
this department.

  -T



Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

steve donovan
In reply to this post by GrayFace-2
On Thu, Feb 23, 2017 at 4:41 PM, Sergey Rozhenko <[hidden email]> wrote:
> I've made a script to statically catch unwanted global variables access,
> still haven't posted it to the list BTW. Lack of static typing is
> regrettable, but in practice it wasn't a problem because of this script.
> Misnamed fields are still a possibility, maybe I had 1 or 2.

Definitely - any serious use of Lua requires an external checker. My
version is lglob, which also catches things like 'math.sine' and has
whitelisting. (Descended from lhf's orginal luac output parser, via
the David Manura version.)  I remember reading about Adobe Lightroom
(which has LOTS of Lua) and how _careful_ you have to be with a
language without static typing to manage the process. You cannot be a
cowboy beyond several thousand lines of Lua.

> And don't forget about compilation times, which can be a real pain in big
> applications sometimes.

Oh yes, especially for big game engines, and especially when you want
to hot-patch functionality within a game.  It's a matter of
temperament, as well. I hate long compile times and don't like playing
with light sabers ;)  So Lua remains popular even with relatively
quick static languages like Go (I know this because I did a
reflection-based Lua/Go bridge called luar which has just been broken
by Go 1.8. This is a bummer because I don't do Go at the moment -
still looking for a take-over maintainer.).  Not surprisingly, Lua is
also used by Rust people (Rust is a language that can make C++ compile
times look quick sometimes)

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Aapo Talvensaari
In reply to this post by Marc Balmer
On Wed, Feb 22, 2017 at 5:35 PM, Marc Balmer <[hidden email]> wrote:
Am 21.02.17 um 22:12 schrieb Dibyendu Majumdar:
do we need a JIT for Lua at all?
No.

I feel that some folks use LuaJIT more as a substitute for C - i.e.
they would have coded in C but that is too hard to do, whereas LuaJIt
gives you almost similar capabilities in a much easier to use package,
so they use LuaJIT as if it is C. What do you guys think?
I see it like you.  If you want speed, use C.

In many cases Lua acts as a glue. A possibility to JIT over Lua Code
and C code boundaries is rather cool. That is what you can get by using
LuaJIT and its FFI, and that doesn't work between LuaJIT and Lua C API.
In that sense, you cannot just say, move it to C as it would most probably
mean "move everything in C". Having low level language like C and a higher
level language like Lua and possibility to JIT between these is quite awesome.

I would say, yes, it is nice to have JIT for Lua. Is it a deal breaker? For many
it isn't.


Regards
Aapo
Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Ahmed Charles
In reply to this post by Dibyendu Majumdar
On 2/21/2017 1:12 PM, Dibyendu Majumdar wrote:
> Hi,
>
> Related to my post about why I have not used the JIT technology in
> Ravi for my project (yet), is the question: do we need a JIT for Lua
> at all?

Disclaimer: My preferred languages are either C++ or Rust for heavy
lifting and Lua for scripting (though, I don't script very often).

If one takes a step back and thinks about why people use JIT for a given
language, it's exclusively about performance. If performance wasn't a
factor, an interpreter would be preferred because it's easier to
maintain and write.

So, given that JIT is about performance, one might wonder what
properties of code allows JIT implementations the best chance of
delivering better performance? Well, code which always passes the same
objects of the same types to the same functions, allow for the easiest
time achieving good performance with JIT. Basically, write exactly the
same code as you would in a statically typed language (without a
compiler to help you get it right) and you'll get better performance.

My hypothesis is thus, that if one finds themselves needing a JIT
implementation for their dynamic language, they've chosen the wrong
language for their requirements (or their requirements changed in some
unexpected way that was not appropriately anticipated).

JIT implementations for languages that are statically typed seem even
weirder, since the Just-In-Time part isn't required and you can just
compile the code once on the target at install time. .NET has supported
this for a while, but I have no idea how often it's used.

The one scenario that is ideal for JIT is actually language agnostic and
that is for situations where it is desired that code be delivered and
run on an arbitrary architecture as quickly as possible. For example,
visiting a web page and having complex interactions, like a game. But,
rather than that being a justification for using a dynamic language
(i.e. JavaScript), it should be viewed more as a reason to have
webassembly, which is a language agnostic, ISA-like binary format
designed with sandboxing in mind while allowing relatively easy
translation from it to a given architecture's machine code. The fact
that webassembly is JIT compiled is as relevant to the average
programmer as the fact that all modern x86 cpu's are just JITing x86
assembly to proprietary microcode on the processor.

Anyways, language choice should be based on the problem domain and the
anticipated performance requirements thereof. Implementing really
complex JIT for a specific language is just a sign that a codebase
written in that language achieved more success than the original writers
anticipated. Note, whether or not that success would've been possible if
a different language were chosen initially is next to impossible to
determine. Though, all 'successful', language specific JIT
implementations that I know of follow this pattern.

Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Luke Gorrie-2
In reply to this post by Coda Highland
On 22 February 2017 at 00:55, Coda Highland <[hidden email]> wrote:
IIRC Snabb uses LuaJIT to handle network packet filtering, so that's a
project with some pretty stringent performance requirements that a
non-compiled language would probably have difficulty meeting.

Right. Snabb has a total processing budget of a few hundred CPU cycles per packet to e.g. read it from the network, route it, filter it, transform it, and write it out again. Our code has to be competitive with C written by Intel engineers and aggressively optimized for their latest CPUs using intrinsics and other compiler trickery. This is very challenging with LuaJIT but by some miracle we are succeeding - even in most of the inner loop code.

This application would definitely not be viable with an interpreter due to the practical performance requirements.

(I'm not really sure whether Snabb counts as a "Lua" application or only a "LuaJIT" application. I am a little curious about whether Lua aspires to be a single implementation language or a Lisp-like family of languages or whether it's meaningless to anthropomorphize it like that. I should reread the HOPL paper for context ... :))

Cheers,
-Luke


Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Dibyendu Majumdar
Hi Luke,

On 1 March 2017 at 09:26, Luke Gorrie <[hidden email]> wrote:

> On 22 February 2017 at 00:55, Coda Highland <[hidden email]> wrote:
>>
>> IIRC Snabb uses LuaJIT to handle network packet filtering, so that's a
>> project with some pretty stringent performance requirements that a
>> non-compiled language would probably have difficulty meeting.
>
>
> Right. Snabb has a total processing budget of a few hundred CPU cycles per
> packet to e.g. read it from the network, route it, filter it, transform it,
> and write it out again. Our code has to be competitive with C written by
> Intel engineers and aggressively optimized for their latest CPUs using
> intrinsics and other compiler trickery. This is very challenging with LuaJIT
> but by some miracle we are succeeding - even in most of the inner loop code.

It would be interesting to know why you chose LuaJIT for this
application. What were the driving factors for choosing a dynamically
typed scripting language? Today, with the availability of a language
like Rust, would you still make the same choice?

Thanks and Regards
Dibyendu

Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Dirk Laurie-2
In reply to this post by Luke Gorrie-2
2017-03-01 11:26 GMT+02:00 Luke Gorrie <[hidden email]>:

> I am a little curious about whether Lua aspires to be a single
> implementation language or a Lisp-like family of languages or
> whether it's meaningless to anthropomorphize it like that.

The designers of Lua think of it as a C library, of which the
loading and execution of code written in the Lua scripting
language forms a large part. They offer the Lua interpreter
and the Lua-to-bytecode compiler as sample applications.

The users of Lua embed it, generalize it, write applications
in it, in the process changing the implementation as well
as the language. This is possible because of the very generous
MIT-like license under which Lua is published.

I don't think Lua herself has any aspirations. Last time I asked
her about it, she said: "🌕🌖🌗🌘🌑".

Reply | Threaded
Open this post in threaded view
|

Re: Does Lua need a JIT?

Luke Gorrie-2
In reply to this post by Dibyendu Majumdar
On 1 March 2017 at 11:10, Dibyendu Majumdar <[hidden email]> wrote:
It would be interesting to know why you chose LuaJIT for this
application. What were the driving factors for choosing a dynamically
typed scripting language? Today, with the availability of a language
like Rust, would you still make the same choice?

I prefer dynamic languages to static ones. LuaJIT provided a reasonable compromise between performance, expressiveness, and easy learning curve.

Lua's gentle learning curve is especially valuable in making the code accessible to people who are not professional programmers. This way amateur programmers can contribute to the project directly e.g. the network engineers who build and maintain ISPs and feel a do-it-yourself impulse. If we had assumed that contributors would be professional system programmers then we would end up working with an entirely different group of people i.e. staff of network equipment vendors (and they are already happy with C/C++ anyway.)

I did evaluate Rust for about one hour. It hadn't finished compiling yet when I gave up :).

Here is what I blogged at the time:


(If I were revising that now I would say LuaJIT's implementation is "small" but not "simple" :-))

Cheers,
-Luke


12