Dev Stories: Living on a Single Thread

Loading a lot of Objects or executing a lot of tasks as fast as possible in conjunction with keeping your app responsive – has always been a challenge when you only have a Single Thread at your disposal, as you have to manage timing for suspensions and switching between tasks.

Most modern apps don’t have this problem, as they run in Multiple Threads which are seemingly communicating with each other.

In many cases the  User Interface (UI) is drawn in Main thread and tasks are executed in parallel background threads, making UI rendering independent of background tasks. Meaning that, if background task hangs, UI will still keep running i.e. stay responsive and capable of informing the user of what’s going on.

Unfortunately Unity is not thread safe(as of Aug, 2018) – all calls to Unity API should be done only from Main Thread , otherwise you’ll probably get an Exception. (Yes  I’m aware of the Jobs system and it’s awesome, but not quite there yet)
There is also one more thing to note: in Unity  order of execution of certain functions really matters sometimes and we can’t run them in parallel.

We had an issue with generating a lot of mesh files and textures from our custom serialized format (files were loaded in parallel, but generation of unity objects was serial). If we generated them all at once without waiting for UI redraw – Progress Bar would freeze until very end of the operation, which kinda killed the sole purpose of having a progress indicator.

GIF

Image: Progress bar freezes until the very end (10x fast forward)

That could be fixed by waiting for the end  of the frame for each file. Though we will waste a lot of precious time for no reason.

GIF

Image: Waiting for the end  of the frame for each file

The goal was to generate as much objects per frame as possible. Also to give UI some time to redraw, before continuing with object generation.

To do this I created a “Loop Slicer”, which slices loops into chunks. Each chunk has around 16ms (Less than 1 Frame time on 60FPS, I will experiment with this later) to execute. UI callbacks are executed between each chunk.

Implementation is very simple;  Loop checks the timer before going to next index and will be paused if 16ms limit was exceeded. It will continue After executing callback and waiting for passed waiter.

GIF

Image:  Loop Slicer Chunks

I short Loop slicer’s main purpose is to split a bunch of little tasks between frames in the most efficient way possible(at least for me lol).

And that’s about it for today guys.
Oh and just in case you find this useful here’s the download link:

LoopSlicesr_GF.zip

Feel free to leave a comment down bellow if you have any questions, issues or just found a bug haha.

– Patik

 

Welcome to Golden Fox Games Blog

Hi!

We’re Golden Fox Games, an independent game studio based in Tbilisi, the country of Georgia.

This is supposed to be an introductory blog post, so let’s not talk about how hard it is to make games and CREATE an actual studio and the indie philosophy in general. Maybe next time.

We’re a privately held studio. Co-founded in 2015 by a couple of guys with Business, Marketing, Art and Media and of course die hard gamer background.

We’re working on an Action RPG game called NEILA for digital download platforms that we hope to release in foreseeable future. We will be discussing the actual game in upcoming posts thus keeping you guys up to date on our progress.

Posts will cover everything including (but not limited to):   Programming, Game Design, Level Design, Concept art, Storytelling, Sound design etc.

So sit tight and stay tuned! It’s going to be a fun ride!

You can also follow us on Twitter and Facebook

– Golden Fox