Discussion:
Lua Coroutines (Thread) Performance
Timothée VERROUIL
2011-07-13 12:38:17 UTC
Permalink
Hi,

I'm building a Script Engine that binding C++ to Lua.
This part is ok and work correctly but know, I've to implement the game logic part and I'm not really aware about the best practices.
So, here is what I want to do, and the twice solutions I've found.

I've a C++ GameObject and I want to script it with Lua.
So, I've to send a reference of that object to Lua (no problem here) and I've to Update the script each frame of the game loop.
But, I'see 2 ways to do that, each has its advantages and inconvenients.

Notice that to inject a reference, I do something like that :
- I affect a new name to the right metatable :
Metatable["tmpVar123"] = Metatable
- I set it to global
- I affect a local variable to this global

1) Whith Lua Thread
When the game start, for each gameObject which has a script, I create a new Lua Thread from C++ and initialize all of its local variables
(as self which represents the reference of the gameObject).
So, when I Update the script, the local variables are well initialized and there are no collisions with the other scripts because of the Lua Thread.

2) Without Lua Thread
I do the same as with Lua Thread but I don't create one thread for each script.
So, in order to avoid collisions between scripts, I initialize each local variables before Updating the script.

So, I think that to choose between this 2 methods, all depends of the performance of the lua thread (imagine if I have something like 1000 game objects,
so 1000 threads too!)

Thanks,

Tim
HyperHacker
2011-07-13 14:21:29 UTC
Permalink
Post by Timothée VERROUIL
Hi,
I'm building a Script Engine that binding C++ to Lua.
This part is ok and work correctly but know, I've to implement the game
logic part and I'm not really aware about the best practices.
So, here is what I want to do, and the twice solutions I've found.
I've a C++ GameObject and I want to script it with Lua.
So, I've to send a reference of that object to Lua (no problem here) and
I've to Update the script each frame of the game loop.
But, I'see 2 ways to do that, each has its advantages and inconvenients.
     Metatable["tmpVar123"] = Metatable
   - I set it to global
   - I affect a local variable to this global
1) Whith Lua Thread
 When the game start, for each gameObject which has a script, I create a new
Lua Thread from C++ and initialize all of its local variables
(as self which represents the reference of the gameObject).
So, when I Update the script, the local variables are well initialized and
there are no collisions with the other scripts because of the Lua Thread.
2) Without Lua Thread
I do the same as with Lua Thread but I don't create one thread for each script.
So, in order to avoid collisions between scripts, I initialize each local
variables before Updating the script.
So, I think that to choose between this 2 methods, all depends of the
performance of the lua thread (imagine if I have something like 1000 game
objects,
so 1000 threads too!)
Thanks,
Tim
Well in the single Lua state approach, you can just do something like:
-Push the game object "frame" function
-Push the object itself any any arguments to the function
-Call using lua_call or lua_pcall
once for each object that needs to be updated. So there isn't really
any *need* for more than one Lua state. As long as you use a decent
naming convention, global collisions shouldn't be an issue (and
provide a way to share information between objects).

However, using more than one state has the advantage that each state
can run in its own OS thread. As computers add more CPU cores and
become more oriented to parallel processing, being able to take
advantage of all those cores is important. If you do this you can have
multiple objects updating simultaneously, which should offer a
significant speed boost if done well.

Of course, you don't need one Lua state per object either. You might
create one per CPU core, and then divide your objects into groups and
use the single-state approach on each group.


--
Sent from my toaster.
Ivan-Assen Ivanov
2011-07-13 15:24:30 UTC
Permalink
We run 1000s of threads (one or several per entity in the game world)
in games that run on current-gen consoles with their
not-exactly-Lua-loving PowerPC CPUs. The trick is to have them "sleep"
most of the time (e.g. if something is playing an animation which will
last 2.5s and cannot be interrupted, don't resume its Lua thread
before 2.5s have elapsed). The fact of having 1000 Lua threads won't
hurt your performance; what you do with them might.

Assen
Timothée VERROUIL
2011-07-18 08:12:39 UTC
Permalink
Post by HyperHacker
-Push the game object "frame" function
-Push the object itself any any arguments to the function
-Call using lua_call or lua_pcall
once for each object that needs to be updated. So there isn't really
any *need* for more than one Lua state. As long as you use a decent
naming convention, global collisions shouldn't be an issue (and
provide a way to share information between objects).
However, using more than one state has the advantage that each state
can run in its own OS thread. As computers add more CPU cores and
become more oriented to parallel processing, being able to take
advantage of all those cores is important. If you do this you can have
multiple objects updating simultaneously, which should offer a
significant speed boost if done well.
Of course, you don't need one Lua state per object either. You might
create one per CPU core, and then divide your objects into groups and
use the single-state approach on each group.
--
Sent from my toaster.
Hi,

Thanks for your answers.
But, is their an easy way to create a local variable from C++ to Lua ?
I just found how to inject global variables with the setglobal function.
For exemple :

local self = theRefFromC++

And, I don't find how to call a specific function describes in a Lua Script
For example :
MyActor = {}

MyActor:Manage(Object)

end

How can I call this function and how can I be sure that there aren't any collisions with
another actor with the same script ?

Thanks,

Tim
HyperHacker
2011-07-18 08:18:36 UTC
Permalink
Post by Timothée VERROUIL
Post by HyperHacker
-Push the game object "frame" function
-Push the object itself any any arguments to the function
-Call using lua_call or lua_pcall
once for each object that needs to be updated. So there isn't really
any *need* for more than one Lua state. As long as you use a decent
naming convention, global collisions shouldn't be an issue (and
provide a way to share information between objects).
However, using more than one state has the advantage that each state
can run in its own OS thread. As computers add more CPU cores and
become more oriented to parallel processing, being able to take
advantage of all those cores is important. If you do this you can have
multiple objects updating simultaneously, which should offer a
significant speed boost if done well.
Of course, you don't need one Lua state per object either. You might
create one per CPU core, and then divide your objects into groups and
use the single-state approach on each group.
--
Sent from my toaster.
Hi,
Thanks for your answers.
But, is their an easy way to create a local variable from C++ to Lua ?
I just found how to inject global variables with the setglobal function.
local self = theRefFromC++
And, I don't find how to call a specific function describes in a Lua Script
MyActor = {}
MyActor:Manage(Object)
end
How can I call this function and how can I be sure that there aren't any collisions with
another actor with the same script ?
Thanks,
Tim
MyActor:Manage(Object) is just a shortcut for MyActor.Manage(MyActor,
Object), so you can call it that way the same as you'd call any
function.

There's no simple way to create a local variable using the C API, but
of course you can provide a C function that returns whatever you need
and assign that to a local variable as with any function.
--
Sent from my toaster.
Timothée VERROUIL
2011-07-19 07:57:57 UTC
Permalink
Post by HyperHacker
-Push the game object "frame" function
-Push the object itself any any arguments to the function
-Call using lua_call or lua_pcall
once for each object that needs to be updated. So there isn't really
any *need* for more than one Lua state. As long as you use a decent
naming convention, global collisions shouldn't be an issue (and
provide a way to share information between objects).
However, using more than one state has the advantage that each state
can run in its own OS thread. As computers add more CPU cores and
become more oriented to parallel processing, being able to take
advantage of all those cores is important. If you do this you can have
multiple objects updating simultaneously, which should offer a
significant speed boost if done well.
Of course, you don't need one Lua state per object either. You might
create one per CPU core, and then divide your objects into groups and
use the single-state approach on each group.
--
Sent from my toaster.
Hi,

Thanks for your answers.
But, is their an easy way to create a local variable from C++ to Lua ?
I just found how to inject global variables with the setglobal function.
For exemple :

local self = theRefFromC++

And, I don't find how to call a specific function describes in a Lua Script
For example :
MyActor = {}

MyActor:Manage(Object)

end

How can I call this function and how can I be sure that there aren't any collisions with
another actor with the same script ?

Thanks,

Tim

Loading...