Skip to content

Commit e0b5faf

Browse files
committed
refactor: optimisation of read/write continuous sequences of bytes
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
1 parent 6f12d37 commit e0b5faf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

include/scale/detail/collections.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ namespace scale {
4343
std::vector<bool>>)
4444
{
4545
encode(as_compact(collection.size()), encoder);
46+
if constexpr (std::same_as<
47+
std::remove_cvref_t<decltype(*std::begin(collection))>,
48+
uint8_t>) {
49+
encoder.write(collection);
50+
return;
51+
}
4652
for (auto &&item : std::forward<decltype(collection)>(collection)) {
4753
encode(std::as_const(item), encoder);
4854
}
@@ -57,6 +63,12 @@ namespace scale {
5763
requires NoTagged<decltype(collection)>
5864
and (not DecomposableArray<decltype(collection)>)
5965
{
66+
if constexpr (std::same_as<
67+
std::remove_cvref_t<decltype(*std::begin(collection))>,
68+
uint8_t>) {
69+
encoder.write(collection);
70+
return;
71+
}
6072
for (auto &&item : std::forward<decltype(collection)>(collection)) {
6173
encode(std::as_const(item), encoder);
6274
}
@@ -111,6 +123,15 @@ namespace scale {
111123

112124
auto &mutable_collection = reinterpret_cast<MutableCollection &>(
113125
const_cast<Collection &>(collection));
126+
127+
if constexpr (std::same_as<std::remove_cvref_t<decltype(*std::begin(
128+
mutable_collection))>,
129+
uint8_t>) {
130+
if (decoder.isContinuousSource()) {
131+
decoder.read(mutable_collection);
132+
return;
133+
}
134+
}
114135
for (decltype(auto) item : mutable_collection) {
115136
decode(item, decoder);
116137
}
@@ -174,6 +195,14 @@ namespace scale {
174195
raise(DecodeError::TOO_MANY_ITEMS);
175196
}
176197

198+
if constexpr (std::same_as<std::remove_cvref_t<decltype(*std::begin(
199+
mutable_collection))>,
200+
uint8_t>) {
201+
if (decoder.isContinuousSource()) {
202+
decoder.read(mutable_collection);
203+
return;
204+
}
205+
}
177206
for (decltype(auto) item : mutable_collection) {
178207
decode(item, decoder);
179208
}

0 commit comments

Comments
 (0)