comp.lang.ada
 help / color / mirror / Atom feed
From: Natasha Kerensikova <lithiumcat@gmail.com>
Subject: Re: Reading the while standard input into a String
Date: Mon, 6 Jun 2011 10:08:01 +0000 (UTC)
Date: 2011-06-06T10:08:01+00:00	[thread overview]
Message-ID: <slrniup9o1.i18.lithiumcat@sigil.instinctive.eu> (raw)
In-Reply-To: e483abef-a9d3-40f2-90fd-66d8a1bb3f95@p13g2000yqh.googlegroups.com

Hello,

On 2011-06-06, Ludovic Brenta <ludovic@ludovic-brenta.org> wrote:
> Natasha Kerensikova wrote on comp.lang.ada:
>> I have encountered what was a very easily solved problem in C: dump the
>> whole contents of the standard input into some place in memory, in order
>> to process it afterwards.
>
> Personally I think this is a fundamentally bad idea.  Consider:
>
> $ my_program < /dev/random
>
> Do you consider it acceptable to use all memory and all swap space
> before finally bailing out with a Storage_Error and not doing any
> actual processing?

Actually, in principle, yes. I mean, I want to be able to write code
that behave that way as far as the "reading the whole standard input"
goes, because that's this part I'm trying to learn about.

Of course, I don't want a real program to behave that way, so I would
probably add a "reasonable" limit (like 16 MB) that interrupt the loop,
but that's beyond the scope of my question. And anyway, I don't trust
any program to not use all memory and all swap space, so I have posix
ulimits set accordingly.

> I'd rather process the input one fixed-length chunk at a time and
> discard each chunk after processing (possibly by recycling the in-
> memory buffer for the next chunk).

I would do so if online processing was an option. However in the program
containing code similar to what I posted, I cannot do any processing
before reaching the end of input, because the processed format allows
forward references, so references have to be gathered in a first pass on
the whole input, before doing a second pass where dereference can take
place.

> Note that reading one character at a time from a stream is quite fast
> since streams normally use the operating system's buffered IO. Adding
> a second layer of buffering inside your program is probably counter-
> productive.

That second layer of buffer was actually meant to reduce the number of
Ada.Strings.Unbounded.Append calls (and the expensive reallocations that
go with them).

However I thought that something similar to C's fread(), which can use
efficient bulk memory copy, would be better than a loop of single
character read (even when buffered in memory).


Thanks for your comments,
Natasha



  reply	other threads:[~2011-06-06 10:08 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-05 16:20 Reading the while standard input into a String Natasha Kerensikova
2011-06-06  1:49 ` robin
2011-06-06  7:18 ` Dmitry A. Kazakov
2011-06-06 10:46   ` Natasha Kerensikova
2011-06-06 12:05     ` Dmitry A. Kazakov
2011-06-06 16:55       ` Jeffrey Carter
2011-06-06 17:42         ` Pascal Obry
2011-06-06 17:43         ` Pascal Obry
2011-06-06 18:31         ` Dmitry A. Kazakov
2011-06-06  8:09 ` stefan-lucks
2011-06-06  8:33 ` Ludovic Brenta
2011-06-06 10:08   ` Natasha Kerensikova [this message]
2011-06-06 10:27     ` Ludovic Brenta
2011-06-06 10:31       ` Ludovic Brenta
2011-06-06 12:07         ` Natasha Kerensikova
2011-06-06 15:18   ` Maciej Sobczak
2011-06-06 18:18     ` Dmitry A. Kazakov
2011-06-06 18:36       ` Maciej Sobczak
2011-06-06 18:53         ` Dmitry A. Kazakov
2011-06-06 19:10           ` J-P. Rosen
2011-06-06 19:46             ` Dmitry A. Kazakov
2011-06-06 23:37           ` Shark8
2011-06-07  3:00             ` Randy Brukardt
2011-06-07  7:25             ` Dmitry A. Kazakov
2011-06-07  7:32           ` Maciej Sobczak
2011-06-07  8:51             ` Dmitry A. Kazakov
2011-06-07  3:10         ` Randy Brukardt
2011-06-06 19:06       ` J-P. Rosen
2011-06-06 19:52         ` Dmitry A. Kazakov
2011-06-07  3:15         ` Randy Brukardt
2011-06-06 22:14   ` Robert A Duff
2011-06-06  9:46 ` Georg Bauhaus
2011-06-06 11:16 ` Georg Bauhaus
2011-06-06 12:11   ` Dmitry A. Kazakov
2011-06-06 13:32     ` Georg Bauhaus
2011-06-06 14:06       ` Dmitry A. Kazakov
2011-06-06 14:14         ` Georg Bauhaus
2011-06-07  3:19         ` Randy Brukardt
2011-06-06 18:14 ` John B. Matthews
2011-06-07 10:23   ` Martin
2011-06-07 16:57     ` John B. Matthews
2011-06-07 17:22     ` Robert A Duff
2011-06-07 17:55       ` John B. Matthews
2011-06-08  0:26         ` Robert A Duff
2011-06-08  7:38         ` Egil Høvik
2011-06-08  9:27           ` Martin
2011-06-08 12:04             ` Egil Høvik
2011-06-08 18:09               ` Niklas Holsti
2011-06-09  0:46                 ` Randy Brukardt
2011-06-10  8:22                   ` Martin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox