Thread: GetTime()
View Single Post
10-02-20, 08:10 PM   #14
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Originally Posted by KL1SK View Post
Yes, I was considering using 'coroutine'. But does it have any advantages other than convenience?
Based on my practice, beautiful code in most cases ~= fast code.
The coroutine are common Lua functions, the main cost of it compares to the normal Lua func is about two parts:

I. The cost of the coroutine's creation is much bigger than call a function, so I build a thread pool to re-use them, you may check the PLoop/Threading.lua#L53-L135, that's an example how to build a thread pool in Lua(Normally people do this in C part, the logic is a little complex). I use __Async__ to wrap the functions, so they can be processed in re-used coroutines.

II. The cost of the coroutine context's switch, the cost is tiny but I can't say that can be ignored, it's a price for putting the logic together.

For async codes, they are processed in several phases, that's why I build a framework for them, I use a task schedule manager to control all those async tasks, so the process can be seperated into several phases without dropping the fps.

So I don't try to reduce the corotuine's switch cost since I can do nothing about it, I just manage the time slice of async tasks to make the game smooth. And that's what can't be done with normal Lua functions, since we can't yield/resume them.

For daily works, the main reason of using coroutines is to avoid the callbacks, but for now the callback mechanism is more popular in the WOW, since most logic is not that complex compares to daily works. And it's a really hard job to make an useful coroutine framework.

For me, the beauty and readability is important, and some useful mechanism can be bring in like get user input directly Scorpio the-user-interaction-apis and much more.

Last edited by kurapica.igas : 10-02-20 at 08:28 PM.
  Reply With Quote