diff mbox series

Micro-optimization to avoid creating temporary path

Message ID 20181218155200.GA8769@redhat.com
State New
Headers show
Series Micro-optimization to avoid creating temporary path | expand

Commit Message

Jonathan Wakely Dec. 18, 2018, 3:52 p.m. UTC
Now that path::operator/=(basic_string_view<value_type>) works directly
from the string argument, instead of constructing a temporary path from
the string, it's potentially more efficient to do 'path(x) /= s' instead
of 'x / s'. This changes the only relevant place in the library.

	* src/filesystem/std-dir.cc (filesystem::_Dir::advance): Append
	string to lvalue to avoid creating temporary path.

Tested x86_64-linux, committed to trunk.
commit e48b1b71c76d12f999d9cf4a12239845281a26e4
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Dec 18 15:39:12 2018 +0000

    Micro-optimization to avoid creating temporary path
    
    Now that path::operator/=(basic_string_view<value_type>) works directly
    from the string argument, instead of constructing a temporary path from
    the string, it's potentially more efficient to do 'path(x) /= s' instead
    of 'x / s'. This changes the only relevant place in the library.
    
            * src/filesystem/std-dir.cc (filesystem::_Dir::advance): Append
            string to lvalue to avoid creating temporary path.

Comments

Jonathan Wakely Dec. 18, 2018, 4:38 p.m. UTC | #1
On 18/12/18 15:52 +0000, Jonathan Wakely wrote:
>Now that path::operator/=(basic_string_view<value_type>) works directly
>from the string argument, instead of constructing a temporary path from
>the string, it's potentially more efficient to do 'path(x) /= s' instead
>of 'x / s'. This changes the only relevant place in the library.
>
>	* src/filesystem/std-dir.cc (filesystem::_Dir::advance): Append
>	string to lvalue to avoid creating temporary path.

This is only an optimization if it doesn't introduce a new copy! Fixed
by the attached patch.

Tested x86_64-linux, committed to trunk.
commit c2cccc34e8d8c9db216b96bffaba018f50ec7b75
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Dec 18 16:34:47 2018 +0000

    Fix previous commit to move instead of copying
    
            * src/filesystem/std-dir.cc (filesystem::_Dir::advance): Move new
            path instead of copying.

diff --git a/libstdc++-v3/src/filesystem/std-dir.cc b/libstdc++-v3/src/filesystem/std-dir.cc
index 216182a2e56..b0f869fc8fd 100644
--- a/libstdc++-v3/src/filesystem/std-dir.cc
+++ b/libstdc++-v3/src/filesystem/std-dir.cc
@@ -63,7 +63,7 @@ struct fs::_Dir : _Dir_base
       {
 	auto name = path;
 	name /= entp->d_name;
-	entry = fs::directory_entry{name, get_file_type(*entp)};
+	entry = fs::directory_entry{std::move(name), get_file_type(*entp)};
 	return true;
       }
     else if (!ec)
diff mbox series

Patch

diff --git a/libstdc++-v3/src/filesystem/std-dir.cc b/libstdc++-v3/src/filesystem/std-dir.cc
index 038f635a712..216182a2e56 100644
--- a/libstdc++-v3/src/filesystem/std-dir.cc
+++ b/libstdc++-v3/src/filesystem/std-dir.cc
@@ -61,7 +61,9 @@  struct fs::_Dir : _Dir_base
   {
     if (const auto entp = _Dir_base::advance(skip_permission_denied, ec))
       {
-	entry = fs::directory_entry{path / entp->d_name, get_file_type(*entp)};
+	auto name = path;
+	name /= entp->d_name;
+	entry = fs::directory_entry{name, get_file_type(*entp)};
 	return true;
       }
     else if (!ec)