-
Notifications
You must be signed in to change notification settings - Fork 149
Optimize memory re-use by using one buffer pool for all players. #265
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
Conversation
9316a6c to
3b42af1
Compare
3b42af1 to
3cef64c
Compare
internal/mux/mux.go
Outdated
| p.closeImpl() | ||
| } | ||
|
|
||
| func (p *playerImpl) disposeBuf() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
putBackBufferToPool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I went with returnBufferToPool and renamed the other method getBufferFromPool
3cef64c to
51a583e
Compare
|
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
hajimehoshi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
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.