log,optimization: use original log string when no suspicious chars found

> build/src/bench/bench_bitcoin -filter='LogEscapeMessage.+' --min-time=10000

> C compiler ............................ AppleClang 16.0.0.16000026

|               ns/op |                op/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|              106.54 |        9,386,391.31 |    0.2% |     11.01 | `LogEscapeMessageNormal`
|              335.58 |        2,979,947.73 |    0.3% |     10.96 | `LogEscapeMessageSuspicious`
This commit is contained in:
Lőrinc 2025-02-21 11:35:40 +01:00
parent a445c688ab
commit 6f049e1230
2 changed files with 16 additions and 9 deletions

View File

@ -60,6 +60,7 @@ static void LogWithoutWriteToFile(benchmark::Bench& bench)
}
namespace BCLog {
bool IsSuspicious(const char ch) noexcept;
std::string LogEscapeMessage(std::string_view str);
}
@ -73,7 +74,7 @@ static void LogEscapeMessageNormal(benchmark::Bench& bench) {
bench.batch(normal_logs.size()).run([&] {
for (const auto& msg : normal_logs) {
const auto& res{BCLog::LogEscapeMessage(msg)};
const auto& res{std::ranges::none_of(msg, BCLog::IsSuspicious) ? msg : BCLog::LogEscapeMessage(msg)};
ankerl::nanobench::doNotOptimizeAway(res);
}
});
@ -90,7 +91,7 @@ static void LogEscapeMessageSuspicious(benchmark::Bench& bench) {
bench.batch(suspicious_logs.size()).run([&] {
for (const auto& msg : suspicious_logs) {
const auto& res{BCLog::LogEscapeMessage(msg)};
const auto& res{std::ranges::none_of(msg, BCLog::IsSuspicious) ? msg : BCLog::LogEscapeMessage(msg)};
ankerl::nanobench::doNotOptimizeAway(res);
}
});

View File

@ -315,6 +315,11 @@ std::string BCLog::Logger::LogTimestampStr(SystemClock::time_point now, std::chr
}
namespace BCLog {
bool IsSuspicious(const char ch) noexcept
{
const auto uch{static_cast<uint8_t>(ch)};
return (uch < ' ' && uch != '\n') || uch == '\x7f';
}
/** Belts and suspenders: make sure outgoing log messages don't contain
* potentially suspicious characters, such as terminal control codes.
*
@ -322,14 +327,15 @@ namespace BCLog {
* It escapes instead of removes them to still allow for troubleshooting
* issues where they accidentally end up in strings.
*/
std::string LogEscapeMessage(std::string_view str) {
std::string LogEscapeMessage(std::string_view str)
{
std::string ret;
for (char ch_in : str) {
uint8_t ch = (uint8_t)ch_in;
if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
ret += ch_in;
} else {
ret.reserve(3 + str.size()); // Assume at least one suspicious character
for (auto ch : str) {
if (IsSuspicious(ch)) {
ret += strprintf("\\x%02x", ch);
} else {
ret += ch;
}
}
return ret;
@ -392,7 +398,7 @@ void BCLog::Logger::LogPrintStr(std::string_view str, std::string_view logging_f
void BCLog::Logger::LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
std::string str_prefixed = LogEscapeMessage(str);
std::string str_prefixed{std::ranges::none_of(str, IsSuspicious) ? str : LogEscapeMessage(str)};
if (m_buffering) {
{