public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] stdio-common: Add the fgetln function
Date: Tue, 3 May 2022 17:40:04 -0700	[thread overview]
Message-ID: <577d0656-8b38-07d8-7b48-01870d3730c7@cs.ucla.edu> (raw)
In-Reply-To: <871qxbxe2i.fsf@oldenburg.str.redhat.com>

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?

  parent reply	other threads:[~2022-05-04  0:40 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 [this message]
2022-06-09  7:37   ` Florian Weimer
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=577d0656-8b38-07d8-7b48-01870d3730c7@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=fweimer@redhat.com \
    --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).