From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id C29CFBC6C for ; Fri, 18 Jan 2008 17:55:03 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAADppkEdC+Vypnmdsb2JhbACQFAEBAQEHBAYHChiVIYd2 X-IronPort-AV: E=Sophos;i="4.25,217,1199660400"; d="scan'208";a="21483709" Received: from ug-out-1314.google.com ([66.249.92.169]) by mail4-smtp-sop.national.inria.fr with ESMTP; 18 Jan 2008 17:55:03 +0100 Received: by ug-out-1314.google.com with SMTP id k40so519472ugc.18 for ; Fri, 18 Jan 2008 08:55:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:subject:from:to:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer:content-transfer-encoding; bh=RXqdYCvVdf1TDPeq5guXSUeVs7GyAaqQcdN8xicIzrE=; b=GeQYjxDYtFo2dE0yK8LKzF4juSSIPAHn5iXN2Ps0Sea2Qa7uT0kJBv0yc8mjDynuFgZPgfW7YEWETxfrmNnoSqRCKQ6r+EM0yevDzeJMu+wJW2uOoOH/gUWEnhM+kOkC8BF7c7jJmpz86skQatTbP0TJpxIro05VUvQ/IVVqOvo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:from:to:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer:content-transfer-encoding; b=nBg1E6ldd3Au2LLqxNfNjh6lOB8l75vVtjtwlWs7SLvne2NP8zA+U8LZQteCEbUO8j8KHNBErZ00kh5PHrd96ebAxAkUjJ2XxX7argwLCtaOH6iS4KR3bb2ZvRo9NY3UsA0u4bPfnJq8eK1DK/JZfTt9KyX4DPlvwtSta01XnvU= Received: by 10.66.219.11 with SMTP id r11mr1361623ugg.31.1200675302974; Fri, 18 Jan 2008 08:55:02 -0800 (PST) Received: from ?192.168.0.10? ( [82.237.227.151]) by mx.google.com with ESMTPS id o11sm1200472fkf.9.2008.01.18.08.54.57 (version=SSLv3 cipher=RC4-MD5); Fri, 18 Jan 2008 08:54:58 -0800 (PST) Subject: Re: [Caml-list] Strange performances From: Benjamin Canou To: caml-list@yquem.inria.fr In-Reply-To: <20080118.181206.85503086.garrigue@math.nagoya-u.ac.jp> References: <1200619933.6383.47.camel@benjamin-laptop> <20080118.111503.185813743.garrigue@math.nagoya-u.ac.jp> <9d3ec8300801172339j38bf734dm5b84f951a4342188@mail.gmail.com> <20080118.181206.85503086.garrigue@math.nagoya-u.ac.jp> Content-Type: text/plain; charset=UTF-8 Date: Fri, 18 Jan 2008 17:55:14 +0100 Message-Id: <1200675314.6345.32.camel@benjamin-laptop> Mime-Version: 1.0 X-Mailer: Evolution 2.12.1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; runtime:01 bug:01 ocaml:01 runtime:01 segfault:01 segfaults:01 segfaults:01 segfault:01 ocaml's:01 out-of-bound:01 beginner's:01 ocaml:01 bug:01 beginners:01 wrote:01 Hi, Indeed, using runtime exceptions catching as a programming style may lead to strange behaviours, but I think using the any pattern in the try with construct was the real mistake. So basically, to those who did not understand the problem : This code works perfectly : let list_of_string s = let rec list_of_string s i = try let e = s.[i] in e :: list_of_string s (succ i) with Invalid_argument "index out of bounds" -> [] in list_of_string s 0 And if I had used a correct matching of exceptions in my original code : let list_of_string s = let rec list_of_string s i = try s.[i] :: list_of_string s (succ i) with Invalid_argument "index out of bounds" -> [] in list_of_string s 0 I would have noticed that the bug came from my carelessness about the evaluation order (btw, thank you Jacques) : Fatal error: exception Stack_overflow In fact, the function calls itself recursively to construct the right hand side of the list constructor until the stack is full. With the any (_) pattern, the function returns [] for each call from the one corresponding to the end of the string to the one causing the overflow. So the result is correct, but it takes a time proportional to the maximum size of the stack... With a pattern matching the out of bounds exception, the stack overflow is not caught and the programs exits abnormally right after the overflow. Jacques, if I remember well, the ocaml runtime is not able to detect stack overflows in native code on all platforms, that's why you get a segfault instead of a Stack overflow exception. Benjamin Canou. Le vendredi 18 janvier 2008 à 18:12 +0900, Jacques Garrigue a écrit : > From: "Till Varoquaux" > > On Jan 18, 2008 2:15 AM, Jacques Garrigue wrote: > > ... > > > By the way, on my machine your version doesn't even work in native > > > code, I only get segfaults. This is allowed behaviour for > > > out-of-bounds access. > > > > Could you please clarify? This seems a little scary to me, I thought > > segfaults where acceptable only when you used unsafe features (or ran > > out of stack). > > This is why I sent an erratum. The cause for the segfault was not the > array access, but the stack overflow, which occured due to ocaml's > peculiar evaluation order. > Still, I maintain that intentionally raising and catching out-of-bound > accesses is not good programming style... > > Jacques Garrigue > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs