public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "terra at gnome dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/96484] New: Horrible performance of std::read_symlink
Date: Wed, 05 Aug 2020 13:51:41 +0000	[thread overview]
Message-ID: <bug-96484-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 96484
           Summary: Horrible performance of std::read_symlink
           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 49004
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49004&action=edit
Preprocessed ttt.C

This program takes ~0.25s per round in the loop for a total of minutes.  It
should take millisecons in total.

To observe the problem, "foo" needs to be a large file -- more than 500M in my
case -- that is not a symlink.  Mine is on an NFS server.


#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int
main()
{
  for (int i = 0; i < 1000; i++) {
    std::error_code ec;
    std::cerr << i << ": " << fs::read_symlink ("foo", ec) << std::endl;
  }
}


The problem is in the implementation of read_symlink.  First an lstat call is
done (fine), but then the st_size field is used as a hint for the symlink's
size.  There is no check that the file is a symlink at this point, so in my
case a 500M buffer is allocated via an mmap system call and the kernel takes
its sweet time.

Note: a file with size 4G-1 on a 32-bit system might produce an infinite loop
as the buffer will have size 0 and get doubled from that.

Suggestion 1: if the lstat doesn't show a symlink, set the error code
right then and exit.

Suggestion 2: cap st_size at something reasonable and let the loop handle it if
it needs to be bigger.








# /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) 


# uname -a
Linux monsterd09 5.0.13-1-default #1 SMP Sun May 5 15:48:04 UTC 2019 (b11e2d7)
x86_64 x86_64 x86_64 GNU/Linux

             reply	other threads:[~2020-08-05 13:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-05 13:51 terra at gnome dot org [this message]
2020-08-05 15:20 ` [Bug libstdc++/96484] " redi at gcc dot gnu.org
2020-08-05 15:24 ` terra at gnome dot org
2020-08-06 17:48 ` cvs-commit at gcc dot gnu.org
2020-08-06 20:23 ` cvs-commit at gcc dot gnu.org
2020-08-06 20:24 ` cvs-commit at gcc dot gnu.org
2020-08-06 20:25 ` cvs-commit at gcc dot gnu.org
2020-08-06 20:26 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-96484-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).