Skip to content

<format>: Avoid unnecessary calls to _Getcvt() #1825

Description

@statementreply

The performance of format degraded after #1815. Profiling shows that _Getcvt() is consuming a significant part of the CPU time.

We could investigate:

  • Avoid calling _Getcvt() when unnecessary (when the format string is interpreted as UTF-8 or UTF-16).
  • Reduce the number of calls to _Getcvt().
#include <chrono>
#include <format>
#include <iostream>
#include <iterator>
#include <ratio>

using namespace std;
using namespace std::chrono;

int main() {
    constexpr int iters = 10'000'000;

    string buf;
    buf.reserve(64);
    size_t dummy = 0;

    const auto start_time = steady_clock::now();

    for (int i = 0; i < iters; ++i) {
        buf.clear();
        format_to(back_inserter(buf), "run {0}/{1}", i + 1, iters);
        dummy += buf.size();
    }

    const auto end_time     = steady_clock::now();
    const auto average_time = duration<double, nano>{end_time - start_time} / iters;

    cout << "average time: " << average_time << "\n";
    cout << format("dummy: {}\n", dummy);
}
cl /EHsc /W4 /WX /std:c++latest /O2 /utf-8 temp.cpp

Before #1815: 179 ns
After #1815: 241 ns

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedSomething works now, yay!formatC++20/23 formatperformanceMust go faster

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions