perf: avoid allocation on lean_trace use in interpreter (#12036)

After #12001, it was no longer true that `lean_trace(name(...), ...)`
would only perform the name allocation if no trace option was set. This
PR instead avoids the allocation in any case by avoiding this pattern.
This commit is contained in:
Sebastian Ullrich
2026-01-18 20:33:48 +01:00
committed by GitHub
parent d8fb702d73
commit 30e23eae2b
3 changed files with 18 additions and 11 deletions

View File

@@ -275,9 +275,9 @@ jobs:
// * `bv_` sometimes times out calling into cadical even though we should be using
// the standard compile flags for it.
// * `grind_guide` always times out.
// * `pkg/` tests sometimes time out (likely even hang), related to Lake CI
// * `pkg/|lake/` tests sometimes time out (likely even hang), related to Lake CI
// failures?
"CTEST_OPTIONS": "-E 'StackOverflow|reverse-ffi|interactive|async_select_channel|9366|run/bv_|grind_guide|pkg/'"
"CTEST_OPTIONS": "-E 'StackOverflow|reverse-ffi|interactive|async_select_channel|9366|run/bv_|grind_guide|pkg/|lake/'"
},
{
"name": "macOS",

View File

@@ -202,6 +202,8 @@ extern "C" float lean_float32_of_nat(lean_obj_arg a);
static string_ref * g_boxed_mangled_suffix = nullptr;
static name * g_interpreter_prefer_native = nullptr;
DEBUG_CODE(static name * g_interpreter_step = nullptr;)
DEBUG_CODE(static name * g_interpreter_call = nullptr;)
// constants (lacking native declarations) initialized by `lean_run_init`
// We can assume this variable is never written to and read from in parallel; see `enableInitializersExecution`.
@@ -638,7 +640,7 @@ private:
// make reference reassignable...
std::reference_wrapper<fn_body const> b(b0);
while (true) {
DEBUG_CODE(lean_trace(name({"interpreter", "step"}),
DEBUG_CODE(lean_trace(*g_interpreter_step,
tout() << std::string(m_call_stack.size(), ' ') << format_fn_body_head(b) << "\n";);)
switch (fn_body_tag(b)) {
case fn_body_kind::VDecl: { // variable declaration
@@ -668,7 +670,7 @@ private:
// NOTE: `var` must be called *after* `eval_expr` because the stack may get resized and invalidate
// the pointer
var(fn_body_vdecl_var(b)) = v;
DEBUG_CODE(lean_trace(name({"interpreter", "step"}),
DEBUG_CODE(lean_trace(*g_interpreter_step,
tout() << std::string(m_call_stack.size(), ' ') << "=> x_";
tout() << fn_body_vdecl_var(b).get_small_value() << " = ";
print_value(tout(), var(fn_body_vdecl_var(b)), fn_body_vdecl_type(b));
@@ -793,7 +795,7 @@ private:
// specify argument base pointer explicitly because we've usually already pushed some function arguments
void push_frame(decl const & d, size_t arg_bp) {
DEBUG_CODE({
lean_trace(name({"interpreter", "call"}),
lean_trace(*g_interpreter_call,
tout() << std::string(m_call_stack.size(), ' ')
<< decl_fun_id(d);
for (size_t i = arg_bp; i < m_arg_stack.size(); i++) {
@@ -809,7 +811,7 @@ private:
m_jp_stack.resize(get_frame().m_jp_bp);
m_call_stack.pop_back();
DEBUG_CODE({
lean_trace(name({"interpreter", "call"}),
lean_trace(*g_interpreter_call,
tout() << std::string(m_call_stack.size(), ' ')
<< "=> ";
print_value(tout(), r, t);
@@ -1216,9 +1218,10 @@ void initialize_ir_interpreter() {
ir::g_init_globals = new name_hash_map<object *>();
register_bool_option(*ir::g_interpreter_prefer_native, LEAN_DEFAULT_INTERPRETER_PREFER_NATIVE, "(interpreter) whether to use precompiled code where available");
DEBUG_CODE({
register_trace_class({"interpreter"});
register_trace_class({"interpreter", "call"});
register_trace_class({"interpreter", "step"});
ir::g_interpreter_call = new name({"interpreter", "call"});
register_trace_class(*ir::g_interpreter_call);
ir::g_interpreter_step = new name({"interpreter", "step"});
register_trace_class(*ir::g_interpreter_step);
});
ir::g_native_symbol_cache = new name_hash_map<ir::native_symbol_cache_entry>();
ir::g_native_symbol_cache_mutex = new std::shared_timed_mutex();
@@ -1227,6 +1230,10 @@ void initialize_ir_interpreter() {
void finalize_ir_interpreter() {
delete ir::g_native_symbol_cache_mutex;
delete ir::g_native_symbol_cache;
DEBUG_CODE({
delete ir::g_interpreter_call;
delete ir::g_interpreter_step;
});
delete ir::g_init_globals;
delete ir::g_interpreter_prefer_native;
delete ir::g_boxed_mangled_suffix;

View File

@@ -61,8 +61,8 @@ extern "C" LEAN_EXPORT obj_res lean_internal_get_default_options(obj_arg) {
return get_default_options().steal();
}
extern "C" LEAN_EXPORT obj_res lean_options_get_empty();
options::options(): object_ref(lean_options_get_empty()) {}
extern "C" LEAN_EXPORT obj_res lean_options_get_empty(obj_arg u);
options::options(): object_ref(lean_options_get_empty(box(0))) {}
extern "C" LEAN_EXPORT bool lean_options_get_bool(obj_arg opts, obj_arg n, bool default_value);
bool options::get_bool(name const & n, bool default_value) const {