LibIPC: Encode spans of trivial arithmetic types more efficiently

For stuff like Span<u8>, we should obviously grow the message buffer
once, and then copy all the bytes in one go.
This commit is contained in:
Andreas Kling 2025-12-01 10:37:58 +01:00 committed by Jelle Raaijmakers
parent 1c45930767
commit 096eddfd5a
Notes: github-actions[bot] 2025-12-01 14:14:18 +00:00
2 changed files with 15 additions and 0 deletions

View file

@ -125,6 +125,8 @@ class Span : public Detail::Span<T> {
public:
using Detail::Span<T>::Span;
using ElementType = T;
constexpr Span() = default;
[[nodiscard]] ALWAYS_INLINE constexpr T const* data() const { return this->m_values; }

View file

@ -139,6 +139,7 @@ template<>
ErrorOr<void> encode(Encoder&, URL::BlobURLEntry::MediaSource const&);
template<Concepts::Span T>
requires(!IsArithmetic<typename T::ElementType>)
ErrorOr<void> encode(Encoder& encoder, T const& span)
{
TRY(encoder.encode_size(span.size()));
@ -149,6 +150,18 @@ ErrorOr<void> encode(Encoder& encoder, T const& span)
return {};
}
template<Concepts::Span T>
requires(IsArithmetic<typename T::ElementType>)
ErrorOr<void> encode(Encoder& encoder, T const& span)
{
TRY(encoder.encode_size(span.size()));
VERIFY(!Checked<size_t>::multiplication_would_overflow(span.size(), sizeof(typename T::ElementType)));
TRY(encoder.append(reinterpret_cast<u8 const*>(span.data()), span.size() * sizeof(typename T::ElementType)));
return {};
}
template<typename T, size_t N>
ErrorOr<void> encode(Encoder& encoder, Array<T, N> const& array)
{