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

Using query.IsEmpty to reduce scheduling cost?

Discussion in 'Entity Component System' started by Timboc, Mar 20, 2021.

  1. Timboc

    Timboc

    Joined:
    Jun 22, 2015
    Posts:
    234
    Given a chain of jobs within a system:
    Code (CSharp):
    1. systemState.Dependency = someIJC1.ScheduleParallel(query1, systemState.Dependency);
    2. systemState.Dependency = someIJC2.ScheduleParallel(query2, systemState.Dependency);
    3. systemState.Dependency = someIJC3.ScheduleParallel(query3, systemState.Dependency);
    4. systemState.Dependency = someIJC4.ScheduleParallel(query4, systemState.Dependency);
    I have a large system with a chain of 20 such jobs and I was investigating what I could do about the main thread cost (~0.4ms in a build for just the scheduling overhead).
    I found that wrapping them each with a
    if(!query.IsEmptyIgnoreFilter)
    was hugely helpful.
    E.g.
    Code (CSharp):
    1. if(!query1.IsEmptyIgnoreFilter) systemState.Dependency = someIJC1.ScheduleParallel(query1, systemState.Dependency);
    2. if(!query2.IsEmptyIgnoreFilter) systemState.Dependency = someIJC2.ScheduleParallel(query2, systemState.Dependency);
    3. if(!query3.IsEmptyIgnoreFilter) systemState.Dependency = someIJC3.ScheduleParallel(query3, systemState.Dependency);
    4. if(!query4.IsEmptyIgnoreFilter) systemState.Dependency = someIJC4.ScheduleParallel(query4, systemState.Dependency);
    I was quite surprised by the difference this made. Is this an optimisation that could be built-in to Schedule/ScheduleParallel or am I somehow shooting myself in the foot by doing this?

    *My test was in a fully bursted ISystemBase (hence IJC not batch entity) but I believe this applies to SystemBase too
    **I also tried JobHandle.CombineDependencies(NativeArray<>) - but perhaps as a lot of my jobs depend on the jobhandle of the previous - this was no better. Behaviour was also still the same.
     
    Last edited: Mar 20, 2021