Skip to content

Commit

Permalink
Optimise size calculation for fundamental types, std::string, and std…
Browse files Browse the repository at this point in the history
…::string_view in compute_encoded_size_and_cache_string_lengths
  • Loading branch information
odygrd committed Sep 22, 2024
1 parent 195c930 commit a39d257
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
attempting to log `std::string s(std::numeric_limits<uint32_t>::max(), 'a');` would previously cause a crash.
- Tuned `transit_events_soft_limit` and `transit_events_hard_limit` default values; added error checks for invalid
configurations.
- Improved dynamic log level handling by checking at compile-time using `constexpr` on the hot path.
- Improved dynamic log level handling and optimize size calculation for fundamental types, std::string and std::
string_view on the hot path.
- Added support for appending a custom timestamp format to log filenames via `StartCustomTimestampFormat`.
Example usage:
```cpp
Expand Down
15 changes: 12 additions & 3 deletions include/quill/core/Codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,20 @@ template <typename... Args>
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT size_t compute_encoded_size_and_cache_string_lengths(
QUILL_MAYBE_UNUSED detail::SizeCacheVector& conditional_arg_size_cache, Args const&... args) noexcept
{
// we always need to clear the cache first before calling this function
conditional_arg_size_cache.clear();
if constexpr (!std::conjunction_v<std::disjunction<
std::is_arithmetic<detail::remove_cvref_t<Args>>, std::is_enum<detail::remove_cvref_t<Args>>,
std::is_same<detail::remove_cvref_t<Args>, void const*>, detail::is_std_string<detail::remove_cvref_t<Args>>,
std::is_same<detail::remove_cvref_t<Args>, std::string_view>>...>)
{
// Clear the cache whenever processing involves non-fundamental types,
// or when the arguments are not of type std::string or std::string_view.
conditional_arg_size_cache.clear();
}

size_t total_sum{0};
// Do not use fold expression with '+ ...' as we need a guaranteed sequence for the args here
// Avoid using a fold expression with '+ ...' because we require a guaranteed evaluation
// order to ensure that each argument is processed in sequence. This is essential for
// correctly populating the conditional_arg_size_cache
((total_sum += Codec<detail::remove_cvref_t<Args>>::compute_encoded_size(conditional_arg_size_cache, args)), ...);
return total_sum;
}
Expand Down

0 comments on commit a39d257

Please sign in to comment.