Slightly improve memory usage by packing some structs used in the die cache

This commit is contained in:
Jeremy Rifkin 2025-02-17 22:18:15 -06:00
parent e22300b36d
commit aed47df73e
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
5 changed files with 27 additions and 8 deletions

View File

@ -223,6 +223,10 @@ target_compile_features(
target_compile_definitions(${target_name} PRIVATE NOMINMAX) target_compile_definitions(${target_name} PRIVATE NOMINMAX)
if(HAS_ATTRIBUTE_PACKED)
target_compile_definitions(${target_name} PRIVATE HAS_ATTRIBUTE_PACKED)
endif()
if(NOT CPPTRACE_STD_FORMAT) if(NOT CPPTRACE_STD_FORMAT)
target_compile_definitions(${target_name} PUBLIC CPPTRACE_NO_STD_FORMAT) target_compile_definitions(${target_name} PUBLIC CPPTRACE_NO_STD_FORMAT)
endif() endif()

View File

@ -15,6 +15,10 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_support(HAS_CXXABI has_cxxabi.cpp "" "" "") check_support(HAS_CXXABI has_cxxabi.cpp "" "" "")
endif() endif()
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_support(HAS_ATTRIBUTE_PACKED has_attribute_packed.cpp "" "" "")
endif()
if(NOT WIN32) if(NOT WIN32)
check_support(HAS_UNWIND has_unwind.cpp "" "" "") check_support(HAS_UNWIND has_unwind.cpp "" "" "")
check_support(HAS_EXECINFO has_execinfo.cpp "" "" "") check_support(HAS_EXECINFO has_execinfo.cpp "" "" "")

View File

@ -0,0 +1,6 @@
struct __attribute__((packed)) foo {
int i;
double d;
};
int main() {}

View File

@ -21,6 +21,7 @@
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <functional> #include <functional>
#include <limits>
#include <memory> #include <memory>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
@ -44,19 +45,15 @@ namespace libdwarf {
class die_cache { class die_cache {
public: public:
struct die_handle { struct die_handle {
std::size_t die_index; std::uint32_t die_index;
}; };
private: private:
struct basic_range_entry { struct PACKED basic_range_entry {
die_handle die; die_handle die;
Dwarf_Addr low; Dwarf_Addr low;
Dwarf_Addr high; Dwarf_Addr high;
}; };
static_assert( struct PACKED annotated_range_entry {
sizeof(basic_range_entry) == 3 * sizeof(void*),
"Expected basic_range_entry to be smaller (this is memory critical)"
);
struct annotated_range_entry {
die_handle die; die_handle die;
Dwarf_Addr low; Dwarf_Addr low;
Dwarf_Addr high; Dwarf_Addr high;
@ -72,7 +69,8 @@ namespace libdwarf {
public: public:
die_handle add_die(die_object&& die) { die_handle add_die(die_object&& die) {
dies.push_back(std::move(die)); dies.push_back(std::move(die));
return die_handle{dies.size() - 1}; VERIFY(dies.size() < std::numeric_limits<std::uint32_t>::max());
return die_handle{static_cast<std::uint32_t>(dies.size() - 1)};
} }
template<typename Void = void> template<typename Void = void>
auto insert(die_handle die, Dwarf_Addr low, Dwarf_Addr high) auto insert(die_handle die, Dwarf_Addr low, Dwarf_Addr high)

View File

@ -30,6 +30,13 @@
#define MSVC_CDECL #define MSVC_CDECL
#endif #endif
// support is pretty good https://godbolt.org/z/djTqv7WMY, checked in cmake during config
#ifdef HAS_ATTRIBUTE_PACKED
#define PACKED __attribute__((packed))
#else
#define PACKED
#endif
namespace cpptrace { namespace cpptrace {
namespace detail { namespace detail {
static const stacktrace_frame null_frame { static const stacktrace_frame null_frame {