[libcxx-commits] [PATCH] D125606: [libc++][format] Improve string formatters

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat May 14 06:04:17 PDT 2022


Mordante created this revision.
Herald added a subscriber: mgorny.
Herald added a project: All.
Mordante requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

This changes the implementation of the formatter. Instead of inheriting
from a specialized parser all formatters will use the same generic
parser. This reduces the binary size.

The new parser contains some additional fields only used in the chrono
formatting. Since this doesn't change the size of the parser the fields
are in the generic parser. The parser is designed to fit in 128-bit,
making it cheap to pass by value.

The new format function is a const member function. This isn't required
by the Standard yet, but it will be after LWG-3636 is accepted.
Additionally P2286 <https://reviews.llvm.org/P2286> adds a formattable concept which requires the member
function to be const qualified in C++23. This paper is likely to be
accepted in the 2022 July plenary.

Depends on D121530 <https://reviews.llvm.org/D121530>

NOTE parts of the code now contains duplicates for the current and new parser.
The intention is to remove the duplication in followup patches. A general
overview of the final code is available in D124620 <https://reviews.llvm.org/D124620>. That review however lacks a
bit of polish.

Most of the new code is based on the same algorithms used in the current code.

The final version of this code reduces the binary size by 17 KB for this example
code

  int main() {
    {
      std::string_view sv{"hello world"};
      std::format("{}{}|{}{}{}{}{}{}|{}{}{}{}{}{}|{}{}{}|{}{}|{}", true, '*',
                  (signed char)(42), (short)(42), (int)(42), (long)(42), (long long)(42), (__int128_t)(42),
                  (unsigned char)(42), (unsigned short)(42), (unsigned int)(42), (unsigned long)(42),
                  (unsigned long long)(42), (__uint128_t)(42),
                  (float)(42), (double)(42), (long double)(42),
                  "hello world", sv,
                  nullptr);
    }
    {
      std::wstring_view sv{L"hello world"};
      std::format(L"{}{}|{}{}{}{}{}{}|{}{}{}{}{}{}|{}{}{}|{}{}|{}", true, L'*',
                  (signed char)(42), (short)(42), (int)(42), (long)(42), (long long)(42), (__int128_t)(42),
                  (unsigned char)(42), (unsigned short)(42), (unsigned int)(42), (unsigned long)(42),
                  (unsigned long long)(42), (__uint128_t)(42),
                  (float)(42), (double)(42), (long double)(42),
                  L"hello world", sv,
                  nullptr);
    }
  }


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125606

Files:
  libcxx/include/CMakeLists.txt
  libcxx/include/__format/formatter_output.h
  libcxx/include/__format/formatter_string.h
  libcxx/include/__format/parser_std_format_spec.h
  libcxx/include/format
  libcxx/include/module.modulemap
  libcxx/test/libcxx/private_headers.verify.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125606.429442.patch
Type: text/x-patch
Size: 34481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220514/2ae85fc5/attachment-0001.bin>


More information about the libcxx-commits mailing list