Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated Code Optimization #780

Closed
CCludts opened this issue Sep 2, 2018 · 3 comments
Closed

Generated Code Optimization #780

CCludts opened this issue Sep 2, 2018 · 3 comments
Milestone

Comments

@CCludts
Copy link

CCludts commented Sep 2, 2018

Hi,

Currently Entitas' ComponentEntityGenerator is using CreateComponent for AddComponent/RemoveComponent.
This means (heavy) use of Generics which carries a small but non-trivial performance penality. I dont have any easy-to-share benchmarks, but it improved starting-performance slightly for our specific case.

        // current entitas
        var component = CreateComponent<${ComponentType}>(index);
        // suggested optimization
        var componentPool = GetComponentPool(index);
        var component = componentPool.Count > 0 ? (${ComponentType})componentPool.Pop() : new ${ComponentType}();

Related is use of Activator.CreateInstance for creation of entities. We've replaced this with a function passed as the last argument for Context constructor. The context caches this function and uses it instead of the generic version.

        readonly Func<TEntity> _createEntity;

        public Context(int totalComponents, int startCreationIndex, ContextInfo contextInfo, Func<IEntity, IAERC> aercFactory, Func<TEntity> createEntity) {
            //--- snip
            _createEntity = createEntity;  //  previous: (TEntity)Activator.CreateInstance(typeof(TEntity))
            //--- snip
        }
    public GameContext()
        : base(
            GameComponentsLookup.TotalComponents,
            0,
            new Entitas.ContextInfo(
                "Game",
                GameComponentsLookup.componentNames,
                GameComponentsLookup.componentTypes
            ),
            (entity) =>

#if (ENTITAS_FAST_AND_UNSAFE)
                new Entitas.UnsafeAERC()
#else
                new Entitas.SafeAERC(entity)
#endif
            ,
            () => new GameEntity()
        ) {

Cheers

@sschmid
Copy link
Owner

sschmid commented Sep 2, 2018

Hi, thanks for the suggestion. I will try to find time to create a performance test, to see see the benefits
👍

@sschmid sschmid added this to the Entitas 1.8.3 milestone Oct 5, 2018
@sschmid
Copy link
Owner

sschmid commented Oct 24, 2018

Interesting, I remember a long time ago, when I added component pooling, I was explicitly testing
CreateComponent
vs
CreateComponent

and the latter was faster. Tested again now and it's the other way around. I will update the generated code!

@sschmid
Copy link
Owner

sschmid commented Oct 24, 2018

Passing in the an entity factory method speed things up by a factor of 2x, very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants