From a510d1748416ff29b1ed3cab92ac0ad943b6e590 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Fri, 23 Aug 2024 16:57:26 -0700 Subject: [PATCH] build-time: hide boost stacktrace in a .cc file Saves about 16s of CPU time. Not a lot but not nothing. Feels more like the principle of the thing. Change-Id: I0992d4024317c20d6985a7977d5649edfb9f46bb --- src/libutil/fmt.cc | 10 ++++++++++ src/libutil/fmt.hh | 12 +++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/libutil/fmt.cc b/src/libutil/fmt.cc index 400fb7ea0..bff5af020 100644 --- a/src/libutil/fmt.cc +++ b/src/libutil/fmt.cc @@ -1,4 +1,9 @@ #include "fmt.hh" // IWYU pragma: keep +// Darwin and FreeBSD stdenv do not define _GNU_SOURCE but do have _Unwind_Backtrace. +#if __APPLE__ || __FreeBSD__ +#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED +#endif +#include template class boost::basic_format; @@ -11,4 +16,9 @@ template HintFmt::HintFmt(const std::string &, const uint64_t &, const char * co HintFmt::HintFmt(const std::string & literal) : HintFmt("%s", Uncolored(literal)) {} +void printStackTrace() +{ + std::cerr << boost::stacktrace::stacktrace() << std::endl; +} + } diff --git a/src/libutil/fmt.hh b/src/libutil/fmt.hh index 7589e51e2..ee3e1e2e7 100644 --- a/src/libutil/fmt.hh +++ b/src/libutil/fmt.hh @@ -4,11 +4,6 @@ #include #include #include -// Darwin and FreeBSD stdenv do not define _GNU_SOURCE but do have _Unwind_Backtrace. -#if __APPLE__ || __FreeBSD__ -#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED -#endif -#include #include "ansicolor.hh" // Explicit instantiation in fmt.cc @@ -16,6 +11,9 @@ extern template class boost::basic_format; namespace nix { +/** Prints a C++ stack trace to stderr using boost stacktrace */ +void printStackTrace(); + /** * Values wrapped in this struct are printed in magenta. * @@ -176,14 +174,14 @@ public: std::cerr << "HintFmt received incorrect number of format args. Original format string: '"; std::cerr << format << "'; number of arguments: " << sizeof...(args) << "\n"; // And regardless of the coredump give me a damn stacktrace. - std::cerr << boost::stacktrace::stacktrace() << std::endl; + printStackTrace(); abort(); } } catch (boost::io::format_error & ex) { // Same thing, but for anything that happens in the member initializers. std::cerr << "HintFmt received incorrect format string. Original format string: '"; std::cerr << format << "'; number of arguments: " << sizeof...(args) << "\n"; - std::cerr << boost::stacktrace::stacktrace() << std::endl; + printStackTrace(); abort(); }