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

demux: reclaim demux_packets to reduce memory allocator pressure #14955

Merged
merged 2 commits into from
Feb 5, 2025

Conversation

kasper93
Copy link
Contributor

@kasper93 kasper93 commented Sep 30, 2024

This update introduces a demux_packet_pool, allowing for the reuse of previously allocated packets when needed.

sizeof(AVPacket) is not a part of the lavc public ABI, which prevents us to allocate memory in larger blocks. However, we can substantially decrease the amount of alloc/free operations during playback by reusing both mpv's demux_packet and AVPacket.

This adjustment addresses the root cause of issue #12076, which, although resolved upstream, did not fully tackle the persistent problem of allocating small blocks of aligned memory. This issue largely stems from the FFmpeg design of the AVPacket API. After this change memory will no longer be allocated once cache limits is reached.

The demux_packet_pool is shared as a global pool of packets for a given MPContext.

This change significantly speeds up the demuxer deinitialization, benefiting file switching scenarios, especially when a large demuxer cache is used.

See: #12294
See: #12563

I decided to open a PR without the "memory leak" part, which was the main discussion point in #12566. After refining the patches, it is no longer the main objective of this change. Therefore, #12566 remains focused on the "fast quit" option, and here we introduce the demux pool. Hopefully, we will be able to iterate on these patches in this way.

Copy link

github-actions bot commented Sep 30, 2024

Download the artifacts for this pull request:

Windows
macOS

demux/packet_pool.h Outdated Show resolved Hide resolved
demux/demux_mkv.c Outdated Show resolved Hide resolved
@sfan5 sfan5 self-requested a review September 30, 2024 18:28
@kasper93 kasper93 force-pushed the demux_pool branch 6 times, most recently from 8830632 to 48006ce Compare October 1, 2024 02:23
Copy link
Contributor

@CounterPillow CounterPillow left a comment

Choose a reason for hiding this comment

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

Some concurrency concerns but they might be wholly theoretical

demux/packet_pool.c Show resolved Hide resolved
demux/packet_pool.c Outdated Show resolved Hide resolved
demux/packet_pool.h Outdated Show resolved Hide resolved
filters/filter.c Show resolved Hide resolved
demux/packet_pool.h Outdated Show resolved Hide resolved
demux/packet_pool.c Outdated Show resolved Hide resolved
demux/packet.c Outdated Show resolved Hide resolved
Copy link
Member

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

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

lgtm. will test later.

demux/packet_pool.c Outdated Show resolved Hide resolved
This update introduces a demux_packet_pool, allowing for the reuse of
previously allocated packets when needed.

sizeof(AVPacket) is not a part of the lavc public ABI, which prevents us
to allocate memory in larger blocks. However, we can substantially
decrease the amount of alloc/free operations during playback by reusing
both mpv's demux_packet and AVPacket.

This adjustment addresses the root cause of issue mpv-player#12076, which,
although resolved upstream, did not fully tackle the persistent problem
of allocating small blocks of aligned memory. This issue largely stems
from the FFmpeg design of the AVPacket API. After this change memory
will no longer be allocated once cache limits is reached.

The demux_packet_pool is shared as a global pool of packets for a given
MPContext.

This change significantly speeds up the demuxer deinitialization,
benefiting file switching scenarios, especially when a large demuxer
cache is used.

See: mpv-player#12294
See: mpv-player#12563
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
For debugging without a pool, maybe.
Copy link
Member

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

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

works

@kasper93
Copy link
Contributor Author

kasper93 commented Feb 5, 2025

Hopefully it will be fine, we can always revert.

@kasper93 kasper93 merged commit de4004c into mpv-player:master Feb 5, 2025
27 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.

4 participants