Thread Tools Display Modes
10-02-20, 06:49 AM   #1
KL1SK
A Murloc Raider
Join Date: Sep 2020
Posts: 9
Originally Posted by kurapica.igas View Post
I prefer the Lua's coroutine compares to the callback since the logic can be put together
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.
  Reply With Quote
10-02-20, 08:10 PM   #2
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
10-03-20, 03:01 AM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,356
Originally Posted by KL1SK View Post
If I understood correctly "Debug profiling" works constantly. debugprofilestop () just returns its running time?
debugprofilestop() returns the number of milliseconds since the last time debugprofilestart() was called. The value is undefined if the later has never been called, but seems to tick at the same rate anyway.

The idea of these two functions is a misnomer. There isn't some "profiling" code behind them. One stores the current time in memory and the other compares that with the current time.



Originally Posted by KL1SK View Post
thanks for xpcall, never used such a useful feature before . And also does not eat up performance.
xpcall() and pcall() makes the function given run in protected mode, which means any errors encountered returns to the caller. It's useful in code like event handlers and callback libraries that need to run a list of registered functions, any of which will cause cascading problems if an unprotected error occurs. xpcall() lets you specify a custom error handler to run, however I just pipe in the existing one so it still gets the proper call stack in the debug data.



Originally Posted by KL1SK View Post
I decided to abandon OnUpdate in favor of C_Timer, it turned out to be a little cheaper. In addition to this, you can run multiple loops in parallel and they work great. Possibly the engine runs them in different threads, since the load does not grow proportionally.
Lua is a single-threaded environment, meaning it doesn't support true asynchronous threads. OnUpdate guarantees that a frame goes out in between each time the function runs. C_Timer on the other hand adds the function to an internal scheduler that tries to run the function as close to the given time as it can when the engine isn't busy. It is capable of running multiple times in between two frames, so it isn't as reliable in limiting how often a function is run, especially when given very short delays.

A general rule of thumb I follow. OnUpdate when you have a long task that you need to spread out over time. C_Timer when you have a small task that needs to be repeated, but not as frequent.



Originally Posted by KL1SK View Post
I was considering using 'coroutine'. But does it have any advantages other than convenience?
It's just personal preference. Coroutines can be very powerful if you know how to use them. Realistically, I've never really needed them.

What a coroutine does is lets you pause execution of its assigned function and resume it later. Its more specialized use is for multi-stage functions that have a list of multiple heavy purpose tasks you want to spread over time.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 10-03-20 at 03:06 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » GetTime()


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off