Commit c42dcaf0 authored by Dominik Charousset's avatar Dominik Charousset

Implement varbyte encoding on binary serializers

parent 5459caf1
......@@ -86,10 +86,17 @@ error binary_deserializer::end_object() {
}
error binary_deserializer::begin_sequence(size_t& list_size) {
auto s = static_cast<uint32_t>(list_size);
if (auto err = apply(s))
return err;
list_size = s;
// Use varbyte encoding to compress sequence size on the wire.
uint32_t x = 0;
int n = 0;
uint8_t low7;
do {
if (auto err = apply_impl(low7))
return err;
x |= static_cast<uint32_t>((low7 & 0x7F)) << (7 * n);
++n;
} while (low7 & 0x80);
list_size = x;
return none;
}
......
......@@ -71,8 +71,19 @@ error binary_serializer::end_object() {
}
error binary_serializer::begin_sequence(size_t& list_size) {
auto s = static_cast<uint32_t>(list_size);
return apply(s);
// Use varbyte encoding to compress sequence size on the wire.
// For 64-bit values, the encoded representation cannot get larger than 10
// bytes. A scratch space of 16 bytes suffices as upper bound.
uint8_t buf[16];
auto i = buf;
auto x = static_cast<uint32_t>(list_size);
while (x > 0x7f) {
*i++ = (static_cast<uint8_t>(x) & 0x7f) | 0x80;
x >>= 7;
}
*i++ = static_cast<uint8_t>(x) & 0x7f;
apply_raw(static_cast<size_t>(i - buf), buf);
return none;
}
error binary_serializer::end_sequence() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment