Parsing a complete string is more efficient than appending each
component one-by-one.
* src/c++17/fs_path.cc (path::parent_path()): Create whole path at
once instead of building it iteratively.
Tested powerpc64le-linux and x86_64-w64-mingw32, committed to trunk.
commit ed3d2a78e4bb6ee1c2b7483bd03d5a9747309edf
Author: Jonathan Wakely <[email protected]>
Date: Wed May 29 21:30:04 2019 +0100
Optimize filesystem::path::parent_path()
Parsing a complete string is more efficient than appending each
component one-by-one.
* src/c++17/fs_path.cc (path::parent_path()): Create whole path at
once instead of building it iteratively.
diff --git a/libstdc++-v3/src/c++17/fs_path.cc
b/libstdc++-v3/src/c++17/fs_path.cc
index 8e01bf510d3..c438ddc61fd 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;
}