From a83640d2da2854cadc625c3d1fe6e82a0a76b388 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 21 Feb 2025 16:49:01 -0600 Subject: [PATCH] Improved ring buffer read-ahead --- CHANGELOG.md | 1 + .../src/file_manager/ring_buffer_base.cpp | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6ed38f9..251e0ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * Fixed setting `HostConfig.ApiUser` via `set_value_by_name` * Fixed setting `HostConfig.Path` via `set_value_by_name` * Fixed setting `HostConfig.Protocol` via `set_value_by_name` +* Improved ring buffer read-ahead * Integrated `renterd` version 2.0.0 * Prefer using local cache file when opening files * Refactored `app_config` unit tests diff --git a/repertory/librepertory/src/file_manager/ring_buffer_base.cpp b/repertory/librepertory/src/file_manager/ring_buffer_base.cpp index e818ae76..9771fcd8 100644 --- a/repertory/librepertory/src/file_manager/ring_buffer_base.cpp +++ b/repertory/librepertory/src/file_manager/ring_buffer_base.cpp @@ -98,8 +98,8 @@ auto ring_buffer_base::close() -> bool { return res; } -auto ring_buffer_base::download_chunk(std::size_t chunk, - bool skip_active) -> api_error { +auto ring_buffer_base::download_chunk(std::size_t chunk, bool skip_active) + -> api_error { REPERTORY_USES_FUNCTION_NAME(); unique_mutex_lock chunk_lock(chunk_mtx_); @@ -225,6 +225,8 @@ auto ring_buffer_base::read(std::size_t read_size, std::uint64_t read_offset, forward(chunk - ring_pos_); } else if (chunk < ring_pos_) { reverse(ring_pos_ - chunk); + } else { + ring_pos_ = chunk; } res = download_chunk(chunk, false); @@ -264,15 +266,26 @@ void ring_buffer_base::reader_thread() { REPERTORY_USES_FUNCTION_NAME(); unique_mutex_lock chunk_lock(chunk_mtx_); - auto next_chunk{ring_pos_}; + auto last_pos = ring_pos_; + auto next_chunk = ring_pos_; chunk_notify_.notify_all(); chunk_lock.unlock(); while (not get_stop_requested()) { chunk_lock.lock(); - next_chunk = next_chunk + 1U > ring_end_ ? ring_begin_ : next_chunk + 1U; - const auto check_and_wait = [this, &chunk_lock, &next_chunk]() { + if (last_pos == ring_pos_) { + ++next_chunk; + } else { + next_chunk = ring_pos_ + 1U; + last_pos = ring_pos_; + } + + if (next_chunk > ring_end_) { + next_chunk = ring_begin_; + } + + const auto check_and_wait = [this, &chunk_lock, &last_pos, &next_chunk]() { if (get_stop_requested()) { chunk_notify_.notify_all(); chunk_lock.unlock(); @@ -281,6 +294,7 @@ void ring_buffer_base::reader_thread() { if (get_read_state().all()) { chunk_notify_.wait(chunk_lock); + last_pos = ring_pos_; next_chunk = ring_pos_; }