From 0a05558a8ca32b9ebd508be355b7718641fc5d34 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 29 Dec 2020 00:39:23 +0100 Subject: [PATCH] Make the persistent buffer thread local Now that we allow ad hoc socket spawning for the audio sockets. --- src/common/communication/common.h | 14 ++++++-------- src/common/communication/vst3.h | 32 ++++++++++++++----------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/common/communication/common.h b/src/common/communication/common.h index 9829c691..ef9a34be 100644 --- a/src/common/communication/common.h +++ b/src/common/communication/common.h @@ -491,14 +491,12 @@ class AdHocSocketHandler { if (acceptor) { acceptor->accept(socket); - if constexpr (ad_hoc_sockets) { - // As mentioned in `acceptor's` docstring, this acceptor will be - // recreated in `receive_multi()` on another context, and - // potentially on the other side of the connection in the case - // where we're handling `vst_host_callback` VST2 events - acceptor.reset(); - boost::filesystem::remove(endpoint.path()); - } + // As mentioned in `acceptor's` docstring, this acceptor will be + // recreated in `receive_multi()` on another context, and + // potentially on the other side of the connection in the case + // where we're handling `vst_host_callback` VST2 events + acceptor.reset(); + boost::filesystem::remove(endpoint.path()); } else { socket.connect(endpoint); } diff --git a/src/common/communication/vst3.h b/src/common/communication/vst3.h index a80d090d..e306c333 100644 --- a/src/common/communication/vst3.h +++ b/src/common/communication/vst3.h @@ -189,34 +189,30 @@ class Vst3MessageHandler : public AdHocSocketHandler { * request. See the definition of `F` for more information. * * @tparam F A function type in the form of `T::Response(T)` for every `T` - * in `Request`. This way we can directly deserialize into a `T::Response` - * on the side that called `receive_into(T, T::Response&)`. - * @tparam keep_buffers Whether processing buffers should be kept around and - * reused. This is used to minimize allocations in the audio processing - * loop. This can only be used when `ad_hoc_sockets` is set to false, - * since we can of course only reuse buffers within a single thread. These - * buffers will also never shrink, but that should not be an issue here. + * in `Request`. This way we can directly deserialize into a `T::Response` + * on the side that called `receive_into(T, T::Response&)`. + * @tparam persistent_buffers Whether processing buffers should be kept + * around and reused. This is used to minimize allocations in the audio + * processing loop. These buffers will also never shrink, but that should + * not be an issue with the `IAudioProcessor` and `IComponent` functions. + * Saving and loading state is handled on the main sockets. * * @relates Vst3MessageHandler::send_event */ - template + template void receive_messages(std::optional> logging, F callback) { - std::vector persistent_buffer{}; - if constexpr (keep_buffers) { - static_assert(!ad_hoc_sockets, - "Buffers can only be reused when ad-hoc socket " - "spawning has been disabled."); - } + thread_local std::vector persistent_buffer{}; // Reading, processing, and writing back the response for the requests // we receive works in the same way regardless of which socket we're // using const auto process_message = [&](boost::asio::local::stream_protocol::socket& socket) { - auto request = keep_buffers ? read_object( - socket, persistent_buffer) - : read_object(socket); + auto request = + persistent_buffers + ? read_object(socket, persistent_buffer) + : read_object(socket); // See the comment in `receive_into()` for more information bool should_log_response = false; @@ -241,7 +237,7 @@ class Vst3MessageHandler : public AdHocSocketHandler { logger.log_response(!is_host_vst, response); } - if constexpr (keep_buffers) { + if constexpr (persistent_buffers) { write_object(socket, response, persistent_buffer); } else { write_object(socket, response);