From e334becefbf221db19e8e4e4c550da036db3b61b Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 3 Mar 2020 00:13:33 -0800 Subject: [PATCH] Adopt debounce for highlighting and autosuggestions This prevents a thundering herd of threads for certain interactive scenarios. --- src/reader.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/reader.cpp b/src/reader.cpp index 10fee7c94..cfb45b61f 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -144,6 +144,20 @@ operation_context_t get_bg_context(const std::shared_ptr &env, return operation_context_t{nullptr, *env, std::move(cancel_checker)}; } +/// Get the debouncer for autosuggestions and background highlighting. +/// These are deliberately leaked to avoid shutdown dtor registration. +static debounce_t &debounce_autosuggestions() { + const long kAutosuggetTimeoutMs = 500; + static debounce_t *res = new debounce_t(kAutosuggetTimeoutMs); + return *res; +} + +static debounce_t &debounce_highlighting() { + const long kHighlightTimeoutMs = 500; + static debounce_t *res = new debounce_t(kHighlightTimeoutMs); + return *res; +} + bool edit_t::operator==(const edit_t &other) const { return cursor_position_before_edit == other.cursor_position_before_edit && offset == other.offset && length == other.length && old == other.old && @@ -1482,9 +1496,10 @@ void reader_data_t::update_autosuggestion() { auto performer = get_autosuggestion_performer(parser(), el->text(), el->position(), history); auto shared_this = this->shared_from_this(); - iothread_perform(performer, [shared_this](autosuggestion_result_t result) { - shared_this->autosuggest_completed(std::move(result)); - }); + debounce_autosuggestions().perform( + performer, [shared_this](autosuggestion_result_t result) { + shared_this->autosuggest_completed(std::move(result)); + }); } } @@ -2226,9 +2241,10 @@ void reader_data_t::super_highlight_me_plenty(int match_highlight_pos_adjust, bo } else { // Highlighting including I/O proceeds in the background. auto shared_this = this->shared_from_this(); - iothread_perform(highlight_performer, [shared_this](highlight_result_t result) { - shared_this->highlight_complete(std::move(result)); - }); + debounce_highlighting().perform(highlight_performer, + [shared_this](highlight_result_t result) { + shared_this->highlight_complete(std::move(result)); + }); } highlight_search();