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

Setting value for FloatVariable is causing GC allocations and slower execution #154

Open
SharkWithLasers opened this issue Aug 19, 2021 · 4 comments

Comments

@SharkWithLasers
Copy link

In one of my MonoBehaviours, I am updating a FloatVariable every Update.

However, I am noticing that this is causing the Update for the function to be much slower than if I had just set a float.

With the line
myFloatVariable.Value = myFloat, the Update function takes 0.54ms to complete and has a bunch of GC allocations:
image

Setting only the float itself is significantly less time:
image

Anyone know why this might be the case?

Thanks!

@chulini
Copy link

chulini commented Aug 19, 2021

It's because every time you assign a FloatVariable, the variable iterates over all listeners listening to that variable assignation and invoke an event in case it should. That's why it takes much more time to assign a FloatVariable than a float.

@SharkWithLasers
Copy link
Author

Ahh, that makes sense. I didn't expect FloatVariable to inherit from GameEventBase. I'm not sure if it's wise to update a FloatVariable every frame of an update loop.

I also noticed that the GC allocations happen because "enable debug" is on in Preferences. Turning off "enable debug" gets rid of most of the allocations, and gave comparable performance to using FloatVariable with debug on.

@chulini
Copy link

chulini commented Aug 23, 2021

I had one time the bad idea of setting the player's input every frame on a couple of FloatVariable and, as I added more and more listeners for those variables, everything started gradually to becoming very slow.

My advise would be not to use SO when you need something to be updated every frame.

@Monsoonexe
Copy link

The GC allocations are probably coming from GameEventBase's StackTrace feature. These allocations only happen in the Editor.

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

No branches or pull requests

3 participants