Skip to content

Commit

Permalink
minor codec improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed Aug 21, 2024
1 parent e40d714 commit e7136ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
13 changes: 7 additions & 6 deletions include/quill/core/Codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ struct Codec
// Copy the length first and then the actual string
size_t const len = arg.length();
std::memcpy(buffer, &len, sizeof(len));
buffer += sizeof(len);

if (len != 0)
if (QUILL_LIKELY(len != 0))
{
// copy the string, no need to zero terminate it as we got the length
std::memcpy(buffer + sizeof(len), arg.data(), arg.length());
std::memcpy(buffer, arg.data(), len);
buffer += len;
}

buffer += sizeof(len) + len;
}
else
{
Expand Down Expand Up @@ -219,8 +219,9 @@ struct Codec
// for std::string we first need to retrieve the length
size_t len;
std::memcpy(&len, buffer, sizeof(len));
std::string_view arg = std::string_view{reinterpret_cast<char const*>(buffer + sizeof(size_t)), len};
buffer += sizeof(len) + len;
buffer += sizeof(len);
std::string_view const arg = std::string_view{reinterpret_cast<char const*>(buffer), len};
buffer += len;
return arg;
}
else
Expand Down
10 changes: 5 additions & 5 deletions include/quill/std/Pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ struct Codec<std::pair<T1, T2>>
static size_t compute_encoded_size(detail::SizeCacheVector& conditional_arg_size_cache,
std::pair<T1, T2> const& arg) noexcept
{
// Explicitly separate the calls to ensure the order of evaluation is maintained on MSVC,
// as it may evaluate the expressions in a different order, leading to side effects.
auto const first_size = Codec<T1>::compute_encoded_size(conditional_arg_size_cache, arg.first);
auto const second_size = Codec<T2>::compute_encoded_size(conditional_arg_size_cache, arg.second);
return first_size + second_size;
// Explicitly separate the calls to ensure the order of evaluation is maintained,
// as the compiler may evaluate the expressions in a different order, leading to side effects.
size_t total_size = Codec<T1>::compute_encoded_size(conditional_arg_size_cache, arg.first);
total_size += Codec<T2>::compute_encoded_size(conditional_arg_size_cache, arg.second);
return total_size;
}

static void encode(std::byte*& buffer, detail::SizeCacheVector const& conditional_arg_size_cache,
Expand Down

0 comments on commit e7136ba

Please sign in to comment.