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.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e6c9800e35ccfeee X-Google-Attributes: gid103376,public From: Mats Weber Subject: Re: GNAT: Performance of String functions Date: 1997/07/24 Message-ID: <33D74581.DEC93419@elca-matrix.ch>#1/1 X-Deja-AN: 258490291 References: <5r1l6e$e0h$1@ratatosk.uio.no> <1997Jul22.071638.1@eisner> <33D4F30F.5299@online.no> <5r5cfh$irn$1@ratatosk.uio.no> X-Priority: 3 (Normal) Organization: ELCA Matrix SA Reply-To: Mats.Weber@elca-matrix.ch Newsgroups: comp.lang.ada Date: 1997-07-24T00:00:00+00:00 List-Id: > I'd be delighted if it is possible to write a portable bounded string > package > that does not copy blindly on assignment. So far everything I have > seen leads > me to believe that this is not possible. Perhaps if bounded strings > were > implemented with pointers to the actual strings, but that may just > complicate > matters. In GNAT 3.09, bounded strings are implemented as subtype Length_Range is Natural range 0 .. Max_Length; type Bounded_String is record Length : Length_Range := 0; Data : String (1 .. Max_Length); end record; an alternative implementation is subtype Length_Range is Natural range 0 .. Max_Length; type Bounded_String (Length : Length_Range := 0) is record Data : String (1 .. Length); end record; both have their advantages and disadvantages: GNAT's choice implies copying the whole record on assignment, whereas the other approoach will probably only copy the storage occupied by the value. So yes, bounded strings can be implemented without blind copying on assignment. On the other hand, operations such as appending a single character to a string can be done directly in the GNAT implementation, but involves re-assigning the whole value in the alternative implementation, and can turn O(N) algorithms into O(N**2). Also, in the GNAT case, if you get into a situation where predefined "=" re-emerges, then your program is broken because you will be comparing the whole records instead of just the slice 1 .. Length. This problem does not occur with the alternative approach. I think an AI is about to be voted that will require predefined "=" to behave correctly for these types, which will almost force implementors to use the alternative approach.