Commit Graph

208 Commits

Author SHA1 Message Date
Arthur O'Dwyer db9425cb06 [libc++] [LIBCXX-DEBUG-FIXME] Fix an iterator-invalidation issue in string::assign.
This appears to be a bug in our string::assign: when assigning into
a longer string, from a shorter snippet of itself, we invalidate
iterators before doing the copy. We should invalidate them afterward.
Also drive-by improve the formatting of a function header.

Differential Revision: https://reviews.llvm.org/D101675
2021-05-05 16:20:53 -04:00
Arthur O'Dwyer e87479b00f [libc++] Remove the special logic for "noexcept iterators" in basic_string.
This reverts a large chunk of http://reviews.llvm.org/D15862 ,
and also fixes bugs in `insert`, `append`, and `assign`, which are now regression-tested.
(Thanks to Tim Song for pointing out the bug in `append`!)

Before this patch, we did a special dance in `append`, `assign`, and `insert`
(but not `replace`). All of these require the strong exception guarantee,
even when the user-provided InputIterator might have throwing operations.

The naive way to accomplish this is to construct a temporary string and
then append/assign/insert from the temporary; i.e., finish all the potentially
throwing and self-inspecting InputIterator operations *before* starting to
modify self. But this is slow, so we'd like to skip it when possible.

The old code (D15682) attempted to check that specific iterator operations
were nothrow: it assumed that if the iterator operations didn't throw, then
it was safe to iterate the input range multiple times and therefore it was
safe to use the fast-path non-naive version. This was wrong for two reasons:
(1) the old code checked the wrong operations (e.g. checked noexceptness of `==`,
but the code that ran used `!=`), and (2) the conversion of value_type to char
could still throw, or inspect the contents of self.

The new code is much simpler, although still much more complicated than it
really could be. We'll likely revisit this codepath at some point, but for now
this patch suffices to get it passing all the new regression tests.

The added tests all fail before this patch, and succeed afterward.
See https://quuxplusone.github.io/blog/2021/04/17/pathological-string-appends/

Differential Revision: https://reviews.llvm.org/D98573
2021-04-26 16:22:43 -04:00
Arthur O'Dwyer 5c40c994c3 [libc++] s/_LIBCPP_NO_HAS_CHAR8_T/_LIBCPP_HAS_NO_CHAR8_T/g
This was raised in D94511.

Differential Revision: https://reviews.llvm.org/D100736
2021-04-21 12:49:07 -04:00
Louis Dionne 4cd6ca102a [libc++] NFC: Normalize `#endif //` comment indentation 2021-04-20 12:03:32 -04:00
Arthur O'Dwyer 2d0f1fa472 [libc++] Header inclusion tests.
As mandated by the Standard's various synopses, e.g. [iterator.synopsis].
Searching the TeX source for '#include' is a good way to find all of these
mandates.

The new tests are all autogenerated by utils/generate_header_inclusion_tests.py.
I was SHOCKED by how many mandates there are, and how many of them
libc++ wasn't conforming with.

Differential Revision: https://reviews.llvm.org/D99309
2021-04-06 15:31:56 -04:00
Arthur O'Dwyer 199d2ebeed [libc++] Use _EnableIf and __iter_value_type consistently. NFCI.
Specifically, use these metafunctions consistently in areas that are
about to be affected by P1518R2's changes.

This is the NFCI part of https://reviews.llvm.org/D97742 .
The functional-change part is still waiting for P1518R2 to be
officially merged into the working draft.
2021-03-29 09:22:52 -04:00
Marek Kurdej 28f82bec7f [libc++] [C++20] [P0482] Add missing tests and synopses for char8_t.
Left to finish P0482:
* <cuchar> header.
* Parts of <memory_resource> concerning char8_t. Also, tests for hash<pmr::*string>.

Reviewed By: ldionne, #libc, Quuxplusone

Differential Revision: https://reviews.llvm.org/D99184
2021-03-23 18:45:31 +01:00
Martin Storsjö c793f68d9b [libcxx] Don't use dllimport for a static member in a template
This fixes clang warnings (that are treated as errors when running
the test suite):

libcxx/include/string:4409:59: error: definition of dllimport static field [-Werror,-Wdllimport-static-field-def]
               basic_string<_CharT, _Traits, _Allocator>::npos;

The warning is normally not visible as long as the libc++ headers
are treated as system headers.

The same construct is always an error in MSVC.

(One _LIBCPP_FUNC_VIS was added in
2d8f23f571, which broke DLL builds.
59919c4d6b fixed this by adding another
_LIBCPP_FUNC_VIS on the declaration for consistency, but the underlying
issue remained, that one can't use dllimport here.)

Differential Revision: https://reviews.llvm.org/D97168
2021-03-04 08:55:27 +02:00
zoecarver dea74b2820 [libc++] Add `noexcept` to `string::find` and similar members.
Adds `noexcept` to `string_view`/`string::find` and similar members
(`rfind`, etc.). See discussion in D95251. Refs D95821.

Reviewed By: curdeius, ldionne

Differential Revision: https://reviews.llvm.org/D95848
2021-02-09 11:47:40 -08:00
Wim Leflere 6ac9cb2a7c [libc++][P1679] add string contains
C++23 string contains implementation and tests

Paper: https://wg21.link/P1679R3
Standard (string): https://eel.is/c++draft/string.contains
Standard (string_view): https://eel.is/c++draft/string.view.ops#lib:contains,basic_string_view

Differential Revision: https://reviews.llvm.org/D93912
2021-01-19 14:35:07 -05:00
Marek Kurdej 044b892c79 [libc++] Use c++20 instead of c++2a consistently.
* The only exception is that the flag -std=c++2a is still used not to break compatibility with older compilers (clang <= 9, gcc <= 9).
* Bump _LIBCPP_STD_VER for C++20 to 20 and use 21 for the future standard (C++2b).

That's a preparation step to add c++2b support to libc++.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D93383
2021-01-07 13:11:33 +01:00
Arthur O'Dwyer 3696227c10 [libc++] ADL-proof by adding _VSTD:: qualifications to memmove etc.
Generally these calls aren't vulnerable to ADL because they involve only
primitive types. The ones in <list> and <vector> drag in namespace std
but that's OK; the ones in <fstream> and <strstream> are vulnerable
iff `CharT` is an enum type, which seems far-fetched.
But absolutely zero of them *need* ADL to happen; so in my opinion
they should all be consistently qualified, just like calls to any
other (non-user-customizable) functions in namespace std.

Also: Include <cstring> and <cwchar> in <__string>.
We seemed to be getting lucky that <memory> included <iterator>
included <iosfwd> included <wchar.h>. That gave us the
global-namespace `wmemmove`, but not `_VSTD::wmemmove`.
This is now fixed.

I didn't touch these headers:
<ext/__hash> uses strlen, safely
<support/ibm/locale_mgmt_aix.h> uses memcpy, safely
<string.h> uses memchr and strchr, safely
<wchar.h> uses wcschr, safely
<__bsd_locale_fallbacks.h> uses wcsnrtombs, safely

Differential Revision: https://reviews.llvm.org/D93061
2020-12-10 22:03:12 -05:00
Richard Smith 2a2c228c7a Add new 'preferred_name' attribute.
This attribute permits a typedef to be associated with a class template
specialization as a preferred way of naming that class template
specialization. This permits us to specify that (for example) the
preferred way to express 'std::basic_string<char>' is as 'std::string'.

The attribute is applied to the various class templates in libc++ that have
corresponding well-known typedef names.

This is a re-commit. The previous commit was reverted because it exposed
a pre-existing bug that has since been fixed / worked around; see
PR48434.

Differential Revision: https://reviews.llvm.org/D91311
2020-12-09 12:22:35 -08:00
Richard Smith a1344779ab Revert "Add new 'preferred_name' attribute."
This change exposed a pre-existing issue with deserialization cycles
caused by a combination of attributes and template instantiations
violating the deserialization ordering restrictions; see PR48434 for
details.

A previous commit attempted to work around PR48434, but appears to have
only been a partial fix, and fixing this properly seems non-trivial.
Backing out for now to unblock things.

This reverts commit 98f76adf4e and
commit a64c26a47a.
2020-12-08 00:42:48 -08:00
Richard Smith 98f76adf4e Add new 'preferred_name' attribute.
This attribute permits a typedef to be associated with a class template
specialization as a preferred way of naming that class template
specialization. This permits us to specify that (for example) the
preferred way to express 'std::basic_string<char>' is as 'std::string'.

The attribute is applied to the various class templates in libc++ that have
corresponding well-known typedef names.

Differential Revision: https://reviews.llvm.org/D91311
2020-12-07 12:53:07 -08:00
Bruce Mitchener 527a7fdfbd [libc++] Replace several uses of 0 by nullptr
Differential Revision: https://reviews.llvm.org/D43159
2020-11-27 10:00:21 -05:00
Marek Kurdej 841132efda [libc++] [P0966] [C++20] Fix bug PR45368 by correctly implementing P0966: string::reserve should not shrink.
This patch fixes the implementation as well as the tests that didn't actually test the wanted behaviour.
You'll find all the details in the bug report.
It adds as well deprecation warning for reserve() (without argument) and adds a test.

http://wg21.link/P0966R1
https://bugs.llvm.org/show_bug.cgi?id=45368
https://reviews.llvm.org/D54992

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D91778
2020-11-26 10:13:12 +01:00
Arthur O'Dwyer 6e965df605 Revert "Revert "[libc++] ADL-proof <vector> by adding _VSTD:: qualification on calls.""
This reverts commit 620adacf87.

Fix: unsupport C++03 for the new test, define helpers before __swap_allocator

(1) Add _VSTD:: qualification to __swap_allocator.

(2) Add _VSTD:: qualification consistently to __to_address.

(3) Add some more missing _VSTD:: to <vector>, with a regression test.
This part is cleanup after d9a4f936d0.

Note that a vector whose allocator actually runs afoul of any of these ADL calls will
likely also run afoul of simple things like `v1 == v2` (which is also an ADL call).
But, still, libc++ should be consistent in qualifying function calls wherever possible.

Relevant blog post: https://quuxplusone.github.io/blog/2019/09/26/uglification-doesnt-stop-adl/

Differential Revision: https://reviews.llvm.org/D91708
2020-11-20 20:59:18 -05:00
Mikhail Goncharov 620adacf87 Revert "[libc++] ADL-proof <vector> by adding _VSTD:: qualification on calls."
This reverts commit 40267cc989.

Build fails, e.g. http://lab.llvm.org:8011/#/builders/23/builds/108
2020-11-19 15:36:49 +01:00
Arthur O'Dwyer 40267cc989 [libc++] ADL-proof <vector> by adding _VSTD:: qualification on calls.
(1) Add _VSTD:: qualification to __swap_allocator.

(2) Add _VSTD:: qualification consistently to __to_address.

(3) Add some more missing _VSTD:: to <vector>, with a regression test.
This part is cleanup after d9a4f936d0.

Note that a vector whose allocator actually runs afoul of any of these ADL calls will
likely also run afoul of simple things like `v1 == v2` (which is also an ADL call).
But, still, libc++ should be consistent in qualifying function calls wherever possible.

Relevant blog post: https://quuxplusone.github.io/blog/2019/09/26/uglification-doesnt-stop-adl/

Differential Revision: https://reviews.llvm.org/D91708
2020-11-19 09:19:16 -05:00
Louis Dionne 602c193e2a [libc++] Make sure __clear_and_shrink() maintains string invariants
__clear_and_shrink() was added in D41976, and a test was added alongside
it to make sure that the string invariants were maintained. However, it
appears that the test never ran under UBSan before, which would have
highlighted the fact that it doesn't actually maintain the string
invariants.

Differential Revision: https://reviews.llvm.org/D88849
2020-10-07 09:16:59 -04:00
Louis Dionne 31e820378b [libc++] NFCI: Simplify macro definitions for the debug mode
The debug mode always had three possibilities:
- _LIBCPP_DEBUG is undefined => no assertions
- _LIBCPP_DEBUG == 0         => some assertions
- _LIBCPP_DEBUG == 1         => some assertions + iterator checks

This was documented that way, however the code did not make this clear
at all. The discrepancy between _LIBCPP_DEBUG and _LIBCPP_DEBUG_LEVEL
was especially confusing. I reworked how the various macros are defined
without changing anything else to make the code clearer.
2020-10-02 15:11:23 -04:00
Martijn Vels 2bad222680 Add optimization to basic_string::assign for compile-time known constant values.
Summary:
This change optimizes the assign() methods for string where either the contents or lengths are compile time known constants. For small strings (< min_cap) we can execute the assignment entirely inline. For strings up to 128 bytes we allow the compiler to efficiently inline the copy operation after we call the offline __resize<>() method. Short / long branches are taken at the call site for better branch prediction and allowing FDO optimizations.

Benchmarks (unstable / google perflab):
```
name                                                old time/op             new time/op             delta
BM_StringAssignAsciiz_Empty_Opaque                  5.69ns ± 7%             5.97ns ± 7%     ~             (p=0.056 n=5+5)
BM_StringAssignAsciiz_Empty_Transparent             5.39ns ± 7%             0.79ns ± 8%  -85.36%          (p=0.008 n=5+5)
BM_StringAssignAsciiz_Small_Opaque                  11.2ns ± 5%             11.0ns ± 6%     ~             (p=0.548 n=5+5)
BM_StringAssignAsciiz_Small_Transparent             10.1ns ± 7%              1.0ns ± 8%  -89.76%          (p=0.008 n=5+5)
BM_StringAssignAsciiz_Large_Opaque                  23.5ns ± 7%             23.8ns ± 7%     ~             (p=0.841 n=5+5)
BM_StringAssignAsciiz_Large_Transparent             21.4ns ± 7%             12.7ns ± 7%  -40.83%          (p=0.008 n=5+5)
BM_StringAssignAsciiz_Huge_Opaque                    336ns ± 4%              327ns ± 7%     ~             (p=0.421 n=5+5)
BM_StringAssignAsciiz_Huge_Transparent               331ns ± 5%              324ns ± 7%     ~             (p=0.548 n=5+5)
BM_StringAssignAsciizMix_Opaque                     13.6ns ±10%             13.7ns ± 9%     ~             (p=0.690 n=5+5)
BM_StringAssignAsciizMix_Transparent                12.9ns ± 8%              3.6ns ± 8%  -71.82%          (p=0.008 n=5+5)
```

Reviewers: EricWF, #libc!

Subscribers: jfb, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D82221
2020-06-29 14:34:34 -04:00
Marcel Hlopko c9e6519d15 Remove unused _LIBCPP_RAW_ITERATORS
Summary: This change removes seemingly unused _LIBCPP_RAW_ITERATORS.

Reviewers: #libc, EricWF, ldionne

Reviewed By: #libc, EricWF, ldionne

Subscribers: dexonsmith, ldionne, gribozavr2, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D79323
2020-05-07 22:26:15 +02:00
Marek Kurdej 3e895085de [libc++][P1115][C++20] Improving the Return Value of Erase-Like Algorithms II: Free erase/erase if.
Summary:
This patch adds return type to std::erase and std::erase_if functions.

Also:
* Update __cpp_lib_erase_if to 202002L.
* Fix synopsis in unordered_map.
* Fix generate_feature_test_macro_components.py script.

Reviewers: EricWF, mclow.lists, ldionne, #libc

Reviewed By: ldionne, #libc

Subscribers: broadwaylamb, zoecarver, dexonsmith, ldionne, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D75905
2020-05-02 14:04:50 +02:00
Martijn Vels 7ba045a430 Make basic_string::operator=() tail call properly
Summary: We discovered that the compiler may chose not to inline the operator=, which leads to an expensive extra stack frame. This change makes __assign_no_alias always tail called.

Reviewers: EricWF, #libc!

Subscribers: libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D77913
2020-04-10 19:41:46 -04:00
Martijn Vels b019c5c037 Partially inline basic_string copy constructor in UNSTABLE
Summary:
    This is a recommit of https://reviews.llvm.org/D73223 where the added function accidentally ended up inside an idef block.

    This change splits the copy constructor up inlining short initialization, and explicitly outlining long initialization into __init_copy_ctor_external() which is the externally instantiated slow path.

    For unstable ABI, this has the following changes:

    remove basic_string(const basic_string&)
    remove basic_string(const basic_string&, const Allocator&)
    add __init_copy_ctor_external(const value_type*, size_type)
    Quick local benchmark for Copy:

    Master
    ```
    ---------------------------------------------------------------
    Benchmark                    Time             CPU   Iterations
    ---------------------------------------------------------------
    BM_StringCopy_Empty       3.50 ns         3.51 ns    199326720
    BM_StringCopy_Small       3.50 ns         3.51 ns    199510016
    BM_StringCopy_Large       15.7 ns         15.7 ns     45230080
    BM_StringCopy_Huge        1503 ns         1503 ns       464896
    ```
    With this change
    ```
    ---------------------------------------------------------------
    Benchmark                    Time             CPU   Iterations
    ---------------------------------------------------------------
    BM_StringCopy_Empty       1.99 ns         2.00 ns    356471808
    BM_StringCopy_Small       3.29 ns         3.30 ns    203425792
    BM_StringCopy_Large       13.3 ns         13.3 ns     52948992
    BM_StringCopy_Huge        1472 ns         1472 ns       475136
    ```

    Subscribers: libcxx-commits

    Tags: #libc

    Differential Revision: https://reviews.llvm.org/D75639
2020-03-04 17:52:46 -05:00
Martijn Vels 3712edb152 Revert "Partially inline basic_string copy constructor in UNSTABLE"
This reverts commit 8cf76e913b.

We are investigating why this causes compilation issues under -O3
2020-03-04 14:52:17 -05:00
Eric Fiselier 5891e7302f [libc++] Move std::string extern template declarations to end of class.
They need to appear before any member functions are ODR used, because
they change the visibility of many of these functions and otherwise
they could end up with hidden visibility in the DSO.
2020-03-04 13:57:29 -05:00
Martijn Vels 8cf76e913b Partially inline basic_string copy constructor in UNSTABLE
his change splits the copy constructor up inlining short initialization, and explicitly outlining long initialization into __init_copy_ctor_external() which is the externally instantiated slow path.

For unstable ABI, this has the following changes:

remove basic_string(const basic_string&)
remove basic_string(const basic_string&, const Allocator&)
add __init_copy_ctor_external(const value_type*, size_type)
Quick local benchmark for Copy:

Master
```
---------------------------------------------------------------
Benchmark                    Time             CPU   Iterations
---------------------------------------------------------------
BM_StringCopy_Empty       3.50 ns         3.51 ns    199326720
BM_StringCopy_Small       3.50 ns         3.51 ns    199510016
BM_StringCopy_Large       15.7 ns         15.7 ns     45230080
BM_StringCopy_Huge        1503 ns         1503 ns       464896
```

```
---------------------------------------------------------------
Benchmark                    Time             CPU   Iterations
---------------------------------------------------------------
BM_StringCopy_Empty       1.99 ns         2.00 ns    356471808
BM_StringCopy_Small       3.29 ns         3.30 ns    203425792
BM_StringCopy_Large       13.3 ns         13.3 ns     52948992
BM_StringCopy_Huge        1472 ns         1472 ns       475136
```

Author: Martijn Vels <martijn.vels@gmail.com>

Reviewers: EricWF, mclow.list

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D73223
2020-03-03 17:49:25 -05:00
Martijn Vels 675326466b Add flag _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATIONS for basic_string ABI
Summary: This review is a mostly trivial change to use an explicit ABI flag for the unstable external template list. This follows the practice for an ABI flag per feature, and provides a spot for the rational / motivation for the flag.

Reviewers: EricWF, ldionne

Subscribers: dexonsmith, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D75457
2020-03-02 10:26:37 -05:00
Martijn Vels f87d30cba2 Partially inline basic_string::operator=(const basic_string&)
Summary:
This change partially inlines operator=(const basic_string&) where both the input and current instance are short strings, making the assignment a fixed length inlined memcpy.

Assignments where either of the strings are long are delegate to __assign_no_alias<__is_short>(), which is templated for the long / short branch already observed in the caller.

Stable:
```
--------------------------------------------------------------------------------
Benchmark                                     Time             CPU   Iterations
--------------------------------------------------------------------------------
BM_StringAssignStr_Empty_Opaque            2.65 ns         2.66 ns    263745536
BM_StringAssignStr_Empty_Transparent       2.95 ns         2.96 ns    236494848
BM_StringAssignStr_Small_Opaque            2.93 ns         2.94 ns    237301760
BM_StringAssignStr_Small_Transparent       2.69 ns         2.69 ns    265809920
BM_StringAssignStr_Large_Opaque            19.6 ns         19.6 ns     35573760
BM_StringAssignStr_Large_Transparent       19.1 ns         19.1 ns     36716544
BM_StringAssignStr_Huge_Opaque             1901 ns         1901 ns       364544
BM_StringAssignStr_Huge_Transparent        1889 ns         1889 ns       360448
```

Unstable
```
--------------------------------------------------------------------------------
Benchmark                                     Time             CPU   Iterations
--------------------------------------------------------------------------------
BM_StringAssignStr_Empty_Opaque            1.29 ns         1.29 ns    540454912
BM_StringAssignStr_Empty_Transparent       1.11 ns         1.12 ns    628482048
BM_StringAssignStr_Small_Opaque            1.29 ns         1.29 ns    541216768
BM_StringAssignStr_Small_Transparent       1.11 ns         1.11 ns    629469184
BM_StringAssignStr_Large_Opaque            15.6 ns         15.6 ns     44945408
BM_StringAssignStr_Large_Transparent       14.9 ns         14.9 ns     46764032
BM_StringAssignStr_Huge_Opaque             1713 ns         1713 ns       401408
BM_StringAssignStr_Huge_Transparent        1704 ns         1704 ns       397312

```

Subscribers: libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D75211
2020-03-02 09:58:11 -05:00
Martijn Vels d260ea7199 Inline basic_string::erase for fastpath where __n == npos
Summary:
This change checks for the case where people want to erase a string to the end, i.e., __n == npos, and inlines the call if so.

This also demonstrates keeping the ABI intact for V1, but inlining the erase() method for unstable.

Reviewers: EricWF, mclow.lists, ldionne

Reviewed By: EricWF, ldionne

Subscribers: smeenai, dexonsmith, christof, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D73743
2020-02-26 13:37:45 -05:00
Martijn Vels d8969a1cb9 Split _LIBCPP_STRING_EXTERN_TEMPLATE_LIST up into a V1 and UNSTABLE version.
This change splits the _LIBCPP_STRING_EXTERN_TEMPLATE_LIST up into a _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST containing the stable ABI, and a _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST containing the unstable ABI.

The purpose is to explicitly define and maintain the two lists, where the unstable ABI allows for ABI breaking changes for purposes such as optimization while offering a strong guarantee that any change inside the unstable ABI does not affect the stable ABI.

As per the comment in the __string header, we do still allow etries to be added to the stable ABI list as the c++ versions and corresponding c++ std API changes.
2020-02-20 23:21:14 -05:00
Martijn Vels 282b803b62 White space only change: reflow a comment in basic_string
Summary: This change reflows a comment line. This change serves as a no-op test commit

Reviewers: mclow.lists, ldionne, EricWF

Subscribers: dexonsmith, christof, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D73552
2020-01-30 19:55:48 -05:00
Eric Fiselier fa40b41168 Revert "[libc++] Optimize / partially inline basic_string copy constructor"
This reverts commit a8a9c8e0a1.

There are multiple reported failures caused by this change.
Each failure is really weird, but it makes sense to revert
while investigating.
2020-01-20 21:41:58 -05:00
Eric Fiselier a8a9c8e0a1 [libc++] Optimize / partially inline basic_string copy constructor
Splits copy constructor up inlining short initialization, outlining long
initialization into __init_long() which is the externally instantiated slow
path initialization.

Subsequently changing the copy ctor to be inlined (not externally instantiated)
provides significant speed ups for short string initialization.

Generated code given:

void StringCopyCtor(void* mem, const std::string& s) {
    std::string*p = new(mem) std::string{s};
}

asm:
        cmp     byte ptr [rsi + 23], 0
        js      .LBB0_2
        mov     rax, qword ptr [rsi + 16]
        mov     qword ptr [rdi + 16], rax
        movups  xmm0, xmmword ptr [rsi]
        movups  xmmword ptr [rdi], xmm0
        ret
.LBB0_2:
        jmp     std::basic_string::__init_long # TAILCALL

Benchmark:
BM_StringCopy_Empty                                           5.19ns ± 6%             1.50ns ± 8%  -71.02%        (p=0.000 n=10+10)
BM_StringCopy_Small                                           5.14ns ± 8%             1.53ns ± 7%  -70.17%        (p=0.000 n=10+10)
BM_StringCopy_Large                                           18.9ns ± 0%             19.3ns ± 0%   +1.92%        (p=0.000 n=10+10)
BM_StringCopy_Huge                                             309ns ± 1%              316ns ± 5%     ~            (p=0.633 n=8+10)

Patch from Martijn Vels (mvels@google.com)
Reviewed as D72160.
2020-01-17 16:53:54 -05:00
Eric Fiselier 59919c4d6b [libc++] Fix Windows DLL build for string.
We need to mark string::npos with _LIBCPP_FUNC_VIS on the first
in-class declaration, otherwise it might get ignored
2020-01-16 15:01:12 -05:00
Eric Fiselier 313d89724c [libc++] Fix parsing <string> in C++03.
Specifically, add a space between >> when closing templates.
2020-01-15 17:29:55 -05:00
Eric Fiselier 58c7fa5ade [libc++] Optimize basic_string::operator=(const basic_string&) for SSO assignments
This change optimizes the operator=() assignment for short strings by direcly
copying the raw data from the source into the current instance. This creates an
optimized / inlined mempcy up to over 2X faster for short string assignments.
With inlining enabled for operator=, performance is up to 6X faster.

Benchmarks 'as is':
name                                    old time/op   new time/op    delta
BM_StringAssignStr_Empty_Opaque         6.05ns ± 2%   3.59ns ± 0%  -40.67%
BM_StringAssignStr_Empty_Transparent    5.15ns ± 0%   3.08ns ± 0%  -40.12%
BM_StringAssignStr_Small_Opaque         7.71ns ± 0%   3.59ns ± 0%  -53.45%
BM_StringAssignStr_Small_Transparent    7.66ns ± 0%   3.09ns ± 0%  -59.66%
BM_StringAssignStr_Large_Opaque         24.1ns ± 0%   24.9ns ± 0%   +3.22%
BM_StringAssignStr_Large_Transparent    22.2ns ± 0%   22.8ns ± 0%   +2.77%
BM_StringAssignStr_Huge_Opaque           315ns ± 6%    320ns ± 5%     ~
BM_StringAssignStr_Huge_Transparent      318ns ± 5%    321ns ± 4%     ~

Benchmarks with partial inlining operator=():
name                                    old time/op   new time/op    delta
BM_StringAssignStr_Empty_Opaque         5.94ns ± 2%   1.95ns ± 0%  -67.21%
BM_StringAssignStr_Empty_Transparent    5.14ns ± 0%   1.04ns ± 1%  -79.73%
BM_StringAssignStr_Small_Opaque         7.69ns ± 0%   1.96ns ± 0%  -74.48%
BM_StringAssignStr_Small_Transparent    7.65ns ± 0%   1.04ns ± 0%  -86.40%
BM_StringAssignStr_Large_Opaque         24.1ns ± 0%   24.5ns ± 0%   +1.61%
BM_StringAssignStr_Large_Transparent    22.2ns ± 0%   21.1ns ± 0%   -4.70%
BM_StringAssignStr_Huge_Opaque           317ns ± 5%    323ns ± 4%     ~
BM_StringAssignStr_Huge_Transparent      318ns ± 5%    320ns ± 5%     ~

Patch by Martijn Vels (mvels@google.com)
Reviewed as https://reviews.llvm.org/D72704
2020-01-15 17:27:10 -05:00
Eric Fiselier 288a143639 [libc++] Explicitly enumerate std::string external instantiations - Attempt 2
The GCC build failures have been addressed, and the LLDB failures were
  fixed by LLDB.

   I have also verified that the apple-clang 9.0 segfault no longer
   occurs.

Original Message:

 The external instantiation of std::string is a problem for libc++.
    Additions and removals of inline functions in string can cause ABI
    breakages, including introducing new symbols.

    This patch aims to:
      (1) Make clear which functions are explicitly instatiated.
      (2) Prevent new functions from being accidentally instantiated.
      (3) Allow a migration path for adding or removing functions from the
      explicit instantiation over time.

    Although this new formulation is uglier, it is preferable from a
    maintainability and readability standpoint because it explicitly
    enumerates the functions we've chosen to expose in our ABI. Changing
    this list is non-trivial and requires thought and planning.

    (3) is achieved by making it possible to control the extern template declaration
    separately from it's definition. Meaning we could add a new definition to
    the dylib, wait for it to roll out, then add the extern template
    declaration to the header. Similarly, we could remove existing extern
    template declarations while still keeping the definition to prevent ABI
    breakages.
2020-01-15 17:12:49 -05:00
Eric Fiselier 2d8f23f571 [libc++] Explicitly mark basic_string<...>::npos with default
visibility.

This ensures that the version compiled into the library isn't
accidentally hidden.
2020-01-15 17:02:17 -05:00
Eric Fiselier 24d2a015ea [libc++] Make SFINAE'd member functions in string mutually exclusive.
This patch is needed in order to work around a GCC bug that fails to
explicitly instantiate a non-template function of a class template when
there is another overload that's a function template.
(See https://godbolt.org/z/4bUQ_b)

This patch SFINAE's away the function templates when the argument is
a basic_string.
2020-01-15 17:00:26 -05:00
Oliver Stannard 6a634a5dba Revert "[libc++] Explicitly enumerate std::string external instantiations."
This is causing failures for multiple buildbots and bootstrap builds,
details at https://reviews.llvm.org/rG61bd1920.

This reverts commit 61bd19206f.
2020-01-13 13:54:04 +00:00
Eric Fiselier 61bd19206f [libc++] Explicitly enumerate std::string external instantiations.
The external instantiation of std::string is a problem for libc++.
    Additions and removals of inline functions in string can cause ABI
    breakages, including introducing new symbols.

    This patch aims to:
      (1) Make clear which functions are explicitly instatiated.
      (2) Prevent new functions from being accidentally instantiated.
      (3) Allow a migration path for adding or removing functions from the
      explicit instantiation over time.

    Although this new formulation is uglier, it is preferable from a
    maintainability and readability standpoint because it explicitly
    enumerates the functions we've chosen to expose in our ABI. Changing
    this list is non-trivial and requires thought and planning.

    (3) is achieved by making it possible to control the extern template declaration
    separately from it's definition. Meaning we could add a new definition to
    the dylib, wait for it to roll out, then add the extern template
    declaration to the header. Similarly, we could remove existing extern
    template declarations while still keeping the definition to prevent ABI
    breakages.
2020-01-09 15:51:02 -05:00
Eric Fiselier a53534a9f6 [libc++] Add __default_init_tag to basic_string constructors
This removes unneeded zero initialization of string data.

For example, given the below code:

void Init(void *mem) {
    new (mem) std::string("Hello World");
}

Assembly before:

Init(void*):
        xorps   xmm0, xmm0
        movups  xmmword ptr [rdi], xmm0
        mov     qword ptr [rdi + 16], 0
        mov     byte ptr [rdi], 22
        movabs  rax, 8022916924116329800
        mov     qword ptr [rdi + 1], rax
        mov     dword ptr [rdi + 8], 1684828783
        mov     byte ptr [rdi + 12], 0
        ret

Assembly after:

Init():
        mov     byte ptr [rdi], 22
        movabs  rax, 8022916924116329800
        mov     qword ptr [rdi + 1], rax
        mov     dword ptr [rdi + 8], 1684828783
        mov     byte ptr [rdi + 12], 0
        ret

Patch by Martijn Vels (mvels@google.com)
Reviewed as https://reviews.llvm.org/D70621
2019-12-16 19:04:09 -05:00
Eric Fiselier 549545b64a [libc++] Rework compressed pair constructors.
This patch de-duplicates most compressed pair constructors
to use the same code in C++11 and C++03.

Part of doing that is deleting the "__second_tag()" and replacing
it with a "__value_init_tag()" which has the same effect, but
allows for the removal of the special "one-arg" first element
constructor.

This patch is intended to have no semantic change.
2019-12-16 18:38:58 -05:00
marshall 703c26f03b Optimize and fix basic_string move assignment operator. Reviewed as https://reviews.llvm.org/D68623. Thanks to mvels for the patch. 2019-11-27 07:13:32 -08:00
Eric Fiselier f82dba0192 Rename __is_foo_iterator traits to reflect their Cpp17 nature.
With the upcoming introduction of iterator concepts in ranges,
the meaning of "__is_contiguous_iterator" changes drastically.

Currently we intend it to mean "does it have this iterator category",
but it could now also mean "does it meet the requirements of this
concept", and these can be different.
2019-11-18 01:49:32 -05:00
Eric Fiselier 0068c59139 [libc++] Rename __to_raw_pointer to __to_address.
This function has the same behavior as the now-standand std::to_address.
Re-using the name makes the behavior more clear, and in the future it
will allow us to correctly get the raw pointer for user provided pointer
types.
2019-11-16 17:16:09 -05:00