cache: don't use a backbuffer if the cache is as large as the file

It's just wasted memory.

One corner case is when a file grows during playback, but this is rare
and usually happens on-disk only. The cache size was generally limited
before this change already, so no reason to care.

As an unrelated change, move the cache size info to the resize_cache()
function. There's really no reason not to do this, and it's slightly
more informative if the user changes the cache size at runtime.
This commit is contained in:
wm4 2016-08-26 12:28:36 +02:00
parent cb9b60ef19
commit b636b19058
1 changed files with 14 additions and 10 deletions

View File

@ -325,6 +325,14 @@ static int resize_cache(struct priv *s, int64_t size)
int64_t min_size = FILL_LIMIT * 2;
int64_t max_size = ((size_t)-1) / 8;
if (s->stream_size > 0) {
size = MPMIN(size, s->stream_size);
if (size >= s->stream_size) {
MP_VERBOSE(s, "no backbuffer needed\n");
s->back_size = 0;
}
}
int64_t buffer_size = MPCLAMP(size, min_size, max_size);
s->back_size = MPCLAMP(s->back_size, min_size, max_size);
buffer_size += s->back_size;
@ -373,6 +381,10 @@ static int resize_cache(struct priv *s, int64_t size)
if (s->seek_limit > s->buffer_size - FILL_LIMIT)
s->seek_limit = s->buffer_size - FILL_LIMIT;
MP_VERBOSE(s, "Cache size set to %lld KiB (%lld KiB backbuffer)\n",
(long long)(s->buffer_size / 1024),
(long long)(s->back_size / 1024));
assert(s->back_size < s->buffer_size);
return STREAM_OK;
@ -675,22 +687,14 @@ int stream_cache_init(stream_t *cache, stream_t *stream,
s->seek_limit = opts->seek_min * 1024ULL;
s->back_size = opts->back_buffer * 1024ULL;
int64_t cache_size = opts->size * 1024ULL;
s->stream_size = stream_get_size(stream);
int64_t file_size = stream_get_size(stream);
if (file_size >= 0)
cache_size = MPMIN(cache_size, file_size);
if (resize_cache(s, cache_size) != STREAM_OK) {
if (resize_cache(s, opts->size * 1024ULL) != STREAM_OK) {
MP_ERR(s, "Failed to allocate cache buffer.\n");
talloc_free(s);
return -1;
}
MP_VERBOSE(cache, "Cache size set to %lld KiB (%lld KiB backbuffer)\n",
(long long)(s->buffer_size / 1024),
(long long)(s->back_size / 1024));
pthread_mutex_init(&s->mutex, NULL);
pthread_cond_init(&s->wakeup, NULL);