From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 604A61F461 for ; Fri, 5 Jul 2019 23:53:43 +0000 (UTC) Received: from localhost ([::1]:57030 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hjY1O-0008HW-1O for normalperson@yhbt.net; Fri, 05 Jul 2019 19:53:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43132) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hjY1G-0008HI-Qt for bug-gnulib@gnu.org; Fri, 05 Jul 2019 19:53:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hjY1E-0007sW-PU for bug-gnulib@gnu.org; Fri, 05 Jul 2019 19:53:34 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:57190) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hjY1C-0007od-Ab; Fri, 05 Jul 2019 19:53:30 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 0128F1626AC; Fri, 5 Jul 2019 16:53:24 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id bd5dw7J3YTAK; Fri, 5 Jul 2019 16:53:23 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id F35421626B8; Fri, 5 Jul 2019 16:53:22 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id gVnXVoGr8U_z; Fri, 5 Jul 2019 16:53:22 -0700 (PDT) Received: from [192.168.1.9] (cpe-23-242-74-103.socal.res.rr.com [23.242.74.103]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id B4F5C1626AC; Fri, 5 Jul 2019 16:53:22 -0700 (PDT) Subject: Re: [PATCH] stat: don't explicitly request file size for filenames To: =?UTF-8?Q?P=c3=a1draig_Brady?= References: <7AC7AA2A-EDEA-4A80-9A5A-02FAA2D823EF@whamcloud.com> <5a1b87fe-e265-6b0f-66eb-20ad1ba9c6e5@draigBrady.com> From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: <5f23c469-89b6-1280-a8a4-a9b0653f186a@cs.ucla.edu> Date: Fri, 5 Jul 2019 16:53:18 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <5a1b87fe-e265-6b0f-66eb-20ad1ba9c6e5@draigBrady.com> Content-Type: multipart/mixed; boundary="------------AB6B7ABE8173F909B792C83A" Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 131.179.128.68 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jeff Layton , bug-gnulib , Andreas Dilger , "coreutils@gnu.org" Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" This is a multi-part message in MIME format. --------------AB6B7ABE8173F909B792C83A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable P=C3=A1draig Brady wrote: > It would be nice to have areadlink_with_size treat 0 as auto select som= e lower bound. Yes, that sounds good. However, I didn't see why that would entail changi= ng=20 SYMLINK_MAX from 1024 to 1023, or why the patch would affect the document= ed API. How about the attached patch instead? When the guessed size is zero it ty= pically=20 avoids a realloc by using a small stack buffer. --------------AB6B7ABE8173F909B792C83A Content-Type: text/x-patch; name="0001-areadlink-with-size-improve-efficiency.patch" Content-Disposition: attachment; filename="0001-areadlink-with-size-improve-efficiency.patch" Content-Transfer-Encoding: quoted-printable >From d94bf537d7fdda13f4432bb60a98a8bd19d8e18d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 5 Jul 2019 16:48:22 -0700 Subject: [PATCH] areadlink-with-size: improve efficiency MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit If the guessed size is 0, guess 200 first, to avoid a sequence of small readlinks in the usual case. Reallocate at the end to the actual size, to avoid memory waste. Based on a suggestion by P=C3=A1draig Brady. * lib/areadlink-with-size.c (areadlink_with_size): Use a small stack buffer when the stated size is zero. * modules/areadlink-with-size (Depends-on): Add strdup-posix. --- ChangeLog | 11 +++++++++++ lib/areadlink-with-size.c | 26 ++++++++++++++++++++------ modules/areadlink-with-size | 1 + 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 98d5531b1..b07e26335 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2019-07-05 Paul Eggert + + areadlink-with-size: improve efficiency + If the guessed size is 0, guess 200 first, to avoid a sequence of + small readlinks in the usual case. Reallocate at the end to the + actual size, to avoid memory waste. Based on a suggestion by + P=C3=A1draig Brady. + * lib/areadlink-with-size.c (areadlink_with_size): + Use a small stack buffer when the stated size is zero. + * modules/areadlink-with-size (Depends-on): Add strdup-posix. + 2019-07-05 Bruno Haible =20 getcwd-lgpl, getcwd: Don't call realloc when it is pointless. diff --git a/lib/areadlink-with-size.c b/lib/areadlink-with-size.c index 364cc0858..9f1f7959f 100644 --- a/lib/areadlink-with-size.c +++ b/lib/areadlink-with-size.c @@ -60,18 +60,30 @@ areadlink_with_size (char const *file, size_t size) ? symlink_max + 1 : INITIAL_LIMIT_BOUND); =20 + /* Size of stack buffer for initial readlink when the link size hint + is zero. */ + enum { stackbuf_size =3D 200 }; + /* The initial buffer size for the link value. */ - size_t buf_size =3D size < initial_limit ? size + 1 : initial_limit; + size_t buf_size =3D (size =3D=3D 0 ? stackbuf_size + : size < initial_limit ? size + 1 : initial_limit); =20 while (1) { ssize_t r; size_t link_length; - char *buffer =3D malloc (buf_size); + char stackbuf[stackbuf_size]; + char *buf =3D stackbuf; + char *buffer =3D NULL; + + if (! (size =3D=3D 0 && buf_size =3D=3D stackbuf_size)) + { + buf =3D buffer =3D malloc (buf_size); + if (!buffer) + return NULL; + } =20 - if (buffer =3D=3D NULL) - return NULL; - r =3D readlink (file, buffer, buf_size); + r =3D readlink (file, buf, buf_size); link_length =3D r; =20 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1 @@ -86,7 +98,9 @@ areadlink_with_size (char const *file, size_t size) =20 if (link_length < buf_size) { - buffer[link_length] =3D 0; + buf[link_length] =3D 0; + if (!buffer) + return strdup (buf); /* Shrink BUFFER before returning it. */ if (link_length + 1 < buf_size) { diff --git a/modules/areadlink-with-size b/modules/areadlink-with-size index 82a902187..a4ab0e178 100644 --- a/modules/areadlink-with-size +++ b/modules/areadlink-with-size @@ -9,6 +9,7 @@ Depends-on: readlink ssize_t stdint +strdup-posix unistd =20 configure.ac: --=20 2.17.1 --------------AB6B7ABE8173F909B792C83A--