public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient
@ 2021-04-01 19:25 terra at gnome dot org
  2021-04-01 20:39 ` [Bug libstdc++/99876] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: terra at gnome dot org @ 2021-04-01 19:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

            Bug ID: 99876
           Summary: std::filesystem::absolute is inefficient
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: terra at gnome dot org
  Target Milestone: ---

Created attachment 50498
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50498&action=edit
Preprocesses source code

The documentation for std::filesystem::absolute states:

"For POSIX-based operating systems, std::filesystem::absolute(p) is equivalent
to std::filesystem::current_path() / p except for when p is the empty path. "

g++ implements it is way -- that is correct, but wasteful.

If the given filename is already absolute, it should be simply returned.
There is no need to call current_path() which leads to a getcwd syscall.





# /usr/local/products/gcc/10.1.0/bin/g++ -std=gnu++17 -Wall -O2 ttt.C

# strace ./a.out 2>&1 | tail
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
getcwd("/work/nova7/23232/src", 4096)   = 22
exit_group(0)                           = ?
+++ exited with 0 +++

# cat ttt.C
#include <filesystem>

int
main()
{
  std::filesystem::path foo ("/home/welinder");

  for (int i = 0; i < 10000; i++)
    (void)std::filesystem::absolute(foo);
}

# uname -a
Linux monsterd03 5.3.18-lp152.66-default #1 SMP Tue Mar 2 13:18:19 UTC 2021
(73933a3) x86_64 x86_64 x86_64 GNU/Linux

# /usr/local/products/gcc/10.1.0/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/usr/local/products/gcc/10.1.0/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/products/gcc/10.1.0/lib/gcc/x86_64-suse-linux/10.1.0/lto-wrapper
Target: x86_64-suse-linux
Configured with: ../../gcc-10.1.0/configure --enable-languages=c,c++,fortran
--enable-targets=x86_64-suse-linux,i686-suse-linux
--prefix=/usr/local/products/gcc/10.1.0 --with-gnu-as
--with-as=/usr/local/products/gcc/binutils-2.32/bin/as --with-gnu-ld
--with-ld=/usr/local/products/gcc/binutils-2.32/bin/ld --enable-threads=posix
--enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=pool
x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.1.0 (GCC)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
@ 2021-04-01 20:39 ` redi at gcc dot gnu.org
  2021-04-01 20:41 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-01 20:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-04-01
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to M Welinder from comment #0)
> If the given filename is already absolute, it should be simply returned.
> There is no need to call current_path() which leads to a getcwd syscall.

Yes we already do that for the overload with an error_code parameter:

fs::path
fs::absolute(const path& p, error_code& ec)
{
  path ret;
  if (p.empty())
    {
      ec = make_error_code(std::errc::invalid_argument);
      return ret;
    }
  ec.clear();
  if (p.is_absolute())
    {
      ret = p;
      return ret;
    }

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
  2021-04-01 20:39 ` [Bug libstdc++/99876] " redi at gcc dot gnu.org
@ 2021-04-01 20:41 ` redi at gcc dot gnu.org
  2021-04-19 10:40 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-01 20:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -65,19 +65,12 @@ namespace posix = std::filesystem::__gnu_posix;
 fs::path
 fs::absolute(const path& p)
 {
-#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
   error_code ec;
   path ret = absolute(p, ec);
   if (ec)
     _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot make absolute path", p,
                                             ec));
   return ret;
-#else
-  if (p.empty())
-    _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot make absolute path", p,
-         make_error_code(std::errc::invalid_argument)));
-  return current_path() / p;
-#endif
 }

 fs::path

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
  2021-04-01 20:39 ` [Bug libstdc++/99876] " redi at gcc dot gnu.org
  2021-04-01 20:41 ` redi at gcc dot gnu.org
@ 2021-04-19 10:40 ` redi at gcc dot gnu.org
  2021-08-28 11:02 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-19 10:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |NEW
           Assignee|redi at gcc dot gnu.org            |unassigned at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
                   ` (2 preceding siblings ...)
  2021-04-19 10:40 ` redi at gcc dot gnu.org
@ 2021-08-28 11:02 ` redi at gcc dot gnu.org
  2021-08-28 11:02 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-28 11:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2021-04-01 00:00:00         |2021-8-28
   Target Milestone|---                         |10.4
           Keywords|                            |missed-optimization

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
                   ` (3 preceding siblings ...)
  2021-08-28 11:02 ` redi at gcc dot gnu.org
@ 2021-08-28 11:02 ` redi at gcc dot gnu.org
  2021-08-28 12:07 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-28 11:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
                   ` (4 preceding siblings ...)
  2021-08-28 11:02 ` redi at gcc dot gnu.org
@ 2021-08-28 12:07 ` cvs-commit at gcc dot gnu.org
  2021-10-12 10:59 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-08-28 12:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:07b990ee23e0c7a92d362dbb25fd5d57d95eb8be

commit r12-3198-g07b990ee23e0c7a92d362dbb25fd5d57d95eb8be
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Aug 27 10:59:54 2021 +0100

    libstdc++: Fix inefficiency in filesystem::absolute [PR99876]

    When the path is already absolute, the call to current_path() is
    wasteful, because operator/ will ignore the left operand anyway.

    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>

    libstdc++-v3/ChangeLog:

            PR libstdc++/99876
            * src/c++17/fs_ops.cc (fs::absolute): Call non-throwing form,
            to avoid unnecessary current_path() call.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
                   ` (5 preceding siblings ...)
  2021-08-28 12:07 ` cvs-commit at gcc dot gnu.org
@ 2021-10-12 10:59 ` cvs-commit at gcc dot gnu.org
  2021-10-12 16:28 ` cvs-commit at gcc dot gnu.org
  2021-10-12 16:32 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-12 10:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:7df66a0c95a6b3678c73385969c1395a8aab2bd6

commit r11-9107-g7df66a0c95a6b3678c73385969c1395a8aab2bd6
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Aug 27 10:59:54 2021 +0100

    libstdc++: Fix inefficiency in filesystem::absolute [PR99876]

    When the path is already absolute, the call to current_path() is
    wasteful, because operator/ will ignore the left operand anyway.

    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>

    libstdc++-v3/ChangeLog:

            PR libstdc++/99876
            * src/c++17/fs_ops.cc (fs::absolute): Call non-throwing form,
            to avoid unnecessary current_path() call.

    (cherry picked from commit 07b990ee23e0c7a92d362dbb25fd5d57d95eb8be)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
                   ` (6 preceding siblings ...)
  2021-10-12 10:59 ` cvs-commit at gcc dot gnu.org
@ 2021-10-12 16:28 ` cvs-commit at gcc dot gnu.org
  2021-10-12 16:32 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-12 16:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:154316697dbea9ba96bc14680b642b3ae35dadbd

commit r10-10186-g154316697dbea9ba96bc14680b642b3ae35dadbd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Aug 27 10:59:54 2021 +0100

    libstdc++: Fix inefficiency in filesystem::absolute [PR99876]

    When the path is already absolute, the call to current_path() is
    wasteful, because operator/ will ignore the left operand anyway.

    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>

    libstdc++-v3/ChangeLog:

            PR libstdc++/99876
            * src/c++17/fs_ops.cc (fs::absolute): Call non-throwing form,
            to avoid unnecessary current_path() call.

    (cherry picked from commit 07b990ee23e0c7a92d362dbb25fd5d57d95eb8be)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/99876] std::filesystem::absolute is inefficient
  2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
                   ` (7 preceding siblings ...)
  2021-10-12 16:28 ` cvs-commit at gcc dot gnu.org
@ 2021-10-12 16:32 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-10-12 16:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99876

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 10.4 and 11.3 now.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-10-12 16:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01 19:25 [Bug libstdc++/99876] New: std::filesystem::absolute is inefficient terra at gnome dot org
2021-04-01 20:39 ` [Bug libstdc++/99876] " redi at gcc dot gnu.org
2021-04-01 20:41 ` redi at gcc dot gnu.org
2021-04-19 10:40 ` redi at gcc dot gnu.org
2021-08-28 11:02 ` redi at gcc dot gnu.org
2021-08-28 11:02 ` redi at gcc dot gnu.org
2021-08-28 12:07 ` cvs-commit at gcc dot gnu.org
2021-10-12 10:59 ` cvs-commit at gcc dot gnu.org
2021-10-12 16:28 ` cvs-commit at gcc dot gnu.org
2021-10-12 16:32 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).