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

Remove unnecessary memory allocations #40

Closed
wants to merge 1 commit into from

Conversation

divillysausages
Copy link

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 pass m_MessageList. This means that messageListCopy is the correct size, and we don't have to use AddRange() (which allocates memory, if I remember correctly - but it definitely does if there's more messages that the default list size)
  3. Replace the foreach with a simple for loop, which doesn't allocate any memory

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 pass `m_MessageList`. This means that `messageListCopy` is the correct size, and we don't have to use `AddRange()` (which allocates memory, if I remember correctly - but it definitely does if there's more messages that the default list size)
3) Replace the `foreach` with a simple `for` loop, which doesn't allocate any memory
@johnou
Copy link

johnou commented May 4, 2021

@endel ptal

@angelhodar
Copy link

@endel Please take a look at this! @divillysausages Do you know why its needed to call DispatchMessageQueue? Can i just avoid that call?

@divillysausages
Copy link
Author

@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

@endel endel closed this in a15bc62 Feb 16, 2022
@endel
Copy link
Owner

endel commented Feb 16, 2022

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)

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

Successfully merging this pull request may close these issues.

None yet

4 participants