From b636b1905822272bdde10ebe5d997396751892bf Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 26 Aug 2016 12:28:36 +0200 Subject: 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. --- stream/cache.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/stream/cache.c b/stream/cache.c index 3aad35d95d..85b78f9038 100644 --- a/stream/cache.c +++ b/stream/cache.c @@ -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; - - int64_t file_size = stream_get_size(stream); - if (file_size >= 0) - cache_size = MPMIN(cache_size, file_size); + s->stream_size = stream_get_size(stream); - 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); -- cgit v1.2.3