Rename ParameterInfoCache to be more general

This commit is contained in:
Robbert van der Helm
2021-05-06 18:16:33 +02:00
parent a9a7e3e711
commit 8f310ed89b
2 changed files with 43 additions and 50 deletions
+14 -17
View File
@@ -82,7 +82,9 @@ bool Vst3PluginProxyImpl::unregister_context_menu(size_t context_menu_id) {
void Vst3PluginProxyImpl::clear_caches() { void Vst3PluginProxyImpl::clear_caches() {
clear_bus_cache(); clear_bus_cache();
clear_parameter_cache();
std::lock_guard lock(function_result_cache_mutex);
function_result_cache = FunctionResultCache{};
} }
tresult PLUGIN_API Vst3PluginProxyImpl::setAudioPresentationLatencySamples( tresult PLUGIN_API Vst3PluginProxyImpl::setAudioPresentationLatencySamples(
@@ -552,26 +554,26 @@ int32 PLUGIN_API Vst3PluginProxyImpl::getParameterCount() {
YaEditController::GetParameterCount{.instance_id = instance_id()}; YaEditController::GetParameterCount{.instance_id = instance_id()};
{ {
std::lock_guard lock(parameter_info_cache_mutex); std::lock_guard lock(function_result_cache_mutex);
if (parameter_info_cache.parameter_count) { if (function_result_cache.parameter_count) {
const bool log_response = bridge.logger.log_request(true, request); const bool log_response = bridge.logger.log_request(true, request);
if (log_response) { if (log_response) {
bridge.logger.log_response( bridge.logger.log_response(
true, true,
YaEditController::GetParameterCount::Response( YaEditController::GetParameterCount::Response(
*parameter_info_cache.parameter_count), *function_result_cache.parameter_count),
true); true);
} }
return *parameter_info_cache.parameter_count; return *function_result_cache.parameter_count;
} }
} }
const int32 result = bridge.send_message(request); const int32 result = bridge.send_message(request);
{ {
std::lock_guard lock(parameter_info_cache_mutex); std::lock_guard lock(function_result_cache_mutex);
parameter_info_cache.parameter_count = result; function_result_cache.parameter_count = result;
} }
return result; return result;
@@ -584,9 +586,9 @@ tresult PLUGIN_API Vst3PluginProxyImpl::getParameterInfo(
.instance_id = instance_id(), .param_index = paramIndex}; .instance_id = instance_id(), .param_index = paramIndex};
{ {
std::lock_guard lock(parameter_info_cache_mutex); std::lock_guard lock(function_result_cache_mutex);
if (auto it = parameter_info_cache.parameter_info.find(paramIndex); if (auto it = function_result_cache.parameter_info.find(paramIndex);
it != parameter_info_cache.parameter_info.end()) { it != function_result_cache.parameter_info.end()) {
const bool log_response = bridge.logger.log_request(true, request); const bool log_response = bridge.logger.log_request(true, request);
if (log_response) { if (log_response) {
bridge.logger.log_response( bridge.logger.log_response(
@@ -607,8 +609,8 @@ tresult PLUGIN_API Vst3PluginProxyImpl::getParameterInfo(
info = response.info; info = response.info;
{ {
std::lock_guard lock(parameter_info_cache_mutex); std::lock_guard lock(function_result_cache_mutex);
parameter_info_cache.parameter_info[paramIndex] = response.info; function_result_cache.parameter_info[paramIndex] = response.info;
} }
return response.result; return response.result;
@@ -1286,8 +1288,3 @@ void Vst3PluginProxyImpl::clear_bus_cache() {
processing_bus_cache.emplace(); processing_bus_cache.emplace();
} }
} }
void Vst3PluginProxyImpl::clear_parameter_cache() {
std::lock_guard lock(parameter_info_cache_mutex);
parameter_info_cache = ParameterInfoCache{};
}
+29 -33
View File
@@ -62,17 +62,15 @@ class Vst3PluginProxyImpl : public Vst3PluginProxy {
bool unregister_context_menu(size_t context_menu_id); bool unregister_context_menu(size_t context_menu_id);
/** /**
* Clear the bus and parameter caches. We'll call this on * Clear our function call caches. We'll do this when the plugin calls
* `IComponentHandler::restartComponent`. These caching layers are necessary * `IComponentHandler::restartComponent()`. These caching layers are
* to get decent performance in REAPER as REAPER repeatedly calls these * necessary to get decent performance in certain hosts because they will
* functions many times per second, even though their values will never * call these functions repeatedly even when their values cannot change.
* change.
* *
* HACK: See the doc comment on this class for more information on these * See the bottom of this class for more information on what we're caching.
* caches
* *
* @see clear_bus_cache * @see clear_bus_cache
* @see clear_parameter_cache * @see function_result_cache
*/ */
void clear_caches(); void clear_caches();
@@ -301,7 +299,7 @@ class Vst3PluginProxyImpl : public Vst3PluginProxy {
// From `IXmlRepresentationController` // From `IXmlRepresentationController`
tresult PLUGIN_API tresult PLUGIN_API
getXmlRepresentationStream(Steinberg::Vst::RepresentationInfo& info /*in*/, getXmlRepresentationStream(Steinberg::Vst::RepresentationInfo& info /*in*/,
Steinberg::IBStream* stream /*out*/); Steinberg::IBStream* stream /*out*/) override;
/** /**
* The component handler the host passed to us during * The component handler the host passed to us during
@@ -419,17 +417,6 @@ class Vst3PluginProxyImpl : public Vst3PluginProxy {
*/ */
void clear_bus_cache(); void clear_bus_cache();
/**
* Clears the parameter information cache. Normally hosts only have to
* request this once, since the information never changes. REAPER however in
* some situations asks for this information four times per second. This
* extra back and forth can really add up once plugins start having
* thousands of parameters.
*
* @see parameter_info_cache
*/
void clear_parameter_cache();
/** /**
* If we have an active `IPlugView` instance, try to use the mutual * If we have an active `IPlugView` instance, try to use the mutual
* recursion mechanism so that callbacks made by the plugin can be handled * recursion mechanism so that callbacks made by the plugin can be handled
@@ -536,29 +523,38 @@ class Vst3PluginProxyImpl : public Vst3PluginProxy {
std::mutex processing_bus_cache_mutex; std::mutex processing_bus_cache_mutex;
/** /**
* A cache for `IEditController::getParameterCount()` and * A cache for several function calls that should be safe to cache since
* `IEditController::getParameterInfo()`. We'll memoize these function calls * their values shouldn't change at run time. We'll memoize these function
* until the plugin tells the host that parameter information has changed. * calls until the plugin tells the host that parameter information has
* changed.
* *
* @see parameter_cache * @see function_result_cache
*/ */
struct ParameterInfoCache { struct FunctionResultCache {
/**
* Memoizes `IEditController::getParameterCount()`.
*/
std::optional<int32> parameter_count; std::optional<int32> parameter_count;
/**
* Memoizes `IEditController::getParameterInfo()`.
*/
std::map<int32, Steinberg::Vst::ParameterInfo> parameter_info; std::map<int32, Steinberg::Vst::ParameterInfo> parameter_info;
}; };
/** /**
* A cache for the parameter count and infos. This used to be necessary * A cache for several frequently called functions that should not change
* because in some situations REAPER would query this information many times * values unless the plugin calls `IComponentHandler::restartComponent()`.
* times per second even though it cannot change unless the plugin tells the * This used to be necessary because in some situations REAPER would query
* host that it has. This issue has since been fixed, but we'll keep it in * this information many times times per second even though it cannot change
* because some other hosts also query this information more than once. * unless the plugin tells the host that it has. This issue has since been
* fixed, but we'll keep it in because some other hosts also query this
* information more than once.
* *
* The cache will be cleared when the plugin tells the host that some of its * The cache will be cleared when the plugin tells the host that some of its
* parameter values have changed. * parameter values have changed.
* *
* @see clear_parameter_cache * @see clear_caches
*/ */
ParameterInfoCache parameter_info_cache; FunctionResultCache function_result_cache;
std::mutex parameter_info_cache_mutex; std::mutex function_result_cache_mutex;
}; };