From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,64b29dfa2220a59f X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!57g2000hsv.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Reserve_Capacity for Unbounded_String? Date: Mon, 23 Jul 2007 12:29:00 -0700 Organization: http://groups.google.com Message-ID: <1185218940.794004.249540@57g2000hsv.googlegroups.com> References: <1185134043.892012.217560@n2g2000hse.googlegroups.com> NNTP-Posting-Host: 85.0.251.175 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1185218940 20496 127.0.0.1 (23 Jul 2007 19:29:00 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 23 Jul 2007 19:29:00 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: 57g2000hsv.googlegroups.com; posting-host=85.0.251.175; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news1.google.com comp.lang.ada:16565 Date: 2007-07-23T12:29:00-07:00 List-Id: On 22 Lip, 23:32, Robert A Duff wrote: > > Why there is no Reserve_Capacity for Unbounded_String? > > Probably for historical reasons. Would introducing it break any existing code? > > 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. 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. 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). -- Maciej Sobczak http://www.msobczak.com/