Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Coroutine for heavy operation

Discussion in 'Editor & General Support' started by Wulirocks, Jun 26, 2022.

  1. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    Hey there,

    I am trying to clarify the process for executing heavy operation using Coroutine so as to minimize lags.
    I am building for WebGL so it is less performant.. and multi threading is not allowed.

    It is about running some code to load some world data:

    Inside my coroutine I run a heavy For Loop like this

    Code (CSharp):
    1.  
    2. int yieldTime =100;
    3.  
    4. int incr =0;
    5.  
    6. for (int i= 0; i < 1000 ; i++ ) {
    7.  
    8. incr++;
    9.  
    10. if ( incr > yieldTime ){ incr =0;
    11.  
    12. yield return new WaitForSeconds( 5f );
    13.  
    14. // execute something heavy
    15.  
    16. }
    17.  
    18. }
    In my understanding everytime WaitForSeconds( 5f ) runs, the game should not lag for 5 seconds

    so basically I give back the control to the the player and the game should run smoothly during 5 sec after which there will be a hiccups.

    Is that the way to go?

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I haven't used them since I published the other brothers back in 2010. But the reason why is because they don't really solve a single thing. They still run on the main thread. I guess a beginner would not know how to iterate a bit at a time each frame and perhaps they'd be better off learning how to do that, or even just not doing that at all.

    If brute force parallelism solving is needed, Unity's solution for that is DOTS. You can use some or all of Jobs alongside main Unity C# without needing to know any ECS or other fancy programming.

    That would be a decent way to tackle huge tasks.
     
    Wulirocks and CodeSmile like this.
  4. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    Great, never heard of Dot... Initially I was using parallelism of multithreading but had to remove it because WebGL does not support system.threading. People keep saying to me I should use async await... but from what I read there is no difference. Async await also runs on the main thread and also use the process or concurrency. Thanks for the DOTS
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'm not sure about Jobs and WebGL - you would have to check that too :(
     
    Wulirocks likes this.
  6. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    Yes can't figure it out now, they say DOT works with WEBGL... but not sure the job system does....
     
  7. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,053
    You still won‘t get multithreading in WebGL since it‘s not supported by Unity yet (and from what I understand the whole idea of multithreading in browsers is still in the draft/development/experimental phase to begin with - not by Unity, by the WWW consortium / browser developers etc).

    Given how fast the web adopts new technologies we can expect to see multithreading in browsers somewhere in the 2030s. Yes, I‘m serious. By this time we may even see WebGL 2.1. If we‘re lucky.

    Web development = extremely conservative and paranoid development.:p
     
  8. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    I see did not know that :).. indeed.. I was looking at the browser Chrome debugger... the problem is it is hard to know which function/line of code will run time times slower on WELGL .. is there any doc in Unity that warns peoples about what runs fast on a PC but runs slow in WEBGL.. because in case of my application many things runs well in WebGL.

    The loading process is complexe, it is just hard to pin down the lines of code in loding process that will put WebGL in difficulties...
     
  9. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,053
    Everything. :p

    Sorry for being cynical but this is web development in a nutshell.:(
    Switching to WebGL is literally like downgrading your PC to a 2010 mobile phone.
     
  10. Wulirocks

    Wulirocks

    Joined:
    Mar 18, 2013
    Posts:
    63
    No it is good to be cynical, it goes straight to the point... yes I realized that. Got I 16GB ram computer that runs (or loads) the game flawlessly... and in Webgl it is in pain. Now I realize I will go learning full time the unity profiler.(Deep profiler) Already discovered few things. I mean WebGL is a good school for learning how to go deep into optimizing.
    upload_2022-6-27_11-1-35.png

    Just as an example and it is just the beginning..

    https://forum.unity.com/threads/getting-tons-of-object-op_inequality-calls-in-deep-profiler.107776/
    In short, the equality operator to avoid at all cost in long loops... the idea of caching a maximum of variable to optimize a For loop was known to me.. I just did not care since the game was running fine, but in WebGL or on a
    2010 mobile phone like you say, it becomes critical...
     

    Attached Files:

    CodeSmile likes this.
  11. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,053
    Absolutely, on WebGL optimizing even trivial things like null checks or inlining Vector3 operations can make a big difference that would go unnoticed on desktop.
     
    Wulirocks likes this.