packet: change memory estimation heuristics

Determining how much memory something uses is very hard, especially in
high level code (yes we call code using malloc high level). There's no
way to get an exact amount, especially since the malloc arena is shared
with the entire process anyway. So the demuxer packet cache tries to get
by with an estimate using a number of rough guesses.

It seems this wasn't quite good. In some ways, it was too optimistic, in
others it seemed to account for too much data. Try to get it closer to
what malloc and ta probably do. In particular, talloc adds some
singificant overhead (using talloc for mass-data was a mistake, and it's
even my fault). The result appears to match better with measured memory
usage. This is still extremely dependent on malloc implementation and so
on.

The effect is that you may need to adjust the demuxer cache limits to
cache as much data as it did before this commit. In any case, seems to
be better for me.
This commit is contained in:
wm4 2019-07-06 23:52:31 +02:00
parent 3cea180cc0
commit 11027e99f2
1 changed files with 5 additions and 2 deletions

View File

@ -164,7 +164,7 @@ struct demux_packet *demux_copy_packet(struct demux_packet *dp)
return new;
}
#define ROUND_ALLOC(s) MP_ALIGN_UP(s, 64)
#define ROUND_ALLOC(s) MP_ALIGN_UP((s), 16)
// Attempt to estimate the total memory consumption of the given packet.
// This is important if we store thousands of packets and not to exceed
@ -177,12 +177,15 @@ struct demux_packet *demux_copy_packet(struct demux_packet *dp)
size_t demux_packet_estimate_total_size(struct demux_packet *dp)
{
size_t size = ROUND_ALLOC(sizeof(struct demux_packet));
size += 8 * sizeof(void *); // ta overhead
size += 10 * sizeof(void *); // additional estimate for ta_ext_header
if (dp->avpacket) {
assert(!dp->is_cached);
size += ROUND_ALLOC(dp->len);
size += ROUND_ALLOC(sizeof(AVPacket));
size += 8 * sizeof(void *); // ta overhead
size += ROUND_ALLOC(sizeof(AVBufferRef));
size += 64; // upper bound estimate on sizeof(AVBuffer)
size += ROUND_ALLOC(64); // upper bound estimate on sizeof(AVBuffer)
size += ROUND_ALLOC(dp->avpacket->side_data_elems *
sizeof(dp->avpacket->side_data[0]));
for (int n = 0; n < dp->avpacket->side_data_elems; n++)