Skip to content

Conversation

@oliverbestmann
Copy link
Contributor

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.

p.closeImpl()
}

func (p *playerImpl) disposeBuf() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

putBackBufferToPool?

Copy link
Contributor Author

@oliverbestmann oliverbestmann Aug 5, 2025

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

@oliverbestmann
Copy link
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.
Copy link
Member

@hajimehoshi hajimehoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@hajimehoshi hajimehoshi merged commit 33432a7 into ebitengine:main Aug 5, 2025
6 checks passed
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.

2 participants