Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is ScheduleParallel slower than Schedule in this case?

Discussion in 'Entity Component System' started by Greenwar, Nov 8, 2020.

  1. Greenwar

    Greenwar

    Joined:
    Oct 11, 2014
    Posts:
    54

    Quality turned out bad, but the one on the left takes 2.00ms vs 1.22ms.
    hashmap is a NativeMultiHashMap<int, Entity>(50000, Allocator.Persistent);


    What am I missing?
     
    lndcobra likes this.
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    because NativeHashMap parallel writer using interlock. And the generated random number is only 0,1,2,3,4.
    So most of the time parallel job is waiting for another job to release the lock.
     
    lndcobra likes this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    you waste more time scheduling worker threads than the task itself. I'm pretty sure in this case if the number of entities is small, calling a bursted Run() will be the faster option.
     
  4. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    that should not cause 1 ms of difference, it should be about 0.1ms at most.
     
    Opeth001 likes this.
  5. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    as you said
    if he passes a simple writer instead of a parallelWriter and uses a run() or a schedule instead of a scheduleParallel it will be faster.
     
  6. Greenwar

    Greenwar

    Joined:
    Oct 11, 2014
    Posts:
    54
    Oh okay, I wasn't aware that it interlocked based on the key. That's unfortunate.
     
    CPlusSharp22 likes this.