Search Unity

Is lambda for managed components assured to produce no garbage?

Discussion in 'Entity Component System' started by davenirline, Mar 13, 2020.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I've run it through in the profiler with a captured local variable. It does indeed produce garbage.

    Code (CSharp):
    1. protected override void OnUpdate() {
    2.     int value = UnityEngine.Random.Range(1, 101);
    3.    
    4.     this.Entities.With(this.query).ForEach(delegate(SampleManaged sample, AnotherManaged another) {
    5.         sample.Value = value;
    6.     });
    7. }
    I've read somewhere that lambdas would be compiled to something that won't throw garbage but I'm not sure if it's applied to managed components.
     
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    This is the fix to remove the garbage but it's verbose:
    Code (CSharp):
    1. public class MultipleManagedComponentSystem : ComponentSystem {
    2.     private EntityQuery query;
    3.  
    4.     private readonly Processor processor = new Processor();
    5.     private EntityQueryBuilder.F_CC<SampleManaged, AnotherManaged> foreachDelegate;
    6.    
    7.     protected override void OnCreate() {
    8.         this.query = GetEntityQuery(typeof(SampleManaged), typeof(AnotherManaged));
    9.         this.foreachDelegate = this.processor.Execute;
    10.     }
    11.  
    12.     protected override void OnUpdate() {
    13.         int value = UnityEngine.Random.Range(1, 101);
    14.         this.processor.Init(value);
    15.         this.Entities.With(this.query).ForEach(this.foreachDelegate);
    16.     }
    17.    
    18.     private class Processor {
    19.         private int value;
    20.  
    21.         public void Init(int value) {
    22.             this.value = value;
    23.         }
    24.  
    25.         public void Execute(SampleManaged sample, AnotherManaged another) {
    26.             sample.Value = this.value;
    27.         }
    28.     }
    29. }
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    ComponentSystem lambdas don't use the codegen lambdas. Use SystemBase instead with WithoutBurst() and Run().
     
    davenirline likes this.
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,709
    Wait I was also under the impression that the lambda entity api creates no garbage
     
  5. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    Do you mean to use ComponentSystemBase? It doesn't have the EntityQueryBuilder property (Entities).
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    He mean SystemBase. It’s new compilation of better things from ComponentSystem and JobComponentSystem with better code generation for ForEach and automatic dependency handling for ForEach.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Does it make a difference if it's a standalone build? Unity does add a few checks in editor which can also generate garbage, but I don't know for this case. Just a thought.
     
  8. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I didn't know that. Will try it.

    Edit: Wait, does it mean that lambdas in JobComponentSystem produce garbage as well?
     
    Last edited: Mar 13, 2020
  9. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    No, JobComponentSystem is when they first introduced the codegen to prevent that. However at this point there is no reason to be using ComponentSystem or JobComponentSystem. Unity has said they will be removed eventually. SystemBase is the only thing we should be using.
     
    nicolasgramlich and NotaNaN like this.
  10. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,709
    Wait JobComponentSystem is going away. Can I get a link to catch up please.
     
  11. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    https://docs.unity3d.com/Packages/com.unity.entities@0.8/manual/ecs_systems.html

    From the docs:

    Important: The ComponentSystem and JobComponentSystem classes, along with IJobForEach, are being phased out of the DOTS API, but have not been officially deprecated yet. Use SystemBase and Entities.ForEach instead.
     
    NotaNaN likes this.