#Reducing object allocations in an event handler function

1 messages · Page 1 of 1 (latest)

clever dome
#

In my unity application I have some event handling for events received by the machine through the osc protocol while scenes are going

I'm noticing a bit of ui stuttering and after profiling I found that there's a lot of garbage collection activity in one of my event handler functions (see the image)

The function in question is below. All i'm doing is deserializing the event string into an object and then passing the object along. It's not great that i'm creating a new object every single time but i'm not sure how to rewrite this so that i only have a fixed number of object instances that i'm repopulating.

Any help would be appreciated!

graceful locust
#

One option would be to use structs instead of objects. This is appropriate for reasonably small packets of data

#

If you can guarantee that no lingering references will remain to the data objects, you can also just re-use a single instance each time

#

Unity can do this with collision callbacks

clever dome
#

i just spent sometime spinning up an object pool for each of the object types but that hasn't seemed to help things much based on the profiler

graceful locust
#

are you releasing objects back into the pool once you're done with them?

#

pooling is dicey with an event system like this: whoever consumes the events could decide to keep a reference to the data object

clever dome
#

i thought the pool handled that?

graceful locust
#

no, the pool's job is to:

  • give you an instance of an object when you ask for it
  • store a set of objects that have been returned to the pool
#

you have to tell the pool that you are done with an object

clever dome
#

In cases like with the first type, I'd need to wait until the event finishes (ie when raweegdatareceived ends) before i can release it right?

#

i couldn't release the object before then

#

so yeah that does get a bit weird

clever dome
graceful locust
#

structs are not allocated on the heap, so they cannot possibly result in garbage being allocated

#

unless, of course, they got boxed when casted into an interface type (or just object)

clever dome
#

All of my stuff is in a struct now

#

but still

cunning pivot
#

you might want to turn on deep profiling or put in some markers so you can see where exactly it's allocating, for example do you know if FromJson is expected to allocate?

graceful locust
#

That is something to investigate, yeah.

glacial raft
#

I cant tell what types are used from the example but managed types like string will still allocate.
Allocation could also come from parsing JSON as well as the json string.

#

If 1.4k allocations happen just for ReceiveDataPackets then id guess its the json parsing

graceful locust
#

especially if it's an older library that doesn't use Span to avoid creating tons of tiny strings

glacial raft
#

❤️ stackalloc Span<>

graceful locust
#

you can also use Span to take a slice of a string without having to allocate a new string (very cool)

clever dome
#

I gave a build to the other stakeholder and it seems to be working a lot better after using structs and a few other optimizations to reduce the amount of new object allocations in other parts of the code

#

thanks yall

pine trellis