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.
This commit is contained in:
Fabian Homborg 2021-10-28 19:38:41 +02:00
parent e89bd95d58
commit 5c6c405b9e
3 changed files with 16 additions and 3 deletions

View file

@ -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<const var_dispatch_table_t> 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).

View file

@ -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.

View file

@ -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 = {});