From 0b0951fb890a81b5485b9a10a38715b597235a59 Mon Sep 17 00:00:00 2001 From: Luca Guerra Date: Thu, 23 Nov 2023 18:51:32 +0000 Subject: [PATCH] update(libsinsp): move exception to a separate function to facilitate inlining Signed-off-by: Luca Guerra --- userspace/libsinsp/event.cpp | 4 ++-- userspace/libsinsp/event.h | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/userspace/libsinsp/event.cpp b/userspace/libsinsp/event.cpp index 1b23b3a17d4..cebcd29ad27 100644 --- a/userspace/libsinsp/event.cpp +++ b/userspace/libsinsp/event.cpp @@ -2862,7 +2862,7 @@ std::optional> sinsp_evt::get_enter_evt_para return ret; } -std::string sinsp_evt_param::invalid_len_error(size_t requested_length) const +void sinsp_evt_param::throw_invalid_len_error(size_t requested_length) const { const struct ppm_param_info* parinfo = get_info(); @@ -2871,7 +2871,7 @@ std::string sinsp_evt_param::invalid_len_error(size_t requested_length) const << m_evt->get_num() << " of type " << m_evt->get_type() << " (" << m_evt->get_name() << "): expected length " << requested_length << ", found " << m_len; - return std::string(ss.str()); + throw sinsp_exception(ss.str()); } const struct ppm_param_info* sinsp_evt_param::get_info() const diff --git a/userspace/libsinsp/event.h b/userspace/libsinsp/event.h index a2db1b7ed47..4599f6b5cd6 100644 --- a/userspace/libsinsp/event.h +++ b/userspace/libsinsp/event.h @@ -122,7 +122,12 @@ class SINSP_PUBLIC sinsp_evt_param } const struct ppm_param_info* get_info() const; - std::string invalid_len_error(size_t requested_len) const; + + // Throws a sinsp_exception detailing why the requested_len is incorrect. + // This is only meant to be called by get_event_param_as. This way, this function will not be inlined + // while get_event_param_as will be inlined. + [[gnu::cold]] + void throw_invalid_len_error(size_t requested_len) const; }; /*! @@ -139,7 +144,7 @@ inline T get_event_param_as(const sinsp_evt_param& param) // By moving this error string building operation to a separate function // the compiler is more likely to inline this entire function. // This is important since get_param<> is called in the hot path. - throw sinsp_exception(param.invalid_len_error(sizeof(T))); + param.throw_invalid_len_error(sizeof(T)); } memcpy(&ret, param.m_val, sizeof(T)); @@ -161,7 +166,7 @@ inline std::string_view get_event_param_as(const sinsp_evt_par // By moving this error string building operation to a separate function // the compiler is more likely to inline this entire function. // This is important since get_param<> is called in the hot path. - throw sinsp_exception(param.invalid_len_error(string_len)); + param.throw_invalid_len_error(string_len); } return {param.m_val, string_len};