From: Jonathan Wakely Date: Fri, 14 Jun 2019 18:11:17 +0000 (+0100) Subject: Optimize filesystem::path::parent_path() X-Git-Tag: releases/gcc-9.2.0~241 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=873557820764285a4b8da2d86333c26913449740;p=thirdparty%2Fgcc.git Optimize filesystem::path::parent_path() Parsing a complete string is more efficient than appending each component one-by-one. Backport from mainline 2019-05-29 Jonathan Wakely * src/c++17/fs_path.cc (path::parent_path()): Create whole path at once instead of building it iteratively. From-SVN: r272303 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7b46e22eca02..d4cb3be5e299 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2019-06-14 Jonathan Wakely + + Backport from mainline + 2019-05-29 Jonathan Wakely + + * src/c++17/fs_path.cc (path::parent_path()): Create whole path at + once instead of building it iteratively. + 2019-06-14 Jonathan Wakely Backport from mainline diff --git a/libstdc++-v3/src/c++17/fs_path.cc b/libstdc++-v3/src/c++17/fs_path.cc index 8e01bf510d3e..c438ddc61fd8 100644 --- a/libstdc++-v3/src/c++17/fs_path.cc +++ b/libstdc++-v3/src/c++17/fs_path.cc @@ -1523,11 +1523,9 @@ path::parent_path() const __ret = *this; else if (_M_cmpts.size() >= 2) { - for (auto __it = _M_cmpts.begin(), __end = std::prev(_M_cmpts.end()); - __it != __end; ++__it) - { - __ret /= *__it; - } + const auto parent = std::prev(_M_cmpts.end(), 2); + const auto len = parent->_M_pos + parent->_M_pathname.length(); + __ret.assign(_M_pathname.substr(0, len)); } return __ret; }