Skip to content

Commit

Permalink
update(libsinsp): move exception to a separate function to facilitate…
Browse files Browse the repository at this point in the history
… inlining

Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra committed Nov 24, 2023
1 parent bdb3dff commit 0b0951f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions userspace/libsinsp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,7 @@ std::optional<std::reference_wrapper<std::string>> 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();

Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/*!
Expand All @@ -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));
Expand All @@ -161,7 +166,7 @@ inline std::string_view get_event_param_as<std::string_view>(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};
Expand Down

0 comments on commit 0b0951f

Please sign in to comment.