comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Reserve_Capacity for Unbounded_String?
Date: Mon, 23 Jul 2007 16:30:26 -0400
Date: 2007-07-23T16:30:26-04:00	[thread overview]
Message-ID: <wcc3azelv6l.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 1185218940.794004.249540@57g2000hsv.googlegroups.com

Maciej Sobczak <see.my.homepage@gmail.com> writes:

> On 22 Lip, 23:32, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
>
>> > Why there is no Reserve_Capacity for Unbounded_String?
>>
>> Probably for historical reasons.
>
> Would introducing it break any existing code?

Only in rare cases.  For ex., if you had a type called Reserve_Capacity
in an unrelated package, and you had "use" on that one and also on
unbounded strings, the code would now be illegal.

>> > declare
>> >    S : Unbounded_String;
>> >    My_Capacity : constant := 1000;
>> > begin
>> >    S := To_Unbounded_String (My_Capacity);
>>
>> I would write
>>
>>   To_Unbounded_String (Length => My_Capacity);
>>
>> to make it clear which To_Unbounded_String you're calling.
>
> Yes. I try to follow the "rule" that names associations should be used
> for any subprogram call with more than one parameter.

Well, that's close, but not quite the right rule.  For example, "+" on
integers has two parameters, but I would say X+Y rather than
"+"(Left => X, Right => Y).

I don't have a well-defined rule, but I like to use named notation
often.

I think perhaps the designer of the subprogram should decide whether
named notation is appropriate, and all callers have to obey.  But that's
a language design issue, not an Ada issue.

>...I've never
> considered hardening this rule for overloaded subprograms.
>
>> > "Seems to work fine" means that after the above two operations the
>> > string is logically empty, but the subsequent appends run faster
>> > (which is actually the original motivation).
>>
>> How much faster (I'm curious)?
>
> This of course depends on what is the ration between allocations and
> reservations in a given test.
>
> Here:
>
> with Ada.Strings.Unbounded;
> procedure String_Test is
> begin
>    for I in 1 .. 10000 loop
>       declare
>          S : Ada.Strings.Unbounded.Unbounded_String;
>          use Ada.Strings.Unbounded;
>       begin
> --       S := Ada.Strings.Unbounded.To_Unbounded_String (10000);
> --       Ada.Strings.Unbounded.Delete (S, 1, 10000);
>          for J in 1 .. 10000 loop
>             Ada.Strings.Unbounded.Append (S, 'A');
>          end loop;
>       end;
>    end loop;
> end String_Test;
>
> On my machine and with option -O2 uncommenting the two lines makes a
> difference between 1.86s and 1.43s, which is about 20% gain. 

Interesting.  Thanks for posting the measurements.

>...Depending
> on a situation it might or might not be worth the hassle (the hassle
> is in determining the correct reservation size - and if we can know it
> fully in advance, then maybe there is no point in using unbounded at
> all).

Good point.  If you know the max size, the Bounded (or just plain
String) is appropriate.  But if you know the max "typical" size,
but that you still want it to work for atypical sizes, then this
sort of thing seems reasonable.

As others have pointed out, it depends on the implementation.
But that's always true about efficiency -- if you care about
efficiency, then the abstraction leaks.

- Bob



  reply	other threads:[~2007-07-23 20:30 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 [this message]
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
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