diff --git a/CHANGELOG.md b/CHANGELOG.md index 67d8c82f..01506ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,7 +100,7 @@ attempting to log `std::string s(std::numeric_limits::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 on the hot path. - Added support for appending a custom timestamp format to log filenames via `StartCustomTimestampFormat`. Example usage: ```cpp diff --git a/include/quill/core/Codec.h b/include/quill/core/Codec.h index 88ea00de..0754ebcc 100644 --- a/include/quill/core/Codec.h +++ b/include/quill/core/Codec.h @@ -266,15 +266,27 @@ namespace detail */ template 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 + QUILL_MAYBE_UNUSED detail::SizeCacheVector& conditional_arg_size_cache, + QUILL_MAYBE_UNUSED Args const&... args) noexcept { - // we always need to clear the cache first before calling this function - 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 - ((total_sum += Codec>::compute_encoded_size(conditional_arg_size_cache, args)), ...); - return total_sum; + if constexpr (std::conjunction_v>, std::is_enum>, + std::is_same, void const*>>...>) + { + // If all arguments are fundamental types, calculate the total size directly. + return (0ull + ... + sizeof(Args)); + } + else + { + // Always clear the cache since we're processing non-fundamental types + conditional_arg_size_cache.clear(); + + size_t total_sum{0}; + // 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>::compute_encoded_size(conditional_arg_size_cache, args)), ...); + return total_sum; + } } /**