Optimize memory re-use by using one buffer pool for all players.#265
Merged
Conversation
hajimehoshi
reviewed
Aug 4, 2025
9316a6c to
3b42af1
Compare
hajimehoshi
reviewed
Aug 4, 2025
3b42af1 to
3cef64c
Compare
hajimehoshi
reviewed
Aug 5, 2025
| p.closeImpl() | ||
| } | ||
|
|
||
| func (p *playerImpl) disposeBuf() { |
Contributor
Author
There was a problem hiding this comment.
👍 I went with returnBufferToPool and renamed the other method getBufferFromPool
3cef64c to
51a583e
Compare
Contributor
Author
|
Pushed again |
This way different player instances can re-use memory buffers. This helps keeping the allocation rate low and reduces gc. Sharing is fine under the assumption that after a short while all buffers are at least as large as we currently need them to be. If we get a buffer from the pool that is smaller than the current required buffer size, we'll alloc a bigger buffer and return that one into the pool. Next time, we'll probably get a buffer of the correct size. We now also share the p.buf buffer between players. If a player is stopped or has reached eof, the buffer is put back into the pool. In total, this change helps to reduce the allocation rate (and in turn garbage pressure) when playing a lot of short lived sound effects, and it does not hinder long running playback.
51a583e to
3899c4d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This way different player instances can re-use memory buffers. This helps keeping the allocation rate low and reduces gc.
Sharing is fine under the assumption that after a short while all buffers are at least as large as we currently need them to be. If we get a buffer from the pool that is smaller than the current required buffer size, we'll alloc a bigger buffer and return that one into the pool. Next time, we'll probably get a buffer of the correct size.
We now also share the p.buf buffer between players. If a player is stopped or has reached eof, the buffer is put back into the pool.
In total, this change helps to reduce the allocation rate (and in turn garbage pressure) when playing a lot of short lived sound effects, and it does not hinder long running playback.