Search Unity

Large Lag Spikes In Threaded Loop - Unity 2018

Discussion in 'Scripting' started by brilliantgames, Jun 22, 2018.

  1. brilliantgames

    brilliantgames

    Joined:
    Jan 7, 2012
    Posts:
    1,937
    I'm working on a crowd technology. The system deals with a large amount of agents and data. I've been extremely careful to pool and reuse memory. There are multiple thread operations in the crowd AI which loop infinitely.

    That said, I'm experiencing large lag spikes(sometimes over a 1000ms). The profiler seems to have no idea what's causing them as it claims it's 'editor overhead'. It feels like a garbage collect but the profile says nothing of that either. As I mentioned, I have been very careful with my memory management and every single variable is initialized and reused through out all the for-loops.

    Anyone have any ideas what might be causing this? The part of my code that I feel uneasy about is the fact that there are up to 1000 items removed and added to lists per frame.
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    God with no details this could be *anything* almost.

    But wild guess, are you sure you don't have exceptions happening that are being swallowed due to how async exception handling works?
     
  3. brilliantgames

    brilliantgames

    Joined:
    Jan 7, 2012
    Posts:
    1,937

    Thank you so much. Believe it or not, you were on to something. I had a 'Try/catch' wrapping around one of the forloops which had the error log commented out. I enabled the log and there were a ton of errors, I fixed the errors and it seems to be in pretty great shape now!
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I migrated quite a bit of async code over to jobs. Might want to take a look at that. Basic migration of almost any code is rather painless, even if your data isn't optimized for jobs as in all structs and such. The thing is at the end of that path is burst, which is basically an automatic 5x to 10x speed up on average code. Not too mention the flow is just much better as it's designed to work with the game loop. Exception handling isn't a pain, etc..