diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2021-11-30 16:18:23 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2021-12-01 15:00:32 +0000 |
commit | e9089e4fa9f726405a83d8ce46baeda469ca1a1d (patch) | |
tree | 94f9d8c8e541db58a27bbbce6bbebb53e6e632f8 /gcc | |
parent | 1e625a44f6f3001cea31e0f7c563943ecba92b68 (diff) | |
download | gcc-e9089e4fa9f726405a83d8ce46baeda469ca1a1d.zip gcc-e9089e4fa9f726405a83d8ce46baeda469ca1a1d.tar.gz gcc-e9089e4fa9f726405a83d8ce46baeda469ca1a1d.tar.bz2 |
libstdc++: Avoid unwanted allocations in filesystem::path
When using COW strings, accessing _M_pathname[0] and similar non-const
accessors can cause the string to "leak", meaning it reallocates itself
if it shares ownership with another string object.
This causes test failures for --enable-fully-dynamic-string builds:
/home/jwakely/src/gcc/libstdc++-v3/testsuite/experimental/filesystem/path/construct/90634.cc:62: void test01(): Assertion 'bytes_allocated == 0' failed.
FAIL: experimental/filesystem/path/construct/90634.cc execution test
This FAIL happens because the fully-dynamic move constructor results in
shared ownership, so for path(std::move(std::string("foo"))) the
_M_pathname member shares ownership with the temporary, and the
non-const accesses in _M_split_cmpts() cause a new copy of the string to
be allocated. This un-sharing is wasteful, and entirely unnecessary when
sharing ownership with an rvalue that is about to release its ownership
anyway. Even for lvalues, sharing ownership is not a problem and
reallocating a unique copy of the string is wasteful.
This removes non-const accesses of _M_pathname in the
path::_M_split_cmpts() members.
libstdc++-v3/ChangeLog:
* src/c++17/fs_path.cc (path::_M_split_cmpts()): Remove
micro-optimization for "/" path.
* src/filesystem/path.cc (path::_M_split_cmpts()): Only access
the contents of _M_pathname using const member functions.
Diffstat (limited to 'gcc')
0 files changed, 0 insertions, 0 deletions