From 8608cd11e4600d3dc3bac093e2a863be9a721597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 3 Oct 2021 19:01:44 +0200 Subject: [PATCH] LibAudio: Optimize sample moves in FlacLoader As long as possible, entire decoded frame sample vectors are moved into the output vector, leading to up to 20% speedups by avoiding memmoves on take_first. --- Userland/Libraries/LibAudio/FlacLoader.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index d0d40d9ff4a..ff3774d04ba 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "AK/StdLibExtras.h" #include #include #include @@ -199,15 +200,25 @@ LoaderSamples FlacLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_i return Buffer::create_empty(); size_t samples_to_read = min(max_bytes_to_read_from_input, remaining_samples); + samples.ensure_capacity(samples_to_read); while (samples_to_read > 0) { if (!m_current_frame.has_value()) TRY(next_frame()); - samples.append(m_current_frame_data.take_first()); - if (m_current_frame_data.is_empty()) { + // Do a full vector extend if possible + if (m_current_frame_data.size() <= samples_to_read) { + samples_to_read -= m_current_frame_data.size(); + samples.extend(move(m_current_frame_data)); + m_current_frame_data.clear(); m_current_frame.clear(); + } else { + samples.unchecked_append(m_current_frame_data.data(), samples_to_read); + m_current_frame_data.remove(0, samples_to_read); + if (m_current_frame_data.size() == 0) { + m_current_frame.clear(); + } + samples_to_read = 0; } - --samples_to_read; } m_loaded_samples += samples.size();