Closed divillysausages closed 2 years ago
@endel ptal
@endel Please take a look at this! @divillysausages Do you know why its needed to call DispatchMessageQueue? Can i just avoid that call?
@angelhodar Do you know why its needed to call DispatchMessageQueue? Can i just avoid that call?
On WebGL it does nothing, so if you're only targeting that platform, you can just ignore it. On other platforms it's necessary to get notified of your messages on the main thread - if you don't call it, you'll never receive anything.
I guess you could modify your code to call OnMessage
directly in Receive
, but I really wouldn't recommend it
Thanks a lot for the suggestion @divillysausages, I've applied your change myself because of some recent changes in the Mutex/locking mechanism (Mutex wasn't working on Nintendo Switch)
As
DispatchMessageQueue()
is called every frame, the unnecessary memory allocations quickly add up and mean that you game gets hit with garbage collection where it's not necessary.There are 3 changes here:
1) If there's no messages in
m_MessageList
(most of the time), just return immediately - stops unnecessarily creating and copying an empty list 2) When creating the copy, we just directly passm_MessageList
. This means thatmessageListCopy
is the correct size, and we don't have to useAddRange()
(which allocates memory, if I remember correctly - but it definitely does if there's more messages that the default list size) 3) Replace theforeach
with a simplefor
loop, which doesn't allocate any memory