Skip to content

Commit 33c9122

Browse files
authored
chore(bb): lazy logging (#17164)
While working at C++ simulation optimization (ms granularity) I realized that logging started to take a good chunk of time. This is especially true now that builds use asserts (not `NDEBUG`) by default and there is no "release" mode (and nobody will be using it). The change in this PR makes the formatting of a log lazy for `debug` and `vinfo`.
1 parent 8e963fd commit 33c9122

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

barretenberg/cpp/src/barretenberg/common/bb_bench.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void print_separator(std::ostream& os, bool thick = true)
159159
{
160160
const char* line = thick ? "═══════════════════════════════════════════════════════════════════════════════════════"
161161
"═════════════════════"
162-
: "───────────────────────────────────────────────────────────────────────────--------────"
162+
: "───────────────────────────────────────────────────────────────────────────────────────"
163163
"─────────────────────";
164164
os << Colors::BOLD << Colors::CYAN << line << Colors::RESET << "\n";
165165
}

barretenberg/cpp/src/barretenberg/common/log.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "barretenberg/env/logstr.hpp"
33
#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp"
44
#include <algorithm>
5+
#include <functional>
56
#include <sstream>
67
#include <string>
78
#include <vector>
@@ -55,28 +56,33 @@ template <typename... Args> std::string benchmark_format(Args... args)
5556
}
5657

5758
extern bool debug_logging;
59+
// In release mode (e.g., NDEBUG is defined), we don't compile debug logs.
5860
#ifndef NDEBUG
59-
template <typename... Args> inline void debug(Args... args)
61+
#define debug(...) debug_([&]() { return format(__VA_ARGS__); })
62+
#else
63+
#define debug(...) (void)0
64+
#endif
65+
66+
// We take a function so that evaluation is lazy.
67+
inline void debug_(std::function<std::string()> func)
6068
{
61-
// NDEBUG is used to turn off asserts, so we want this flag to prevent debug log spamming.
6269
if (debug_logging) {
63-
logstr(format(args...).c_str());
70+
logstr(func().c_str());
6471
}
6572
}
66-
#else
67-
template <typename... Args> inline void debug(Args... /*unused*/) {}
68-
#endif
6973

7074
template <typename... Args> inline void info(Args... args)
7175
{
7276
logstr(format(args...).c_str());
7377
}
7478

79+
#define vinfo(...) vinfo_([&]() { return format(__VA_ARGS__); })
80+
7581
extern bool verbose_logging;
76-
template <typename... Args> inline void vinfo(Args... args)
82+
inline void vinfo_(std::function<std::string()> func)
7783
{
7884
if (verbose_logging) {
79-
info(args...);
85+
info(func());
8086
}
8187
}
8288

0 commit comments

Comments
 (0)