Skip to content

Commit

Permalink
Optimize string creation
Browse files Browse the repository at this point in the history
  • Loading branch information
massimiliano96 committed Mar 17, 2024
1 parent be7edb5 commit bed0cf9
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions include/spdlog/pattern_formatter-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,24 @@ class mdc_formatter : public flag_formatter {
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
auto mdc_map = mdc::get_context();
if (!mdc_map.empty()) {
auto last_element = *(--mdc_map.end());
for (const auto &pair : mdc_map) {
std::string mdc_content_;
if (pair != last_element) {
mdc_content_ = pair.first + ':' + pair.second + ' ';
} else {
mdc_content_ = pair.first + ':' + pair.second;
auto last_element = --mdc_map.end();
for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) {
auto &pair = *it;
const auto &key = pair.first;
const auto &value = pair.second;
size_t content_size = key.size() + value.size() + 1; // 1 for ':'

if (it != last_element) {
content_size++; // 1 for ' '
}

ScopedPadder p(content_size, padinfo_, dest);
fmt_helper::append_string_view(key, dest);
fmt_helper::append_string_view(":", dest);
fmt_helper::append_string_view(value, dest);
if (it != last_element) {
fmt_helper::append_string_view(" ", dest);
}
ScopedPadder p(mdc_content_.size(), padinfo_, dest);
fmt_helper::append_string_view(mdc_content_, dest);
}
}
}
Expand Down

0 comments on commit bed0cf9

Please sign in to comment.