Search Unity

Starting a new thread causes frame hit on Android?

Discussion in 'Scripting' started by techmage, Mar 17, 2020.

  1. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I am working on an Oculus Quest project and I am executing a chunk of work on a different thread. To be specific I am parsing a large chunk of JSON that gets downloaded. Now it seems no matter how I trigger the parsing of this JSON chunk, I will usually get like a 5-10 FPS hit.

    To me this just seems pretty absurd that I can't get processing to consistently occur without impacting FPS, doesn't make any sense, if it's another thread doing work, why would it hit fps on unity main thread? I have loaded it up with many debug.logs and ran on the quest itself to confirm the work is being executed on a different thread.

    If I launch this work via Task.Run, I get the FPS hit. If I launch it via ThreadPool.QueueWorkItem, I get the FPS hit.

    HOWEVER, I have noticed that if I launch it via creating a new Thread, setting it to low priority, then calling thread.start(), sometimes that does not cause the FPS hit, but is rare.

    So I have concluded that on Android, or maybe it is specific to Oculus Quest's Android, but this is due to some kind of quirk with thread scheduling. Where I don't think the thread scheduling at the OS level correctly infers how much of a cpu load the work is going to cause, so it puts it on the same CPU as the unity main thread, inadvertently causing the FPS hit to the unity main thread.

    So I am curious. Does anyone have any more ideas about this? Is that the issue? Is there anyway I could force a threaded task to execute on a secondary CPU that the unity main thread isn't on?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    The only thing that can tell you this is the Profiler. What does profiling the code say? You can bring up the profiler window and there are tons of tutorials.

    Also, don't forget there is a pretty big frame rate hit if you are printing a lot with Debug.Log() in that other thread.

    Ultimately it may be necessary to reformat your JSON so that it is an array of smaller chunks of data that you can incrementally parse out. Without understanding your data better, it's hard to suggest anything.
     
  3. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    So any calls to Debug.Log from another thread get sent back to the main thread?
     
  4. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    So all the JSON parsing is on another thread. I have confirmed this by actually seeing it on a background scripting thread in the deep profiler. Also this frame hit does not occur inside the editor. It is only on the quest. Like I just don't get it. Like it really seems to me like it has to do with some pecuilar quirk, like thread affinity or locks deep in the .net SDK taking more?