From 409f11341f3c681d83ee3cb2de2fa16ea8cce601 Mon Sep 17 00:00:00 2001 From: thexai <58434170+thexai@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:43:45 +0200 Subject: [PATCH 1/2] [FileCache] Reduce CServiceBroker calls to obtain advancedsettings --- xbmc/filesystem/FileCache.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/xbmc/filesystem/FileCache.cpp b/xbmc/filesystem/FileCache.cpp index 26b5c978c21e8..3263f2294d25f 100644 --- a/xbmc/filesystem/FileCache.cpp +++ b/xbmc/filesystem/FileCache.cpp @@ -111,6 +111,12 @@ bool CFileCache::Open(const CURL& url) return false; } + const auto advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings(); + if (!advancedSettings) + return false; + + const unsigned int cacheMemSize = advancedSettings->m_cacheMemSize; + m_source.IoControl(IOCTRL_SET_CACHE, this); bool retry = false; @@ -120,9 +126,8 @@ bool CFileCache::Open(const CURL& url) m_seekPossible = m_source.IoControl(IOCTRL_SEEK_POSSIBLE, NULL); // Determine the best chunk size we can use - m_chunkSize = CFile::DetermineChunkSize( - m_source.GetChunkSize(), - CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_cacheChunkSize); + m_chunkSize = + CFile::DetermineChunkSize(m_source.GetChunkSize(), advancedSettings->m_cacheChunkSize); CLog::Log(LOGDEBUG, "CFileCache::{} - <{}> source chunk size is {}, setting cache chunk size to {}", __FUNCTION__, m_sourcePath, m_source.GetChunkSize(), m_chunkSize); @@ -131,7 +136,7 @@ bool CFileCache::Open(const CURL& url) if (!m_pCache) { - if (CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_cacheMemSize == 0) + if (cacheMemSize == 0) { // Use cache on disk m_pCache = std::unique_ptr(new CSimpleFileCache()); // C++14 - Replace with std::make_unique @@ -140,7 +145,7 @@ bool CFileCache::Open(const CURL& url) else { size_t cacheSize; - if (m_fileSize > 0 && m_fileSize < CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_cacheMemSize && !(m_flags & READ_AUDIO_VIDEO)) + if (m_fileSize > 0 && m_fileSize < cacheMemSize && !(m_flags & READ_AUDIO_VIDEO)) { // Cap cache size by filesize, but not for audio/video files as those may grow. // We don't need to take into account READ_MULTI_STREAM here as that's only used for audio/video @@ -152,7 +157,7 @@ bool CFileCache::Open(const CURL& url) } else { - cacheSize = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_cacheMemSize; + cacheSize = cacheMemSize; // NOTE: READ_MULTI_STREAM is only used with READ_AUDIO_VIDEO if (m_flags & READ_MULTI_STREAM) @@ -227,6 +232,12 @@ void CFileCache::Process() return; } + const auto advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings(); + if (!advancedSettings) + return; + + const float readFactor = advancedSettings->m_cacheReadFactor; + CWriteRate limiter; CWriteRate average; @@ -279,13 +290,13 @@ void CFileCache::Process() while (m_writeRate) { - if (m_writePos - m_readPos < m_writeRate * CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_cacheReadFactor) + if (m_writePos - m_readPos < m_writeRate * readFactor) { limiter.Reset(m_writePos); break; } - if (limiter.Rate(m_writePos) < m_writeRate * CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_cacheReadFactor) + if (limiter.Rate(m_writePos) < m_writeRate * readFactor) break; if (m_seekEvent.Wait(100ms)) From 5f37f5b9df8b711650c18268cb97657c64294ec7 Mon Sep 17 00:00:00 2001 From: thexai <58434170+thexai@users.noreply.github.com> Date: Sat, 23 Sep 2023 11:05:28 +0200 Subject: [PATCH 2/2] [FileCache] Replace new with std::make_unique --- xbmc/filesystem/FileCache.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xbmc/filesystem/FileCache.cpp b/xbmc/filesystem/FileCache.cpp index 3263f2294d25f..5d9598931aa1c 100644 --- a/xbmc/filesystem/FileCache.cpp +++ b/xbmc/filesystem/FileCache.cpp @@ -139,7 +139,7 @@ bool CFileCache::Open(const CURL& url) if (cacheMemSize == 0) { // Use cache on disk - m_pCache = std::unique_ptr(new CSimpleFileCache()); // C++14 - Replace with std::make_unique + m_pCache = std::make_unique(); m_forwardCacheSize = 0; } else @@ -181,14 +181,14 @@ bool CFileCache::Open(const CURL& url) const size_t back = cacheSize / 4; const size_t front = cacheSize - back; - m_pCache = std::unique_ptr(new CCircularCache(front, back)); // C++14 - Replace with std::make_unique + m_pCache = std::make_unique(front, back); m_forwardCacheSize = front; } if (m_flags & READ_MULTI_STREAM) { // If READ_MULTI_STREAM flag is set: Double buffering is required - m_pCache = std::unique_ptr(new CDoubleCache(m_pCache.release())); // C++14 - Replace with std::make_unique + m_pCache = std::make_unique(m_pCache.release()); } }