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)
if(HAS_ATTRIBUTE_PACKED)
target_compile_definitions(${target_name} PRIVATE HAS_ATTRIBUTE_PACKED)
endif()
if(NOT CPPTRACE_STD_FORMAT)
target_compile_definitions(${target_name} PUBLIC CPPTRACE_NO_STD_FORMAT)
endif()

View File

@ -15,6 +15,10 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_support(HAS_CXXABI has_cxxabi.cpp "" "" "")
endif()
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_support(HAS_ATTRIBUTE_PACKED has_attribute_packed.cpp "" "" "")
endif()
if(NOT WIN32)
check_support(HAS_UNWIND has_unwind.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 <cstdio>
#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
@ -44,19 +45,15 @@ namespace libdwarf {
class die_cache {
public:
struct die_handle {
std::size_t die_index;
std::uint32_t die_index;
};
private:
struct basic_range_entry {
struct PACKED basic_range_entry {
die_handle die;
Dwarf_Addr low;
Dwarf_Addr high;
};
static_assert(
sizeof(basic_range_entry) == 3 * sizeof(void*),
"Expected basic_range_entry to be smaller (this is memory critical)"
);
struct annotated_range_entry {
struct PACKED annotated_range_entry {
die_handle die;
Dwarf_Addr low;
Dwarf_Addr high;
@ -72,7 +69,8 @@ namespace libdwarf {
public:
die_handle add_die(die_object&& 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>
auto insert(die_handle die, Dwarf_Addr low, Dwarf_Addr high)

View File

@ -30,6 +30,13 @@
#define MSVC_CDECL
#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 detail {
static const stacktrace_frame null_frame {