Pattern to manage heavy operations
Hello.
I'm planning on writing a piece of code that will process a lot of information and I'm afraid that this will freeze the users client. How can I avoid this situation? Let's put it in an example: Code:
local chunkSize = 100 Thanks! |
you could use onupdate to process x items every frame or coroutines or a mix of both (that's what i do, throttling based on fps)
are you sure the issue isn't how you process (or even access) your data? |
Hey sezz, thanks for your answer.
In the implementation I did the client freezes 1 second until the whole thing is done. Which is not so bad, but I wanted to do it properly so that doesn't happend at all. I know that TSM or other addons move more data than mine and they don't have those problems. I honestly didn't know anything about coroutines. I'm going to read about it, but first I will try with the OnUpdate event. I haven't played enough with it, let's see what I can do. Do you know of any site where they explain coroutines with a good example? I imagine they work as threads, right? |
Ok, so I've been researching a little bit about OnUpdate + coroutines and I think I understand how to do it, basically I have to set the Script "OnUpdate" to call a method where I start my own coroutine. The coroutine will process an specific amount of data, based on time or number of items, and keep the state internaly. Once the coroutine is done, I set OnUpdate to nil so it stops calling the coroutine, and I clear its internal state to be reused. That should do it.
I'm going to implement a test to see how it goes. |
This function takes a workload and chips away at it, moving to the next frame after a budgeted amount of time. The budget is set to 50% of the target FPS, or 50% of the current FPS (if there is no target).
Better alternatives might check after X iterations, or sample the frame rate at more than a single point and maybe average it out. Lua Code:
And here is the testing code I used to make sure it works... Lua Code:
|
This is the code I wrote with your ideas:
Lua Code:
And this is how I use it: Lua Code:
So far this improves the performance a lot. It takes longer to finish, but I'm sure I can improve it changing the value of "state.chunkSize". The issue I find with this code is that it loops through the table returned by GetStuff() several times and I don't see how to avoid it. Thank you both! |
I edited my post some time after originally writing it, as I now had time to log in and do some testing. See above for the updated code.
|
Thanks for the code DahkCeles.
I see that in your example in order to process the loop in chunks I will have to split it and add it to the workload array splitted. I like the way you calculate the duration, taking into account the FPS. With this example I have more than enough to improve the performance. Cheers. |
All times are GMT -6. The time now is 09:08 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI