Perf: Optimize function append in include/fmt/base.h#4541
Merged
vitaut merged 4 commits intofmtlib:masterfrom Sep 21, 2025
Merged
Perf: Optimize function append in include/fmt/base.h#4541vitaut merged 4 commits intofmtlib:masterfrom
vitaut merged 4 commits intofmtlib:masterfrom
Conversation
vitaut
reviewed
Sep 21, 2025
include/fmt/base.h
Outdated
| if (free_cap < count) count = free_cap; | ||
| auto count = to_unsigned(end - begin); | ||
| if (free_cap < count) { | ||
| try_reserve(size_ + count); |
Contributor
There was a problem hiding this comment.
Let's replace this with
grow_(*this, size_ + count);to avoid an extra capacity check.
Contributor
Author
There was a problem hiding this comment.
Thanks for the suggestion!
I've replaced try_reserve with grow_ to avoid the extra capacity check as you pointed out. The code has been updated.
Contributor
|
Merged, thanks! |
Contributor
Author
|
Thank you for your quick action! 🫶 |
netbsd-srcmastr
pushed a commit
to NetBSD/pkgsrc
that referenced
this pull request
Nov 2, 2025
# 12.1.0 - 2025-10-29 - Optimized `buffer::append`, resulting in up to ~16% improvement on spdlog benchmarks (fmtlib/fmt#4541). Thanks @fyrsta7. - Worked around an ABI incompatibility in `std::locale_ref` between clang and gcc (fmtlib/fmt#4573). - Made `std::variant` and `std::expected` formatters work with `format_as` (fmtlib/fmt#4574, fmtlib/fmt#4575). Thanks @phprus. - Made `fmt::join<string_view>` work with C++ modules (fmtlib/fmt#4379, fmtlib/fmt#4577). Thanks @Arghnews. - Exported `fmt::is_compiled_string` and `operator""_cf` from the module (fmtlib/fmt#4544). Thanks @CrackedMatter. - Fixed a compatibility issue with C++ modules in clang (fmtlib/fmt#4548). Thanks @tsarn. - Added support for cv-qualified types to the `std::optional` formatter (fmtlib/fmt#4561, fmtlib/fmt#4562). Thanks @OleksandrKvl. - Added demangling support (used in exception and `std::type_info` formatters) for libc++ and clang-cl (fmtlib/fmt#4542, fmtlib/fmt#4560, fmtlib/fmt#4568, fmtlib/fmt#4571). Thanks @FatihBAKIR and @rohitsutreja. - Switched to global `malloc`/`free` to enable allocator customization (fmtlib/fmt#4569, fmtlib/fmt#4570). Thanks @rohitsutreja. - Made the `FMT_USE_CONSTEVAL` macro configurable by users (fmtlib/fmt#4546). Thanks @SnapperTT. - Fixed compilation with locales disabled in the header-only mode (fmtlib/fmt#4550). - Fixed compilation with clang 21 and `-std=c++20` (fmtlib/fmt#4552). - Fixed a dynamic linking issue with clang-cl (fmtlib/fmt#4576, fmtlib/fmt#4584). Thanks @FatihBAKIR. - Fixed a warning suppression leakage on gcc (fmtlib/fmt#4588). Thanks @ZedThree. - Made more internal color APIs `constexpr` (fmtlib/fmt#4581). Thanks @ishani. - Fixed compatibility with clang as a host compiler for NVCC (fmtlib/fmt#4564). Thanks @valgur. - Fixed various warnings and lint issues (fmtlib/fmt#4565, fmtlib/fmt#4572, fmtlib/fmt#4557). Thanks @LiangHuDream and @teruyamato0731. - Improved documentation (fmtlib/fmt#4549, fmtlib/fmt#4551, fmtlib/fmt#4566, fmtlib/fmt#4567, fmtlib/fmt#4578,). Thanks @teruyamato0731, @petersteneteg and @zimmerman-dev.
polter-rnd
added a commit
to polter-rnd/slimlog
that referenced
this pull request
Dec 15, 2025
polter-rnd
added a commit
to polter-rnd/slimlog
that referenced
this pull request
Dec 30, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR optimizes the performance of the
appendfunction ininclude/fmt/base.h.This performance improvement was identified while profiling the
spdloglogging library, a major downstream project that bundlesfmt. The benchmarks show that this optimization provides significant gains inspdlog's throughput and latency. For context, this change was first submitted tospdlog(see gabime/spdlog#3465) where I was advised to contribute it upstream directly tofmt.Across 47 test cases from spdlog's benchmark suite, this change achieves a maximum improvement of 16.67% while guaranteeing no regression exceeds 0.99% in any other case.
Test Plan
fmtproject pass.spdlogproject. This was chosen becausespdlogis a key real-world use case, and its benchmarks effectively measure the performance offmt's core formatting operations under various conditions (single-threaded, multi-threaded, different logging patterns). The commands used were./bench/benchand./bench/latencyfrom thespdlogrepository.Performance Evaluation & Results
Testing Protocol:
spdlogbenchmark suite.(new_value - old_value) / old_value * 100%if a higher value is better (e.g., throughput), or(old_value - new_value) / new_value * 100%if a lower value is better (e.g., latency). A positive percentage indicates a performance gain.Results:
overall_throughput_improvement3.00%overall_latency_improvement3.19%single_threaded.level_off.normal.messages_per_sec0.37%single_threaded.level_off.backtrace_on.messages_per_sec4.33%single_threaded.rotating_st.normal.messages_per_sec2.82%single_threaded.rotating_st.backtrace_on.messages_per_sec1.95%single_threaded.basic_st.normal.messages_per_sec3.15%single_threaded.basic_st.backtrace_on.messages_per_sec3.48%single_threaded.daily_st.normal.messages_per_sec3.13%single_threaded.daily_st.backtrace_on.messages_per_sec3.12%multi_threaded_1.rotating_mt.normal.messages_per_sec1.41%multi_threaded_1.rotating_mt.backtrace_on.messages_per_sec3.45%multi_threaded_1.daily_mt.normal.messages_per_sec2.73%multi_threaded_1.daily_mt.backtrace_on.messages_per_sec12.95%multi_threaded_1.basic_mt.normal.messages_per_sec2.48%multi_threaded_1.basic_mt.backtrace_on.messages_per_sec2.73%multi_threaded_1.level_off.normal.messages_per_sec-0.19%multi_threaded_1.level_off.backtrace_on.messages_per_sec4.35%multi_threaded_4.rotating_mt.normal.messages_per_sec2.49%multi_threaded_4.rotating_mt.backtrace_on.messages_per_sec1.82%multi_threaded_4.daily_mt.normal.messages_per_sec6.08%multi_threaded_4.daily_mt.backtrace_on.messages_per_sec3.00%multi_threaded_4.basic_mt.normal.messages_per_sec3.94%multi_threaded_4.basic_mt.backtrace_on.messages_per_sec-0.69%multi_threaded_4.level_off.normal.messages_per_sec-0.99%multi_threaded_4.level_off.backtrace_on.messages_per_sec4.07%single_threaded.level_off.backtrace_on.elapsed_time0.00%single_threaded.rotating_st.normal.elapsed_time0.00%single_threaded.rotating_st.backtrace_on.elapsed_time0.00%single_threaded.basic_st.normal.elapsed_time0.00%single_threaded.basic_st.backtrace_on.elapsed_time16.67%single_threaded.daily_st.normal.elapsed_time3.33%single_threaded.daily_st.backtrace_on.elapsed_time12.50%multi_threaded_1.rotating_mt.normal.elapsed_time3.03%multi_threaded_1.rotating_mt.backtrace_on.elapsed_time4.76%multi_threaded_1.daily_mt.normal.elapsed_time0.00%multi_threaded_1.daily_mt.backtrace_on.elapsed_time14.89%multi_threaded_1.basic_mt.normal.elapsed_time0.00%multi_threaded_1.basic_mt.backtrace_on.elapsed_time0.00%multi_threaded_1.level_off.backtrace_on.elapsed_time0.00%multi_threaded_4.rotating_mt.normal.elapsed_time1.59%multi_threaded_4.rotating_mt.backtrace_on.elapsed_time0.00%multi_threaded_4.daily_mt.normal.elapsed_time6.06%multi_threaded_4.daily_mt.backtrace_on.elapsed_time2.47%multi_threaded_4.basic_mt.normal.elapsed_time1.67%multi_threaded_4.basic_mt.backtrace_on.elapsed_time0.00%multi_threaded_4.level_off.backtrace_on.elapsed_time0.00%