jeffcampbellmakesgames / Entitas-Redux

An entity-component framework for Unity with code generation and visual debugging
MIT License
102 stars 13 forks source link

[BUG] `ContextObserverBehaviour.Update` is very slow #69

Closed JesseTG closed 2 years ago

JesseTG commented 2 years ago

Describe the bug

I just finished porting my game to Entitas Redux. It went better than expected! However, my game runs considerably slower. Running the profiler tells me that the cause is ContextObserverBehavior.Update.

Unity Version:

Unity 2021.3.1f1

To Reproduce

Run any scene that uses ContextObserverBehaviour and make sure to enable profiling.

Expected behavior

ContextObserverBehavior.Update should not be a bottleneck.

Screenshots

Screenshot 2022-05-11 213734

Additional context

Looking at this code...

https://github.com/jeffcampbellmakesgames/Entitas-Redux/blob/090414b6bd3eb62d5da294bfdbc88f4f3da1e8b8/Unity/Assets/JCMG/EntitasRedux/Scripts/VisualDebugging/ContextObserver/ContextObserverBehaviour.cs#L43-L53

I suspect that the problem is the extremely frequent call to _contextObserver.ToString(), because it coincides with a consistent 0.7 MB allocation each frame. The actual method looks reasonable enough, but it's being called way too much.

Actually, now I'm not so sure. It turns out vanilla Entitas' equivalents are implemented the exact same way, but I didn't have this problem with them.

No, wait, I think I was right the first time. Deep Profile mode says so:

Screenshot 2022-05-11 224159

JesseTG commented 2 years ago

Ah! It turns out I'm leaking ContextObservers. I noticed that the number of them is steadily increasing. The problem is actually with my own code. Whoops, my bad. Sorry for the confusion.