comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Reserve_Capacity for Unbounded_String?
Date: Tue, 24 Jul 2007 19:16:15 -0500
Date: 2007-07-24T19:16:15-05:00	[thread overview]
Message-ID: <f864k3$gmv$1@jacob-sparre.dk> (raw)
In-Reply-To: wccr6mx5pz0.fsf@shell01.TheWorld.com

"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
news:wccr6mx5pz0.fsf@shell01.TheWorld.com...
> "Randy Brukardt" <randy@rrsoftware.com> writes:
...
> > For Janus/Ada, Unbounded_String is just a controlled wrapper around
"access
> > String"; in particular, the length is the length of the item
> > allocated.
>
> Are you saying that:
>
>     for I in 1..N loop
>         Append (Some_Unbounded_String, 'x');
>
> causes a free and a "new" and a copy every time around the loop?

Yup; I just checked the code to make sure. Moreover, I have yet to see an
real example where the performance difference would matter that much (Free
and new aren't that expensive relative to the alternatives). Our spam filter
does operations like this, and as I said earlier, the performance bottleneck
is with Index and with the file I/O operations and not here. (Nor are there
any fragmentation problems that I can detect; the program runs for many days
without issues.)

In any case, I would expect this operation (of making the string longer) to
do a lot of allocations anyway, because the string will need to be expanded
a lot. And any "over"-allocation will be wasteful of memory; for the spam
filter, that would tend to reduce the maximum size of messages that could be
filtered.(Janus/Ada always emphasizes smaller memory usages versus wasting
memory to get better performance.)

Besides, in Janus/Ada, most the alternatives would also do a (under the
covers) "new" and "free". We do try to optimize those out, but by default

             Var : String := Some_Value & 'x';

would generate three allocations and deallocations. The optimizer probably
would get rid of the descriptors or allocate them statically, but the object
Var probably would end up in the heap.

If performance is a real issue, you'd have to write your own code to do the
actual operations you want; for instance, in your example
To_Unbounded_String (Some_Unbounded_String, (1..N => 'x')); would be a lot
faster on any compiler (far fewer calls to a library; even if there is no
allocation, there still will be overhead checks for bounds and the like).
The examples where an alternative implementation of Unbounded_Strings would
make a significant difference such that the application was fast enough with
it and not fast enough without it have to be extremely rare.

                        Randy.





  reply	other threads:[~2007-07-25  0:16 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-22 19:54 Reserve_Capacity for Unbounded_String? Maciej Sobczak
2007-07-22 21:32 ` Robert A Duff
2007-07-23 19:29   ` Maciej Sobczak
2007-07-23 20:30     ` Robert A Duff
2007-07-23  4:28 ` Jeffrey R. Carter
2007-07-23 15:07 ` Adam Beneschan
2007-07-24  1:01   ` Randy Brukardt
2007-07-24  7:57     ` Pascal Obry
2007-07-24 18:58       ` Randy Brukardt
2007-07-24 23:50         ` Robert A Duff
2007-07-25  0:00           ` Randy Brukardt
2007-07-24 23:54         ` Pascal Obry
2007-07-25  0:52           ` Randy Brukardt
2007-07-25  1:28           ` Randy Brukardt
2007-07-25  7:48             ` Pascal Obry
2007-07-25  9:55               ` Georg Bauhaus
2007-07-25 10:02                 ` Georg Bauhaus
2007-07-25 18:58               ` Randy Brukardt
2007-07-25  8:50             ` Martin Krischik
2007-07-25  9:26               ` AW: " Grein, Christoph (Fa. ESG)
2007-07-25 15:32                 ` Martin Krischik
2007-07-25 15:39                 ` Martin Krischik
2007-07-24 23:41     ` Robert A Duff
2007-07-25  0:16       ` Randy Brukardt [this message]
2007-07-25  2:25         ` Robert A Duff
2007-07-25  6:07           ` Simon Wright
2007-07-25 19:08           ` Randy Brukardt
2007-07-25 20:37             ` Maciej Sobczak
2007-07-25 22:06               ` Georg Bauhaus
2007-07-26  6:24                 ` Maciej Sobczak
2007-07-26  8:09                   ` Dmitry A. Kazakov
2007-07-26  8:20                     ` Pascal Obry
2007-07-26  9:59                       ` Dmitry A. Kazakov
2007-07-26  8:35                   ` Georg Bauhaus
2007-07-26 22:11               ` Randy Brukardt
replies disabled

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