Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve decode_varint[struct_pb] #278

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 62 additions & 56 deletions iguana/pb_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,63 +106,69 @@ IGUANA_INLINE uint64_t decode_varint(T& data, size_t& pos) {
const int8_t* p = begin;
uint64_t val = 0;

// end is always greater than or equal to begin, so this subtraction is safe
if (size_t(end - begin) >= 10) { // fast path
int64_t b;
do {
b = *p++;
val = (b & 0x7f);
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 7;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 14;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 21;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 28;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 35;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 42;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 49;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 56;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x01) << 63;
if (b >= 0) {
break;
}
throw std::invalid_argument("Invalid varint value: too many bytes.");
} while (false);
if ((static_cast<uint64_t>(*p) & 0x80) == 0) {
pos = 1;
return static_cast<uint64_t>(*p);
}

// end is always greater than or equal to begin, so this subtraction is safe
if (size_t(end - begin) >= 10)
IGUANA_LIKELY { // fast path
int64_t b;
do {
b = *p++;
val = (b & 0x7f);
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 7;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 14;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 21;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 28;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 35;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 42;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 49;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x7f) << 56;
if (b >= 0) {
break;
}
b = *p++;
val |= (b & 0x01) << 63;
if (b >= 0) {
break;
}
throw std::invalid_argument("Invalid varint value: too many bytes.");
} while (false);
}
else {
int shift = 0;
while (p != end && *p < 0) {
Expand Down
2 changes: 1 addition & 1 deletion iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ struct field_t {
using value_type = typename member_traits<T>::value_type;
using sub_type = ElementType;
constexpr field_t() = default;
constexpr field_t(T member, uint32_t number, frozen::string name)
constexpr field_t(T member, uint32_t number, frozen::string name = "")
: member_ptr(member), field_name(name), field_no(number) {}

T member_ptr;
Expand Down
Loading