Skip to content

Commit 096eddf

Browse files
awesomeklinggmta
authored andcommitted
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.
1 parent 1c45930 commit 096eddf

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

AK/Span.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class Span : public Detail::Span<T> {
125125
public:
126126
using Detail::Span<T>::Span;
127127

128+
using ElementType = T;
129+
128130
constexpr Span() = default;
129131

130132
[[nodiscard]] ALWAYS_INLINE constexpr T const* data() const { return this->m_values; }

Libraries/LibIPC/Encoder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ template<>
139139
ErrorOr<void> encode(Encoder&, URL::BlobURLEntry::MediaSource const&);
140140

141141
template<Concepts::Span T>
142+
requires(!IsArithmetic<typename T::ElementType>)
142143
ErrorOr<void> encode(Encoder& encoder, T const& span)
143144
{
144145
TRY(encoder.encode_size(span.size()));
@@ -149,6 +150,18 @@ ErrorOr<void> encode(Encoder& encoder, T const& span)
149150
return {};
150151
}
151152

153+
template<Concepts::Span T>
154+
requires(IsArithmetic<typename T::ElementType>)
155+
ErrorOr<void> encode(Encoder& encoder, T const& span)
156+
{
157+
TRY(encoder.encode_size(span.size()));
158+
159+
VERIFY(!Checked<size_t>::multiplication_would_overflow(span.size(), sizeof(typename T::ElementType)));
160+
TRY(encoder.append(reinterpret_cast<u8 const*>(span.data()), span.size() * sizeof(typename T::ElementType)));
161+
162+
return {};
163+
}
164+
152165
template<typename T, size_t N>
153166
ErrorOr<void> encode(Encoder& encoder, Array<T, N> const& array)
154167
{

0 commit comments

Comments
 (0)