Search Unity

Component structure and Jobs efficiency

Discussion in 'Entity Component System' started by rsodre, Apr 9, 2019.

  1. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    Does additional interfaces, constructors and methods in my component break the Job efficiency?
    Supposing I have a component like this...

    Code (CSharp):
    1.     public struct ParameterHash : IComponentData, IEquatable<ParameterHash>
    2.     {
    3.         public int Hash;
    4.         public ParameterHash(string addr)
    5.         {
    6.             Hash = Hasher.MakeHash(addr);
    7.         }
    8.         // IEquatable
    9.         public bool Equals(ParameterHash other)
    10.         {
    11.             return Hash == other.Hash;
    12.         }
    13.         public override int GetHashCode()
    14.         {
    15.             return Hash;
    16.         }
    17.     }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    This isn't for ECS right? Just jobs?
     
  3. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    No as far as you use something like that in the caller:
    Code (CSharp):
    1. bool SomeCaller<T>(T lhs, T rhs) where T : struct, IEquatable<ParameterHash>, IComponentData {
    2.      return lhs.Equals(rhs);
    3. }
    Unity also use additional Interfaces in the Entity struct...
     
  4. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    I'm implementing the whole package.
    ECS + Jobs + Burst.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
  6. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    328
    I personally don't see anything wrong with equatable or compators on compdata, IF their comparisons are rigid. IE, a time comparator indicating befote/after relations.
     
  7. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    The compiler is not complaining, and the system is working well.
    What I'm worried is that all the additional methods would make the job system operation less efficient.

    Say, will this return more than the actual Hash field, or be slower because the component has other methods?

    Code (CSharp):
    1. var hashes = _hashedGroup.GetComponentDataArray<ParameterHash>();
     
  8. Guerro323

    Guerro323

    Joined:
    Sep 2, 2014
    Posts:
    25
    Antypodish likes this.
  9. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
  10. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    Blittable is no longer a required constraint (only Unmanaged is)
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    O that is interesting. Such near to impossible track all up to date changes ;)

    I am aware is not complaining. What I try to point at, is the philosophy of ECS and its IComponentData.
    It feels to me, it starts looks ugly in proposed form.

    I am just not convinced, OP approach is desired ECS style, even if it works.
     
  12. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    Yes, there's no need to add all this to the component data.
    Still thinking the old way...
     
  13. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Nothing at all wrong with that as it stands. Although FYI check out math.hash in Unity.Mathematics, and unless testing shows it's an issue I would just calculate the hash in GetHashCode(). Caching it is kind of a code smell, what if it involved values that changed... Leads to hard to track down bugs.

    What might possibly be an issue is what you are using IEquatable for. I have components that are also keys into hash maps so what's good or bad here depends on the larger context.
     
  14. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    I think there's a lot of confusion between the philosophy and practicality when defining the line that separates logic and data in ECS. If one were to assume that logic == methods, then chunks and NativeLists and float3s and everything else would also have to be method-less. I don't know about you but that sounds like a great way to make code buggy with a bunch of helper functions that aren't type-checked. The two practical questions to ask for a method on a piece of data in ECS are:
    1. Does the method only depend on the internal data and the passed in arguments?
    2. Can the method's functionality be batch-optimized by an external method in the future?
    If the answers to both of those questions are "yes", then it is probably fine. Question 1 means that you don't want to store a pointer or reference in an ICD and then try to access those external objects or structs from a method inside the ICD. Question 2 means you should be extra cautious about making any of your fields private.

    And of course, your data pieces will be small, which limits how many useful methods can be made for them. Generally useful methods that make the code cleaner are Constructors, operators (especially comparison operators), comparison and equating interface implementations, properties (unit conversion and value clamping are good examples, also see LocalToWorld for another example), hash functions, casting to other data types, I think you get the idea.
     
    snacktime likes this.
  15. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    From ComponentData documentation...

    ComponentData in Unity (also known as acomponentin standard ECS terms) is a struct that contains only the instance data for an entity. ComponentData cannot contain methods.​
     
  16. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I think they were just trying to make a point, but doing it rather poorly. Providing different views of the data or answering questions that solely derive from the component data, I see no reason to force a level of indirection by separating the two. But it doesn't mean doing so is bad either, it's all about your larger design and context.

    I mean what are the alternatives? If you have all the logic in a job, what if you need to use the logic in multiple places? So what then have static classes that encapsulate the logic that you pass components to? That's basically pure indirection.

    Practical design should win the day here. Do what works best in context.
     
  17. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    While I understand that Unity is trying to help people make the transition from OOP to DOD, I disagree with that wording. Because what happens if the data inside an ICD has methods? All the math types do. Is there any difference between the ICD having methods versus the data inside of it? If there is, is there anything stopping us from wrapping all our data and methods in a struct and putting that struct inside of our ICD? It effectively becomes the same thing, just with another type name to worry about.

    In practicality, Burst does not care (it just inlines everything anyways) nor does the ECS API. So if putting some utility methods inside an ICD keeps your code cleaner and that's what you want to do, I say "Go for it!"

    The ECS architecture makes it very difficult to write yourself into a corner. So unless your definition of "utility" includes the definition of the universe, you probably won't have any issues. There's a place for methods on ICDs, there's a place for methods in static classes, and there's a place for methods in systems. Draw the lines where they make the most sense to you and your team.

    I personally go by the following:
    ICDs : Convenience snippets that the IDE makes obvious and saves a bunch of typing
    Static classes: Complex utilities and complicated math
    Systems and jobs: Game Logic

    Shoot! Was just about to post when snacktime beat me to it saying pretty much the same thing!
     
  18. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    157
    can you elaborate on this?
     
  19. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    timmehhhhhhh likes this.