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,e7536778f913e61a X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!x5g2000yqk.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Exception handling overhead? Date: Fri, 24 Apr 2009 05:06:30 -0700 (PDT) Organization: http://groups.google.com Message-ID: <9faab480-850c-4ec5-b05e-8daec0301928@x5g2000yqk.googlegroups.com> References: NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1240574790 21889 127.0.0.1 (24 Apr 2009 12:06:30 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 24 Apr 2009 12:06:30 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: x5g2000yqk.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4561 Date: 2009-04-24T05:06:30-07:00 List-Id: On Apr 24, 1:52=A0pm, "Peter C. Chapin" wrote: > Hello! > > I'm creating a type intended to represent lines of editable text. It > will provide some specialized services and is not intended to be a > general purpose string type. At the moment I'm wrapping the type > Ada.Strings.Unbounded.Unbounded_String (I'm really using Wide_Strings, > but I don't think that's important right now). I may want to change that > underlying implementation in the future so I don't want to directly > expose my current choice to my clients. Thus I'm providing some > subprograms that do some minor things and then just forward to > subprograms in Ada.Strings.Unbounded. > > My question is about exceptions. To avoid exposing my implementation > choice and to give me more control in the long run I've defined my own > exception for (as an example) an out of bounds access: > > Bad_Index : exception; > > Naturally I want to raise this exception if the client tries to access a > character not in my buffer. This leads to two possibilities. > > function Element(B : Buffer; Index : Positive) return Character is > begin > =A0 if Index > Ada.Strings.Unbounded.Length(B.Internal_String) then > =A0 =A0 raise Bad_Index; > =A0 end if; > =A0 return Ada.Strings.Unbounded.Element(B.Internal_String, Index); > end Element; > > This entails two checks on the index value: the one I'm doing and the > one being done inside Ada.Strings.Unbounded.Element. Another approach is > > function Element(B : Buffer; Index : Positive) return Character is > begin > =A0 return Ada.Strings.Unbounded.Element(B.Internal_String, Index); > exception > =A0 when Ada.Strings.Index_Error =3D> > =A0 =A0 raise Bad_Index; > end Element; > > This lets the check exist in only one place but it invokes the machinery > of exception handling. > > Now I know that in C++ there are implementation methods that allow zero > execution time overhead in the case when an exception is not thrown (at > the expense of increasing executable size and making thrown exceptions > slower). Compilers are not required to use such methods, of course, but > I know of at least one that does... or that can if the right options are > selected. I'm wondering about how this might work in Ada. Does Ada admit > such implementation methods? If so, do compilers commonly use them? From > a raw performance point of view which of the two approaches above would > be "better?" > > For the record... I understand that in my application it probably > doesn't matter much either way. I also understand that the answer is > likely to be very compiler specific. This is mostly a question of > academic interest. > > Thanks! > > Peter GNAT provides two exception handling mechanisms: zero-cost and setjump/ longjump. "Zero-cost" really means zero _distributed_ cost, i.e. you incur a cost only when raising an exception, as you describe. The older setjump/longjump mechanism costs some execution time and memory whether you raise exceptions or not. It is possible to select either mechanism when compiling your program. For some details, see the short mailing list thread starting at [1]. [1] http://gcc.gnu.org/ml/gcc/2006-10/msg00270.html Of course, this is compiler-specific. HTH -- Ludovic Brenta.