From c2516f02f6ec036647f65b10e80f556ab9a71aab Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 21 Jul 2020 20:27:42 +1000 Subject: [PATCH] Do not trigger RegisterModified hooks on register about to be restored This greatly reduces the amount of hooks triggered. --- src/commands.cc | 5 +++-- src/register_manager.cc | 6 ++++-- src/register_manager.hh | 5 +++++ src/utils.hh | 6 ++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index ca28d7ea..84ff8011 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1818,10 +1818,11 @@ void context_wrap(const ParametersParser& parser, Context& context, StringView d const auto& register_manager = RegisterManager::instance(); auto make_register_restorer = [&](char c) { - return on_scope_end([&, c, save=register_manager[c].save(context)] { + auto& reg = register_manager[c]; + return on_scope_end([&, c, save=reg.save(context), d=ScopedSetBool{reg.modified_hook_disabled()}] { try { - RegisterManager::instance()[c].restore(context, save); + reg.restore(context, save); } catch (runtime_error& err) { diff --git a/src/register_manager.cc b/src/register_manager.cc index 65f9a7ce..7bd1d7f9 100644 --- a/src/register_manager.cc +++ b/src/register_manager.cc @@ -11,7 +11,8 @@ namespace Kakoune void StaticRegister::set(Context& context, ConstArrayView values, bool) { m_content.assign(values.begin(), values.end()); - context.hooks().run_hook(Hook::RegisterModified, m_name, context); + if (not m_disable_modified_hook) + context.hooks().run_hook(Hook::RegisterModified, m_name, context); } ConstArrayView StaticRegister::get(const Context&) @@ -46,7 +47,8 @@ void HistoryRegister::set(Context& context, ConstArrayView values, bool if (current_size > size_limit) m_content.erase(m_content.begin(), m_content.begin() + (current_size - size_limit)); - context.hooks().run_hook(Hook::RegisterModified, m_name, context); + if (not m_disable_modified_hook) + context.hooks().run_hook(Hook::RegisterModified, m_name, context); } const String& HistoryRegister::get_main(const Context&, size_t) diff --git a/src/register_manager.hh b/src/register_manager.hh index 732d3449..75daf1ab 100644 --- a/src/register_manager.hh +++ b/src/register_manager.hh @@ -26,6 +26,11 @@ public: using RestoreInfo = Vector; RestoreInfo save(const Context& context) { return get(context) | gather(); } void restore(Context& context, const RestoreInfo& info) { set(context, info, true); } + + NestedBool& modified_hook_disabled() { return m_disable_modified_hook; } + +protected: + NestedBool m_disable_modified_hook; }; // static value register, which can be modified diff --git a/src/utils.hh b/src/utils.hh index 0979aebe..087c4ba8 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -107,6 +107,12 @@ struct ScopedSetBool m_nested_bool.set(); } + ScopedSetBool(ScopedSetBool&& other) + : m_nested_bool(other.m_nested_bool), m_condition(other.m_condition) + { + other.m_condition = false; + } + ~ScopedSetBool() { if (m_condition)