[llvm-branch-commits] [libcxx] [libc++][format][2/7] Optimizes c-string arguments. (PR #101805)

Mark de Wever via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Sep 17 08:22:18 PDT 2024


https://github.com/mordante updated https://github.com/llvm/llvm-project/pull/101805

>From 9c7c29854dd7446c730bb4d15a97a58a829dfa47 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Sat, 30 Mar 2024 17:35:56 +0100
Subject: [PATCH] [libc++][format][2/7] Optimizes c-string arguments.

The formatter specializations for _CharT* and const _CharT* typically
write all elements in a loop. This format's internal functions are
optimized for larger writes.

Instead of writing one element at a time conversion the range to a
basic_string_view and write that instead.

For C string of 6 characters this is a bit slower, but for 60 characters
it's faster. The improvements for back_inserter<std::list<_CharT>> are not
as great as the others; it just gets as slow as basic_string_view<_CharT>.

Before
---------------------------------------------------------------------------------------------------------------
Benchmark                                                                     Time             CPU   Iterations
---------------------------------------------------------------------------------------------------------------
BM_sprintf/C string len = 6                                                4.81 ns         4.80 ns    145890280
BM_format/C string len = 6                                                 52.6 ns         52.4 ns     13252327
BM_format_to_back_inserter<std::string>/C string len = 6                   53.0 ns         52.8 ns     13262680
BM_format_to_back_inserter<std::vector<char>>/C string len = 6             69.7 ns         69.6 ns     10045636
BM_format_to_back_inserter<std::deque<char>>/C string len = 6               148 ns          148 ns      4729368
BM_format_to_back_inserter<std::list<char>>/C string len = 6                127 ns          126 ns      5538441
BM_format_to_iterator/<std::array> C string len = 6                        42.9 ns         42.8 ns     16367158
BM_format_to_iterator/<std::string> C string len = 6                       43.5 ns         43.4 ns     16141644
BM_format_to_iterator/<std::vector> C string len = 6                       42.9 ns         42.8 ns     16366718
BM_format_to_iterator/<std::deque> C string len = 6                        47.8 ns         47.7 ns     14686488
BM_format/string len = 6                                                   55.3 ns         55.2 ns     12696889
BM_format_to_back_inserter<std::string>/string len = 6                     55.4 ns         55.2 ns     12660731
BM_format_to_back_inserter<std::vector<char>>/string len = 6               70.7 ns         70.5 ns      9927313
BM_format_to_back_inserter<std::deque<char>>/string len = 6                 153 ns          153 ns      4573936
BM_format_to_back_inserter<std::list<char>>/string len = 6                  128 ns          128 ns      5486033
BM_format_to_iterator/<std::array> string len = 6                          44.6 ns         44.5 ns     15758122
BM_format_to_iterator/<std::string> string len = 6                         44.7 ns         44.6 ns     15690226
BM_format_to_iterator/<std::vector> string len = 6                         44.3 ns         44.2 ns     15715898
BM_format_to_iterator/<std::deque> string len = 6                          50.3 ns         50.1 ns     13958635
BM_format/string_view len = 6                                              54.2 ns         54.1 ns     12929525
BM_format_to_back_inserter<std::string>/string_view len = 6                54.3 ns         54.1 ns     12929219
BM_format_to_back_inserter<std::vector<char>>/string_view len = 6          70.0 ns         69.8 ns     10022355
BM_format_to_back_inserter<std::deque<char>>/string_view len = 6            153 ns          152 ns      4585749
BM_format_to_back_inserter<std::list<char>>/string_view len = 6             128 ns          128 ns      5489760
BM_format_to_iterator/<std::array> string_view len = 6                     44.2 ns         44.1 ns     15884839
BM_format_to_iterator/<std::string> string_view len = 6                    44.8 ns         44.6 ns     15664278
BM_format_to_iterator/<std::vector> string_view len = 6                    44.7 ns         44.6 ns     15716983
BM_format_to_iterator/<std::deque> string_view len = 6                     50.3 ns         50.2 ns     13936091
BM_sprintf/C string len = 60                                               4.16 ns         4.15 ns    168764227
BM_format/C string len = 60                                                 169 ns          169 ns      4144060
BM_format_to_back_inserter<std::string>/C string len = 60                   167 ns          167 ns      4203915
BM_format_to_back_inserter<std::vector<char>>/C string len = 60             177 ns          176 ns      3965619
BM_format_to_back_inserter<std::deque<char>>/C string len = 60              383 ns          382 ns      1832531
BM_format_to_back_inserter<std::list<char>>/C string len = 60              1270 ns         1267 ns       552686
BM_format_to_iterator/<std::array> C string len = 60                        141 ns          140 ns      4988441
BM_format_to_iterator/<std::string> C string len = 60                       141 ns          141 ns      4956101
BM_format_to_iterator/<std::vector> C string len = 60                       141 ns          141 ns      4963443
BM_format_to_iterator/<std::deque> C string len = 60                        144 ns          143 ns      4893139
BM_format/string len = 60                                                  73.4 ns         73.2 ns      9548455
BM_format_to_back_inserter<std::string>/string len = 60                    73.2 ns         73.0 ns      9524524
BM_format_to_back_inserter<std::vector<char>>/string len = 60              81.6 ns         81.4 ns      8584033
BM_format_to_back_inserter<std::deque<char>>/string len = 60                279 ns          279 ns      2515146
BM_format_to_back_inserter<std::list<char>>/string len = 60                1177 ns         1174 ns       597172
BM_format_to_iterator/<std::array> string len = 60                         44.5 ns         44.4 ns     15753131
BM_format_to_iterator/<std::string> string len = 60                        44.7 ns         44.6 ns     15692630
BM_format_to_iterator/<std::vector> string len = 60                        44.8 ns         44.7 ns     15664689
BM_format_to_iterator/<std::deque> string len = 60                         50.6 ns         50.5 ns     13838617
BM_format/string_view len = 60                                             72.8 ns         72.6 ns      9674007
BM_format_to_back_inserter<std::string>/string_view len = 60               72.7 ns         72.6 ns      9638209
BM_format_to_back_inserter<std::vector<char>>/string_view len = 60         82.6 ns         82.4 ns      8496602
BM_format_to_back_inserter<std::deque<char>>/string_view len = 60           280 ns          280 ns      2508982
BM_format_to_back_inserter<std::list<char>>/string_view len = 60           1176 ns         1173 ns       597714
BM_format_to_iterator/<std::array> string_view len = 60                    44.2 ns         44.1 ns     15896934
BM_format_to_iterator/<std::string> string_view len = 60                   44.7 ns         44.6 ns     15695427
BM_format_to_iterator/<std::vector> string_view len = 60                   44.7 ns         44.6 ns     15680899
BM_format_to_iterator/<std::deque> string_view len = 60                    50.3 ns         50.1 ns     13962755
BM_sprintf/C string len = 6000                                              114 ns          114 ns      6170153
BM_format/C string len = 6000                                             11792 ns        11763 ns        59619
BM_format_to_back_inserter<std::string>/C string len = 6000               11778 ns        11746 ns        59572
BM_format_to_back_inserter<std::vector<char>>/C string len = 6000         11715 ns        11686 ns        60053
BM_format_to_back_inserter<std::deque<char>>/C string len = 6000          25464 ns        25403 ns        27538
BM_format_to_back_inserter<std::list<char>>/C string len = 6000          126044 ns       125726 ns         5572
BM_format_to_iterator/<std::array> C string len = 6000                    10792 ns        10766 ns        64943
BM_format_to_iterator/<std::string> C string len = 6000                   10791 ns        10761 ns        64918
BM_format_to_iterator/<std::vector> C string len = 6000                   10775 ns        10750 ns        65149
BM_format_to_iterator/<std::deque> C string len = 6000                    11242 ns        11215 ns        62542
BM_format/string len = 6000                                                 919 ns          917 ns       763961
BM_format_to_back_inserter<std::string>/string len = 6000                   920 ns          917 ns       763094
BM_format_to_back_inserter<std::vector<char>>/string len = 6000             888 ns          886 ns       793468
BM_format_to_back_inserter<std::deque<char>>/string len = 6000            14999 ns        14963 ns        46758
BM_format_to_back_inserter<std::list<char>>/string len = 6000            114504 ns       114213 ns         6130
BM_format_to_iterator/<std::array> string len = 6000                        120 ns          120 ns      5823426
BM_format_to_iterator/<std::string> string len = 6000                       106 ns          106 ns      6609299
BM_format_to_iterator/<std::vector> string len = 6000                       106 ns          106 ns      6613867
BM_format_to_iterator/<std::deque> string len = 6000                        367 ns          366 ns      1912818
BM_format/string_view len = 6000                                            910 ns          907 ns       771491
BM_format_to_back_inserter<std::string>/string_view len = 6000              911 ns          909 ns       770065
BM_format_to_back_inserter<std::vector<char>>/string_view len = 6000        878 ns          876 ns       794976
BM_format_to_back_inserter<std::deque<char>>/string_view len = 6000       15031 ns        14994 ns        46651
BM_format_to_back_inserter<std::list<char>>/string_view len = 6000       114510 ns       114219 ns         6130
BM_format_to_iterator/<std::array> string_view len = 6000                   121 ns          120 ns      5814249
BM_format_to_iterator/<std::string> string_view len = 6000                  116 ns          115 ns      6072315
BM_format_to_iterator/<std::vector> string_view len = 6000                  115 ns          115 ns      6095762
BM_format_to_iterator/<std::deque> string_view len = 6000                   351 ns          350 ns      2002175

After
---------------------------------------------------------------------------------------------------------------
Benchmark                                                                     Time             CPU   Iterations
---------------------------------------------------------------------------------------------------------------
BM_sprintf/C string len = 6                                                4.83 ns         4.83 ns    145502957
BM_format/C string len = 6                                                 55.1 ns         55.1 ns     12687656
BM_format_to_back_inserter<std::string>/C string len = 6                   55.1 ns         55.1 ns     12691642
BM_format_to_back_inserter<std::vector<char>>/C string len = 6             71.2 ns         71.3 ns      9819560
BM_format_to_back_inserter<std::deque<char>>/C string len = 6               154 ns          154 ns      4548709
BM_format_to_back_inserter<std::list<char>>/C string len = 6                131 ns          131 ns      5338318
BM_format_to_iterator/<std::array> C string len = 6                        45.0 ns         45.0 ns     15569429
BM_format_to_iterator/<std::string> C string len = 6                       45.9 ns         45.9 ns     15240594
BM_format_to_iterator/<std::vector> C string len = 6                       44.4 ns         44.4 ns     15768343
BM_format_to_iterator/<std::deque> C string len = 6                        50.1 ns         50.1 ns     13995837
BM_format/string len = 6                                                   54.8 ns         54.8 ns     12738209
BM_format_to_back_inserter<std::string>/string len = 6                     55.4 ns         55.4 ns     12634483
BM_format_to_back_inserter<std::vector<char>>/string len = 6               70.4 ns         70.4 ns      9948288
BM_format_to_back_inserter<std::deque<char>>/string len = 6                 154 ns          154 ns      4531741
BM_format_to_back_inserter<std::list<char>>/string len = 6                  132 ns          132 ns      5289798
BM_format_to_iterator/<std::array> string len = 6                          44.5 ns         44.5 ns     15743437
BM_format_to_iterator/<std::string> string len = 6                         44.9 ns         44.9 ns     15591961
BM_format_to_iterator/<std::vector> string len = 6                         45.0 ns         45.0 ns     15541308
BM_format_to_iterator/<std::deque> string len = 6                          50.5 ns         50.5 ns     13837884
BM_format/string_view len = 6                                              54.5 ns         54.5 ns     12833591
BM_format_to_back_inserter<std::string>/string_view len = 6                54.6 ns         54.6 ns     12818233
BM_format_to_back_inserter<std::vector<char>>/string_view len = 6          69.9 ns         69.9 ns     10018025
BM_format_to_back_inserter<std::deque<char>>/string_view len = 6            154 ns          154 ns      4534215
BM_format_to_back_inserter<std::list<char>>/string_view len = 6             131 ns          131 ns      5337938
BM_format_to_iterator/<std::array> string_view len = 6                     44.2 ns         44.2 ns     15854695
BM_format_to_iterator/<std::string> string_view len = 6                    45.0 ns         45.0 ns     15551054
BM_format_to_iterator/<std::vector> string_view len = 6                    45.0 ns         45.0 ns     15567905
BM_format_to_iterator/<std::deque> string_view len = 6                     50.5 ns         50.5 ns     13858261
BM_sprintf/C string len = 60                                               4.16 ns         4.16 ns    168066307
BM_format/C string len = 60                                                73.8 ns         73.8 ns      9487364
BM_format_to_back_inserter<std::string>/C string len = 60                  73.7 ns         73.7 ns      9492371
BM_format_to_back_inserter<std::vector<char>>/C string len = 60            83.1 ns         83.1 ns      8399017
BM_format_to_back_inserter<std::deque<char>>/C string len = 60              284 ns          284 ns      2466510
BM_format_to_back_inserter<std::list<char>>/C string len = 60              1177 ns         1177 ns       592514
BM_format_to_iterator/<std::array> C string len = 60                       44.9 ns         44.9 ns     15558172
BM_format_to_iterator/<std::string> C string len = 60                      45.8 ns         45.8 ns     15283103
BM_format_to_iterator/<std::vector> C string len = 60                      44.6 ns         44.6 ns     15678967
BM_format_to_iterator/<std::deque> C string len = 60                       50.5 ns         50.5 ns     13839524
BM_format/string len = 60                                                  72.2 ns         72.2 ns      9687634
BM_format_to_back_inserter<std::string>/string len = 60                    72.0 ns         72.1 ns      9695746
BM_format_to_back_inserter<std::vector<char>>/string len = 60              82.3 ns         82.3 ns      8509528
BM_format_to_back_inserter<std::deque<char>>/string len = 60                279 ns          279 ns      2506108
BM_format_to_back_inserter<std::list<char>>/string len = 60                1180 ns         1180 ns       592963
BM_format_to_iterator/<std::array> string len = 60                         44.7 ns         44.7 ns     15657865
BM_format_to_iterator/<std::string> string len = 60                        45.0 ns         45.0 ns     15569049
BM_format_to_iterator/<std::vector> string len = 60                        44.9 ns         44.9 ns     15576187
BM_format_to_iterator/<std::deque> string len = 60                         50.7 ns         50.7 ns     13803188
BM_format/string_view len = 60                                             72.4 ns         72.4 ns      9663061
BM_format_to_back_inserter<std::string>/string_view len = 60               72.8 ns         72.8 ns      9638941
BM_format_to_back_inserter<std::vector<char>>/string_view len = 60         81.9 ns         81.9 ns      8529931
BM_format_to_back_inserter<std::deque<char>>/string_view len = 60           283 ns          283 ns      2471522
BM_format_to_back_inserter<std::list<char>>/string_view len = 60           1177 ns         1177 ns       594752
BM_format_to_iterator/<std::array> string_view len = 60                    44.2 ns         44.2 ns     15841413
BM_format_to_iterator/<std::string> string_view len = 60                   45.1 ns         45.1 ns     15546279
BM_format_to_iterator/<std::vector> string_view len = 60                   45.0 ns         45.0 ns     15555756
BM_format_to_iterator/<std::deque> string_view len = 60                    50.1 ns         50.1 ns     10000000
BM_sprintf/C string len = 6000                                              344 ns          344 ns      2037753
BM_format/C string len = 6000                                               980 ns          980 ns       713341
BM_format_to_back_inserter<std::string>/C string len = 6000                 979 ns          979 ns       709571
BM_format_to_back_inserter<std::vector<char>>/C string len = 6000           944 ns          944 ns       744518
BM_format_to_back_inserter<std::deque<char>>/C string len = 6000          14916 ns        14915 ns        46875
BM_format_to_back_inserter<std::list<char>>/C string len = 6000          114686 ns       114688 ns         6110
BM_format_to_iterator/<std::array> C string len = 6000                      159 ns          159 ns      4394506
BM_format_to_iterator/<std::string> C string len = 6000                     159 ns          159 ns      4384577
BM_format_to_iterator/<std::vector> C string len = 6000                     159 ns          159 ns      4393225
BM_format_to_iterator/<std::deque> C string len = 6000                      437 ns          437 ns      1601253
BM_format/string len = 6000                                                 929 ns          929 ns       751889
BM_format_to_back_inserter<std::string>/string len = 6000                   933 ns          933 ns       752042
BM_format_to_back_inserter<std::vector<char>>/string len = 6000             892 ns          892 ns       785064
BM_format_to_back_inserter<std::deque<char>>/string len = 6000            14840 ns        14839 ns        47177
BM_format_to_back_inserter<std::list<char>>/string len = 6000            114940 ns       114942 ns         6095
BM_format_to_iterator/<std::array> string len = 6000                        144 ns          144 ns      4859849
BM_format_to_iterator/<std::string> string len = 6000                       106 ns          106 ns      6565564
BM_format_to_iterator/<std::vector> string len = 6000                       107 ns          107 ns      6584152
BM_format_to_iterator/<std::deque> string len = 6000                        368 ns          368 ns      1904727
BM_format/string_view len = 6000                                            919 ns          919 ns       760401
BM_format_to_back_inserter<std::string>/string_view len = 6000              917 ns          917 ns       760934
BM_format_to_back_inserter<std::vector<char>>/string_view len = 6000        880 ns          880 ns       796244
BM_format_to_back_inserter<std::deque<char>>/string_view len = 6000       14854 ns        14853 ns        47101
BM_format_to_back_inserter<std::list<char>>/string_view len = 6000       114963 ns       114967 ns         6086
BM_format_to_iterator/<std::array> string_view len = 6000                   117 ns          117 ns      5974080
BM_format_to_iterator/<std::string> string_view len = 6000                  116 ns          116 ns      6016080
BM_format_to_iterator/<std::vector> string_view len = 6000                  116 ns          116 ns      6053780
BM_format_to_iterator/<std::deque> string_view len = 6000                   351 ns          351 ns      1993879

Comparing libcxx/benchmarks/1/write_string_comparison.bench.out to libcxx/benchmarks/2/write_string_comparison.bench.out
Benchmark                                                                              Time             CPU      Time Old      Time New       CPU Old       CPU New
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
BM_sprintf/C string len = 6                                                         +0.0034         +0.0058             5             5             5             5
BM_format/C string len = 6                                                          +0.0484         +0.0508            53            55            52            55
BM_format_to_back_inserter<std::string>/C string len = 6                            +0.0395         +0.0424            53            55            53            55
BM_format_to_back_inserter<std::vector<char>>/C string len = 6                      +0.0220         +0.0244            70            71            70            71
BM_format_to_back_inserter<std::deque<char>>/C string len = 6                       +0.0399         +0.0424           148           154           148           154
BM_format_to_back_inserter<std::list<char>>/C string len = 6                        +0.0339         +0.0365           127           131           126           131
BM_format_to_iterator/<std::array> C string len = 6                                 +0.0494         +0.0520            43            45            43            45
BM_format_to_iterator/<std::string> C string len = 6                                +0.0539         +0.0568            44            46            43            46
BM_format_to_iterator/<std::vector> C string len = 6                                +0.0359         +0.0384            43            44            43            44
BM_format_to_iterator/<std::deque> C string len = 6                                 +0.0488         +0.0513            48            50            48            50
BM_format/string len = 6                                                            -0.0105         -0.0081            55            55            55            55
BM_format_to_back_inserter<std::string>/string len = 6                              +0.0008         +0.0036            55            55            55            55
BM_format_to_back_inserter<std::vector<char>>/string len = 6                        -0.0046         -0.0021            71            70            71            70
BM_format_to_back_inserter<std::deque<char>>/string len = 6                         +0.0066         +0.0091           153           154           153           154
BM_format_to_back_inserter<std::list<char>>/string len = 6                          +0.0291         +0.0316           128           132           128           132
BM_format_to_iterator/<std::array> string len = 6                                   -0.0030         -0.0006            45            44            44            44
BM_format_to_iterator/<std::string> string len = 6                                  +0.0045         +0.0069            45            45            45            45
BM_format_to_iterator/<std::vector> string len = 6                                  +0.0150         +0.0177            44            45            44            45
BM_format_to_iterator/<std::deque> string len = 6                                   +0.0059         +0.0084            50            51            50            51
BM_format/string_view len = 6                                                       +0.0050         +0.0075            54            55            54            55
BM_format_to_back_inserter<std::string>/string_view len = 6                         +0.0052         +0.0081            54            55            54            55
BM_format_to_back_inserter<std::vector<char>>/string_view len = 6                   -0.0017         +0.0008            70            70            70            70
BM_format_to_back_inserter<std::deque<char>>/string_view len = 6                    +0.0113         +0.0137           153           154           152           154
BM_format_to_back_inserter<std::list<char>>/string_view len = 6                     +0.0260         +0.0286           128           131           128           131
BM_format_to_iterator/<std::array> string_view len = 6                              -0.0006         +0.0021            44            44            44            44
BM_format_to_iterator/<std::string> string_view len = 6                             +0.0059         +0.0084            45            45            45            45
BM_format_to_iterator/<std::vector> string_view len = 6                             +0.0083         +0.0108            45            45            45            45
BM_format_to_iterator/<std::deque> string_view len = 6                              +0.0048         +0.0073            50            51            50            51
BM_sprintf/C string len = 60                                                        +0.0002         +0.0029             4             4             4             4
BM_format/C string len = 60                                                         -0.5640         -0.5629           169            74           169            74
BM_format_to_back_inserter<std::string>/C string len = 60                           -0.5592         -0.5582           167            74           167            74
BM_format_to_back_inserter<std::vector<char>>/C string len = 60                     -0.5294         -0.5282           177            83           176            83
BM_format_to_back_inserter<std::deque<char>>/C string len = 60                      -0.2591         -0.2570           383           284           382           284
BM_format_to_back_inserter<std::list<char>>/C string len = 60                       -0.0730         -0.0707          1270          1177          1267          1177
BM_format_to_iterator/<std::array> C string len = 60                                -0.6804         -0.6797           141            45           140            45
BM_format_to_iterator/<std::string> C string len = 60                               -0.6759         -0.6750           141            46           141            46
BM_format_to_iterator/<std::vector> C string len = 60                               -0.6849         -0.6841           141            45           141            45
BM_format_to_iterator/<std::deque> C string len = 60                                -0.6481         -0.6471           144            51           143            51
BM_format/string len = 60                                                           -0.0167         -0.0142            73            72            73            72
BM_format_to_back_inserter<std::string>/string len = 60                             -0.0151         -0.0127            73            72            73            72
BM_format_to_back_inserter<std::vector<char>>/string len = 60                       +0.0093         +0.0118            82            82            81            82
BM_format_to_back_inserter<std::deque<char>>/string len = 60                        -0.0003         +0.0023           279           279           279           279
BM_format_to_back_inserter<std::list<char>>/string len = 60                         +0.0025         +0.0050          1177          1180          1174          1180
BM_format_to_iterator/<std::array> string len = 60                                  +0.0046         +0.0070            45            45            44            45
BM_format_to_iterator/<std::string> string len = 60                                 +0.0050         +0.0075            45            45            45            45
BM_format_to_iterator/<std::vector> string len = 60                                 +0.0025         +0.0051            45            45            45            45
BM_format_to_iterator/<std::deque> string len = 60                                  +0.0031         +0.0056            51            51            50            51
BM_format/string_view len = 60                                                      -0.0054         -0.0030            73            72            73            72
BM_format_to_back_inserter<std::string>/string_view len = 60                        +0.0001         +0.0026            73            73            73            73
BM_format_to_back_inserter<std::vector<char>>/string_view len = 60                  -0.0090         -0.0066            83            82            82            82
BM_format_to_back_inserter<std::deque<char>>/string_view len = 60                   +0.0097         +0.0126           280           283           280           283
BM_format_to_back_inserter<std::list<char>>/string_view len = 60                    +0.0011         +0.0036          1176          1177          1173          1177
BM_format_to_iterator/<std::array> string_view len = 60                             +0.0001         +0.0026            44            44            44            44
BM_format_to_iterator/<std::string> string_view len = 60                            +0.0083         +0.0107            45            45            45            45
BM_format_to_iterator/<std::vector> string_view len = 60                            +0.0068         +0.0093            45            45            45            45
BM_format_to_iterator/<std::deque> string_view len = 60                             -0.0038         -0.0014            50            50            50            50
BM_sprintf/C string len = 6000                                                      +2.0230         +2.0305           114           344           114           344
BM_format/C string len = 6000                                                       -0.9169         -0.9167         11792           980         11763           980
BM_format_to_back_inserter<std::string>/C string len = 6000                         -0.9169         -0.9166         11778           979         11746           979
BM_format_to_back_inserter<std::vector<char>>/C string len = 6000                   -0.9194         -0.9192         11715           944         11686           944
BM_format_to_back_inserter<std::deque<char>>/C string len = 6000                    -0.4143         -0.4129         25464         14916         25403         14915
BM_format_to_back_inserter<std::list<char>>/C string len = 6000                     -0.0901         -0.0878        126044        114686        125726        114688
BM_format_to_iterator/<std::array> C string len = 6000                              -0.9852         -0.9852         10792           159         10766           159
BM_format_to_iterator/<std::string> C string len = 6000                             -0.9852         -0.9852         10791           159         10761           159
BM_format_to_iterator/<std::vector> C string len = 6000                             -0.9852         -0.9852         10775           159         10750           159
BM_format_to_iterator/<std::deque> C string len = 6000                              -0.9611         -0.9610         11242           437         11215           437
BM_format/string len = 6000                                                         +0.0108         +0.0133           919           929           917           929
BM_format_to_back_inserter<std::string>/string len = 6000                           +0.0138         +0.0167           920           933           917           933
BM_format_to_back_inserter<std::vector<char>>/string len = 6000                     +0.0041         +0.0064           888           892           886           892
BM_format_to_back_inserter<std::deque<char>>/string len = 6000                      -0.0107         -0.0082         14999         14840         14963         14839
BM_format_to_back_inserter<std::list<char>>/string len = 6000                       +0.0038         +0.0064        114504        114940        114213        114942
BM_format_to_iterator/<std::array> string len = 6000                                +0.2011         +0.2040           120           144           120           144
BM_format_to_iterator/<std::string> string len = 6000                               +0.0032         +0.0057           106           106           106           106
BM_format_to_iterator/<std::vector> string len = 6000                               +0.0057         +0.0085           106           107           106           107
BM_format_to_iterator/<std::deque> string len = 6000                                +0.0011         +0.0036           367           368           366           368
BM_format/string_view len = 6000                                                    +0.0105         +0.0130           910           919           907           919
BM_format_to_back_inserter<std::string>/string_view len = 6000                      +0.0057         +0.0081           911           917           909           917
BM_format_to_back_inserter<std::vector<char>>/string_view len = 6000                +0.0023         +0.0050           878           880           876           880
BM_format_to_back_inserter<std::deque<char>>/string_view len = 6000                 -0.0118         -0.0094         15031         14854         14994         14853
BM_format_to_back_inserter<std::list<char>>/string_view len = 6000                  +0.0040         +0.0065        114510        114963        114219        114967
BM_format_to_iterator/<std::array> string_view len = 6000                           -0.0299         -0.0275           121           117           120           117
BM_format_to_iterator/<std::string> string_view len = 6000                          +0.0062         +0.0086           116           116           115           116
BM_format_to_iterator/<std::vector> string_view len = 6000                          +0.0034         +0.0062           115           116           115           116
BM_format_to_iterator/<std::deque> string_view len = 6000                           +0.0011         +0.0035           351           351           350           351
OVERALL_GEOMEAN                                                                     -0.2959         -0.2941             0             0             0             0
---
 libcxx/include/__format/formatter_string.h | 32 +++++-----------------
 1 file changed, 7 insertions(+), 25 deletions(-)

diff --git a/libcxx/include/__format/formatter_string.h b/libcxx/include/__format/formatter_string.h
index dee2b3ad073a51..4a0c988a9714f7 100644
--- a/libcxx/include/__format/formatter_string.h
+++ b/libcxx/include/__format/formatter_string.h
@@ -64,32 +64,14 @@ struct _LIBCPP_TEMPLATE_VIS formatter<const _CharT*, _CharT> : public __formatte
   template <class _FormatContext>
   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const {
     _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer.");
-
-    __format_spec::__parsed_specifications<_CharT> __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx);
-#  if _LIBCPP_STD_VER >= 23
-    if (_Base::__parser_.__type_ == __format_spec::__type::__debug)
-      return __formatter::__format_escaped_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
-#  endif
-
-    // When using a center or right alignment and the width option the length
-    // of __str must be known to add the padding upfront. This case is handled
-    // by the base class by converting the argument to a basic_string_view.
+    // Converting the input to a basic_string_view means the data is looped over twice;
+    // - once to determine the length, and
+    // - once to process the data.
     //
-    // When using left alignment and the width option the padding is added
-    // after outputting __str so the length can be determined while outputting
-    // __str. The same holds true for the precision, during outputting __str it
-    // can be validated whether the precision threshold has been reached. For
-    // now these optimizations aren't implemented. Instead the base class
-    // handles these options.
-    // TODO FMT Implement these improvements.
-    if (__specs.__has_width() || __specs.__has_precision())
-      return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
-
-    // No formatting required, copy the string to the output.
-    auto __out_it = __ctx.out();
-    while (*__str)
-      *__out_it++ = *__str++;
-    return __out_it;
+	// This sounds slower than writing the output directly. However internally
+	// the output algorithms have optimizations for "bulk" operations, which
+	// makes this faster than a single-pass character-by-character output.
+    return _Base::format(basic_string_view<_CharT>(__str), __ctx);
   }
 };
 



More information about the llvm-branch-commits mailing list