From 5c6c405b9e7d88a2e0c4845098a8265887080e3d Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Thu, 28 Oct 2021 19:38:41 +0200 Subject: [PATCH] Cache if tracing is enabled Like the comment said: That var lookup was kind of expensive. So we simply use variable dispatch like we do for countless other things. --- src/env_dispatch.cpp | 7 +++++++ src/trace.cpp | 9 ++++++--- src/trace.h | 3 +++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/env_dispatch.cpp b/src/env_dispatch.cpp index f826078fd..4181c7e1a 100644 --- a/src/env_dispatch.cpp +++ b/src/env_dispatch.cpp @@ -53,6 +53,7 @@ #include "reader.h" #include "screen.h" #include "termsize.h" +#include "trace.h" #include "wutil.h" // IWYU pragma: keep #define DEFAULT_TERM1 "ansi" @@ -311,6 +312,10 @@ static void handle_read_limit_change(const environment_t &vars) { } } +static void handle_fish_trace(const environment_t &vars) { + trace_set_enabled(!vars.get(L"fish_trace").missing_or_empty()); +} + /// Populate the dispatch table used by `env_dispatch_var_change()` to efficiently call the /// appropriate function to handle a change to a variable. /// Note this returns a new-allocated value that we expect to leak. @@ -337,6 +342,7 @@ static std::unique_ptr create_dispatch_table() { var_dispatch_table->add(L"fish_history", handle_fish_history_change); var_dispatch_table->add(L"TZ", handle_tz_change); var_dispatch_table->add(L"fish_use_posix_spawn", handle_fish_use_posix_spawn_change); + var_dispatch_table->add(L"fish_trace", handle_fish_trace); // This std::move is required to avoid a build error on old versions of libc++ (#5801), // but it causes a different warning under newer versions of GCC (observed under GCC 9.3.0, @@ -359,6 +365,7 @@ static void run_inits(const environment_t &vars) { update_wait_on_escape_ms(vars); handle_read_limit_change(vars); handle_fish_use_posix_spawn_change(vars); + handle_fish_trace(vars); } /// Updates our idea of whether we support term256 and term24bit (see issue #10222). diff --git a/src/trace.cpp b/src/trace.cpp index 342696967..ef21bb571 100644 --- a/src/trace.cpp +++ b/src/trace.cpp @@ -6,13 +6,16 @@ #include "flog.h" #include "parser.h" -static const wcstring VAR_fish_trace = L"fish_trace"; +static bool do_trace = false; + +void trace_set_enabled(bool do_enable) { + do_trace = do_enable; +} bool trace_enabled(const parser_t &parser) { const auto &ld = parser.libdata(); if (ld.suppress_fish_trace) return false; - // TODO: this variable lookup is somewhat expensive, consider how to make this cheaper. - return !parser.vars().get(VAR_fish_trace).missing_or_empty(); + return do_trace; } /// Trace an "argv": a list of arguments where the first is the command. diff --git a/src/trace.h b/src/trace.h index b0ba0abb7..f5a0ff7e1 100644 --- a/src/trace.h +++ b/src/trace.h @@ -16,6 +16,9 @@ void trace_argv(const parser_t &parser, const wchar_t *command, const wcstring_l /// \return whether tracing is enabled. bool trace_enabled(const parser_t &parser); +/// Enable or disable tracing. +void trace_set_enabled(bool do_enable); + /// Convenience helper to trace a single string if tracing is enabled. void trace_if_enabled(const parser_t &parser, const wchar_t *command, const wcstring_list_t &argv = {});