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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4fbd260da735f6f4 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 04 May 2007 07:15:03 -0500 Date: Fri, 04 May 2007 08:07:59 -0400 From: Jeffrey Creem User-Agent: Thunderbird 2.0.0.0 (Windows/20070326) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Copying string slices before calling subroutines? References: <0hj5339mjmond132qhbn2o01unurs61lbj@4ax.com> <1178091967.392381.282510@o5g2000hsb.googlegroups.com> <5dv3wh6scrh1.2986pbvdw8y2$.dlg@40tude.net> <4oppvmc6anir.g0mz9mrahmdo.dlg@40tude.net> In-Reply-To: <4oppvmc6anir.g0mz9mrahmdo.dlg@40tude.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4akrg4-mot.ln1@newserver.thecreems.com> NNTP-Posting-Host: 24.147.74.171 X-Trace: sv3-7YbDEc0+XcWvpqxO9ilylNSO9p9hJl1pF8b/d162rufff2S3K2NemChzDwDp1s2N8KCMasNP6FNCpHm!MvTmGdMtspdPzWsS67UcWOL1XHgJPe9MZOFC3SMdo3TnqYEXxIpQSgNELcGd9Tq7xwuX2rrbK+Oi!zO+4q2jKwIAFNgftfy1eFQ== X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.34 Xref: g2news1.google.com comp.lang.ada:15516 Date: 2007-05-04T08:07:59-04:00 List-Id: Dmitry A. Kazakov wrote: > On Fri, 04 May 2007 11:44:14 +0200, Jacob Sparre Andersen wrote: > >> Jacob Sparre Andersen wrote: >>> Dmitry A. Kazakov wrote: >>>> On Fri, 04 May 2007 08:53:59 +0200, Jacob Sparre Andersen wrote: >>>>> for Lines in 1 .. 10 loop >>>>> Last := Index (Source => Text (Text'First .. Last - 1), >>>> Did you check if GNAT did not copy the string slice before calling to >>>> Index? >>> No. Do you have any suggestion for a way to check it. The >>> reference manual (section 6.2) seems to indicate that it is up to >>> the compiler to decide. >> I have found a strong indication that GNAT copies the whole string >> slice onto the stack before calling Index: > > So, Index was innocent. > I don't know that I'd say that...Though it might be innocent in this case, a quick look at the source code (which then remaps index to a common string search package) looks to me like there are other opportunities where some versions of the Index call will make another copy in cases when it need not and further this does not appear to match the comments in the package. Specifically, in the trunk of the current FSF Ada in a-strsea.adb at the top of the package it says (formatting adjusted on this comment) -- Note: This code is derived from the ADAR.CSH public domain Ada 83 -- versions of the Appendix C string handling packages (code extracted -- from Ada.Strings.Fixed). A significant change is that we optimize -- the case of identity mappings for Count and Index, and also -- Index_Non_Blank is specialized (rather than using the -- general Index routine). Great! Sounds promising...Lets take a look at one of them function Index (Source : String; Pattern : String; Going : Direction := Forward; Mapping : Maps.Character_Mapping := Maps.Identity) return Natural is Cur_Index : Natural; Mapped_Source : String (Source'Range); begin if Pattern = "" then raise Pattern_Error; end if; for J in Source'Range loop Mapped_Source (J) := Value (Mapping, Source (J)); end loop; -- Forwards case if Going = Forward then for J in 1 .. Source'Length - Pattern'Length + 1 loop Cur_Index := Source'First + J - 1; if Pattern = Mapped_Source (Cur_Index .. Cur_Index + Pattern'Length - 1) then return Cur_Index; end if; end loop; -- Backwards case else for J in reverse 1 .. Source'Length - Pattern'Length + 1 loop Cur_Index := Source'First + J - 1; if Pattern = Mapped_Source (Cur_Index .. Cur_Index + Pattern'Length - 1) then return Cur_Index; end if; end loop; end if; -- Fall through if no match found. Note that the loops are skipped -- completely in the case of the pattern being longer than the source. return 0; end Index; Hmm...I don't see the optimization for identity mappings anyplace... You can probably stop reading here as the formatting is ugly but the license for the above code snippet is: -- Copyright (C) 1992-2005, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- -- ware Foundation; either version 2, or (at your option) any later ver- -- -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- -- for more details. You should have received a copy of the GNU General -- -- Public License distributed with GNAT; see file COPYING. If not, write -- -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, -- -- Boston, MA 02110-1301, USA. -- -- -- -- As a special exception, if other files instantiate generics from this -- -- unit, or you link this unit with other files to produce an executable, -- -- this unit does not by itself cause the resulting executable to be -- -- covered by the GNU General Public License. This exception does not -- -- however invalidate any other reasons why the executable file might be -- -- covered by the GNU Public License. -- -- -- -- GNAT was originally developed by the GNAT team at New York University. -- -- Extensive contributions were provided by Ada Core Technologies Inc. --