From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from zimbra.cs.ucla.edu (zimbra.cs.ucla.edu [131.179.128.68]) by sourceware.org (Postfix) with ESMTPS id 01BF53858C52 for ; Wed, 4 May 2022 00:40:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 01BF53858C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 19D191600B3; Tue, 3 May 2022 17:40:06 -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 bh1g1niXCX8v; Tue, 3 May 2022 17:40:05 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 5C0191600B7; Tue, 3 May 2022 17:40:05 -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 axypJg-zYXJ1; Tue, 3 May 2022 17:40:05 -0700 (PDT) Received: from [131.179.64.200] (Penguin.CS.UCLA.EDU [131.179.64.200]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 3F2871600B3; Tue, 3 May 2022 17:40:05 -0700 (PDT) Message-ID: <577d0656-8b38-07d8-7b48-01870d3730c7@cs.ucla.edu> Date: Tue, 3 May 2022 17:40:04 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH] stdio-common: Add the fgetln function Content-Language: en-US To: Florian Weimer References: <871qxbxe2i.fsf@oldenburg.str.redhat.com> From: Paul Eggert Organization: UCLA Computer Science Department Cc: libc-alpha@sourceware.org In-Reply-To: <871qxbxe2i.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 May 2022 00:40:08 -0000 If the stream is not already oriented, FreeBSD getln sets the stream to byte-orientation. Should glibc getln do the same? On 5/3/22 00:36, Florian Weimer via Libc-alpha wrote: > + /* Discard the old buffer. This optimizes for a buffered stream, > + with multiple lines in each buffer. */ > + if (fp->_fgetln_buf != NULL) > + { > + free (fp->_fgetln_buf); > + fp->_fgetln_buf = NULL; > + } Hope you don't mind a bit of bikeshedding here.... Why free the fgetln buffer eagerly? Instead, free it only when closing. That would lessen pressure on the memory allocator and would save a few insns in fgetln's usual case. Come to think of it, how about if we restrict fgetln to streams for which either (1) the user has not called setvbuf with a nonnull buffer, or (2) the input line fits in the user-supplied setvbuf buffer. Then we wouldn't need to worry about adding a _fgetln_buf slot, as fgetln could always return a pointer into the already-existing stdio buffer, possibly by enlarging the buffer in case (1). (In case (2) fgetln could fail with ENOMEM if the input line is longer than the user-supplied buffer.) This would suffice for 99.9% of applications and would be more efficient than what FreeBSD does, and the whole point of fgetln is low-level efficiency right?