Search Unity

GetSingleton and synch points

Discussion in 'Entity Component System' started by JooleanLogic, May 21, 2019.

  1. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Am I right in thinking that using GetSingleton and GetSingletonEntity in JobComponentSystem is going to cause a synch point?
    For example, I have something like this where I write to MyComp in the job via cdfe[cdfeEntity]
    Code (CSharp):
    1. new Job(){
    2.     cdfe = GetComponentDataFromEntity<MyComp>(false),
    3.     cdfeEntity = GetSingletonEntity<MyComp>()}
    instead of using GetSingletonEntity() here which would cause a synch point, I'd be better to cache the singleton entity (static ref.) and pass that into job?
     
    psuong likes this.
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yep, under hood it's call GetEntityQueryInternal which have AddReaderWriters which call CompleteDependenciesNoChecks and completes dependency chain
     
    psuong likes this.
  3. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    It waits for jobs that have write access to the singleton so it normally shouldn't be a problem

    Your GetComponentDataFromEntity on the other hand has write access and will lead to GetSigletonEntity calls from following systems to wait for this Job.
     
    Last edited: May 22, 2019
    eizenhorn likes this.
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yep important note! Forgot mention that
     
  5. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Thanks for the feedback.

    While I think I understand the dependency chain side of things, I don't get why GetSingletonEntity() would require waiting for anything as it can't be affected by anything can it?
    I'm not asking for read/write access to MyComp, just the Entity which shouldn't require even touching the MyComp data.
     
  6. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    It will only effect the system jobs that need are accessing it so if the owning system pushes changes to the singleton it's jobs have to complete first before the consuming system can safely read from it.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Cos of this
    upload_2019-5-22_13-38-57.png
    It's register entity query for readers\writers
    upload_2019-5-22_13-39-24.png
    Singleton gets entity by entity query from associated chunk
     
  8. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Yes, I guess you are right, GetSingletonEntity could be implemented without waiting for dependencies as it is not interested at the actual data only the archetype of the entity. Unfortunately it currently isn't implemented that way.

    So your intention is to spawn a Job that writes the singleton and you want to prevent the system itself from waiting for other writers, only the Job should have the dependencies on other readers and writers? Makes sense.

    Edit:
    All of this is wrong!
     
    Last edited: May 22, 2019
  9. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Thanks eizenhorn, but AddReadersWriters is only going to get hit on first call yes?
    GetEntityQueryInternal() is going to return from first for loop cos it's cached so it's not going to call CompleteDependencyInternal() every frame.

    For example all queries in OnCreate also set up readers/writers but that's just to set up the dependency chain for jobs yes? It doesn't cause the system itself to wait in OnUpdate() for any of those jobs to finish? Or does it?

    If you have
    SystemA.JobA<MyComp> write
    SystemB.JobB<MyComp> read
    SystemC

    My understanding is that JobB has to wait on JobA, but SystemB.OnUpdate() doesn't have to wait for JobA to finish. It just schedules JobB and then exits and then SystemC can run even if JobA hasn't finished yet.
    Maybe my understanding on this has been completely wrong and I need to rethink my life. :)
     
  10. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Ohhh, you are right, it should already work without waiting. GetSingletonEntity should never wait, only one time when the query is initially created in OnUpdate. Didn't you try it?
     
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yep it’s only first call if query not exist
     
  12. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Yes I'm using it but I just wasn't sure whether it caused a synch point or not. It's hard to tell from within my game but I was more referring to the general case so I can avoid any blocking code in my systems.
    I'm still a bit unsure what exactly happens though so I'll try create a test case.
     
  13. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    So after some tests, GetSingletonEntity does not cause a synch point but it does show up in Entity Debugger window as a RO dependency which I guess is just a system query dependency and not a job one.
     
  14. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    This is correct. It just means that the inputDeps JobHandle passed to your system contains all jobs spawned by systems which have write access to the component. It will not wait for this dependency in the system as long as you don't query the data.