Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fishy Lag Spikes #42

Open
Zulban opened this issue Mar 29, 2016 · 11 comments
Open

Fishy Lag Spikes #42

Zulban opened this issue Mar 29, 2016 · 11 comments

Comments

@Zulban
Copy link
Contributor

Zulban commented Mar 29, 2016

Currently there are brief lag spikes, about once a second, in scenes with a few hundred fishies. Based on what was said here:

#10 (comment)

it may be a garbage collector and coroutine issue. If someone would like to take a crack at smoothing out this lag spike without impacting FishSchool and Fish functionality, that would be splendid!

@SamirSangani
Copy link
Collaborator

Thanks for the info, will try and look at it as soon as I get a chance.

@Zulban
Copy link
Contributor Author

Zulban commented Mar 30, 2016

Based on some logs, it looks like the coroutines are all executing at the same time. Meaning the way I staggered them is not working. That is, this line in Fish.cs:

yield return new WaitForSeconds (Random.Range (0, fishSchool.interval));

Does not spread out all the Fish calculations. Unity seems to batch coroutines together, not sure how to fix. I think I'm going to use Update.

@peterwilkinson
Copy link

Its a good find!

@Zulban
Copy link
Contributor Author

Zulban commented Mar 30, 2016

Using Update instead of coroutines only got rid of some of the spiking! Two sources of intermittent spikes explains why it was hard to diagnose, I guess.

Here is a graph of all the times where a fish recalculates:

lag-spikes

So those spikes are outisde Fish.cs (which wasn't the case before, the steps were flat before). Now I'm worried it's the octree code, because the width of those steps is irregular.

@SamirSangani
Copy link
Collaborator

I believe it is the stack trace that is taking up a lot of the memory. Would it be possible to have a page where we can list the scripts and the functionality of that script? I tried to work on it a bit today afternoon but there are so many different parts that its difficult to see what is used where. I would also suggest trying to keep the code as clean as possible since it is very easy to get caught up in a spaghetti code which believe me is a nightmare for team projects.

@Zulban
Copy link
Contributor Author

Zulban commented Mar 30, 2016

Could you elaborate on what you mean about the stack trace? I'm curious why you think that's it, and what kinds of things might fix it.

Indeed I am working on commenting my code now. A page would be ideal but I don't see people maintaining it. However we may be able to get people to comment at the top of all their classes, and ideally all their methods.

@SamirSangani
Copy link
Collaborator

I think it was due to some kind of nested coroutines or nested functions that allocated memory and were either not cleared or the GC did not catch them. I am sorry I did not have time to look into it as I had to run for a meeting but will surely try my best to work on it as soon as I get time. I think the best way to diagnose these are to run a minimal amount of functions required and then adding more method calls when you know that underlying base is running well. You can then see the memory calls in the profiler of Unity (I also used the Android NDK, function called c"++filt" to check the stack trace for Android).

For mobile I would recommend using threads so that you can lock threads and monitor them as well as run a few away from the main thread to get a few more fps. I would also suggest looking into using a structured approach such as MVC with reactive extensions (https://www.assetstore.unity3d.com/en/#!/content/17276). This will allow you to have a clean code as well as making it easier for someone joining in the future to contribute to it.

@Zulban
Copy link
Contributor Author

Zulban commented Mar 30, 2016

Well I didn't nest coroutines at all, and the C# code for FishSchool and Fish are maybe 5 functions deep so I don't think that's it. Thanks for the input though.

Spawning 1000 threads for fish would be slower than simply using Update, no? And I don't need to perform the operations asynchronously, or lock anything, so that sounds a bit crazy to me.

I don't love the MVC paradigm.

What about my Fish and FishSchool code do you find particularly messy?

@SamirSangani
Copy link
Collaborator

I am sorry I did not mean to say your code was messy at all, I was just suggesting some approaches. I am also not sure which codes were running in the scenes (I only tested a couple of scenes lvl1 to 2).

Regarding threads, it just helps to move a thread to other cores since phones from the past few years have been multicore (so its an effective way to distribute CPU usage and increase fps). Peter had mentioned that this might be used on different phones and hence I suggested threads since it may provide you a way to get consistent results across all of them (Updates are a bit of a problem since you are not sure how many of those 200 calls actually completed in that frame and how many were pushed to the next which in a phone with slow processor stacks up). I apologize again for any misunderstandings.

@Zulban
Copy link
Contributor Author

Zulban commented Mar 31, 2016

No need for apologies! If you thought anything about my code was off I'd just be happy to know.

The search for the bottleneck continues :P

@peterwilkinson
Copy link

I just got home. It's quite an interesting conversation here. I think the overall thing we need to do is test often and adjust accordingly, I know @Zulban doesn't have the mobile or cloud setup to know how everything is going right away, and so we try to test and report on its compatibility. We can refactor as we find time, or when the answer presents itself. Problem solving is a big part, and dialogue is important.

The efforts have proven valuable as we tested out 3 different experiences within the idea at the center today. We get more User Experience requirements and focus as we meet and observe our intended users. I adjusted the fish school amounts and the coral generation amounts to get the performance needed to test the experience. Now we'll need to take in the feedback and observations to figure what features were missed, improve on, and how to approach the next stage. Its good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants