From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8DAD6385C32C for ; Thu, 9 Jun 2022 07:37:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8DAD6385C32C Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-27-nPA3lzsuNqSfb0b4HTX8qg-1; Thu, 09 Jun 2022 03:37:49 -0400 X-MC-Unique: nPA3lzsuNqSfb0b4HTX8qg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AC5CB3C0F725; Thu, 9 Jun 2022 07:37:48 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.193.50]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D2D2D4619F4; Thu, 9 Jun 2022 07:37:47 +0000 (UTC) From: Florian Weimer To: Paul Eggert Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] stdio-common: Add the fgetln function References: <871qxbxe2i.fsf@oldenburg.str.redhat.com> <577d0656-8b38-07d8-7b48-01870d3730c7@cs.ucla.edu> Date: Thu, 09 Jun 2022 09:37:46 +0200 In-Reply-To: <577d0656-8b38-07d8-7b48-01870d3730c7@cs.ucla.edu> (Paul Eggert's message of "Tue, 3 May 2022 17:40:04 -0700") Message-ID: <87r13y5lth.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Thu, 09 Jun 2022 07:37:52 -0000 * Paul Eggert: > If the stream is not already oriented, FreeBSD getln sets the stream > to byte-orientation. Should glibc getln do the same? Our getdelim doesn't do that explicitly. > 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. The assumption is that very few lines cross buffer boundaries. > 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. It would require a layering violation as far as libio is concerned: a high-level function such as fgetln cannot reallocate the read buffer. You mention setvbuf, but there are probably other cases (and of course GCC 2.95 C++ classes, but we don't need to worry about compatibility with those, I think). > 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? I'm not sure if it's more efficient. The I/O block granularity would change depending on where lines end. Thanks, Florian