From: Florian Weimer <fweimer@redhat.com>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] stdio-common: Add the fgetln function
Date: Thu, 09 Jun 2022 09:37:46 +0200 [thread overview]
Message-ID: <87r13y5lth.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <577d0656-8b38-07d8-7b48-01870d3730c7@cs.ucla.edu> (Paul Eggert's message of "Tue, 3 May 2022 17:40:04 -0700")
* 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
next prev parent reply other threads:[~2022-06-09 7:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-03 7:36 Florian Weimer
2022-05-03 8:06 ` Andreas Schwab
2022-05-03 8:31 ` Florian Weimer
2022-05-03 8:46 ` Andreas Schwab
2022-05-03 9:01 ` Florian Weimer
2022-05-03 9:10 ` Andreas Schwab
2022-05-03 10:45 ` Cristian Rodríguez
2022-05-04 0:40 ` Paul Eggert
2022-06-09 7:37 ` Florian Weimer [this message]
2022-06-09 20:08 ` Paul Eggert
2022-06-24 11:01 ` Florian Weimer
2022-06-24 20:35 ` Paul Eggert
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=87r13y5lth.fsf@oldenburg.str.redhat.com \
--to=fweimer@redhat.com \
--cc=eggert@cs.ucla.edu \
--cc=libc-alpha@sourceware.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).