caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Performance penalty of exception handling
@ 2015-09-16 19:26 "Mark Adams"
  0 siblings, 0 replies; 5+ messages in thread
From: "Mark Adams" @ 2015-09-16 19:26 UTC (permalink / raw)
  To: helmut.brandl, caml-list

One problem is if your function that traps the exception is recursive - it
cannot be made tail recursive if the recursive call is within the 'try' ..
'with', which can cause stack overflow if it recurses thousands of times.

Mark Adams.

on 16/9/15 8:08 PM, Helmut Brandl <helmut.brandl@gmx.net> wrote:

> Hi all,
>
> I use exeption handling quite frequently even in inner loops. A common
> pattern:
>
> let some_function ... =
> ...
> if cannot_compute then raise Not_found else value
>
> try
> let value = some_function ... in
> ...
> with Not_found ->
> handle_exception
>
> I use this pattern even if the direct caller of "some_function" handles
> the exception. In many cases I could design "some_function" in a way
> that it returns an optional value and check the optional value in the
> caller. Is this significantly (at least 10%) faster? If not, I would
> stay with my present design. However I am pressed for performance in the
> inner loops (I am only interested in compilation to native).
>
> Is there any information available on the preformance penalty of
> exception handling? Any hints? Thanks in advance.
>
> Regards
> Helmut
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Performance penalty of exception handling
  2015-09-16 19:08 Helmut Brandl
  2015-09-16 19:15 ` John Whitington
@ 2015-09-16 20:00 ` David Rajchenbach-Teller
  1 sibling, 0 replies; 5+ messages in thread
From: David Rajchenbach-Teller @ 2015-09-16 20:00 UTC (permalink / raw)
  To: Helmut Brandl, caml-list

[-- Attachment #1: Type: text/plain, Size: 1126 bytes --]

I did some benchmarking a while ago.
You can find the summary here:
https://hal.inria.fr/inria-00432575/PDF/monad.pdf

Cheers,
 David



On 16/09/15 21:08, Helmut Brandl wrote:
> Hi all,
> 
> I use exeption handling quite frequently even in inner loops. A common
> pattern:
> 
>     let some_function ... =
>             ...
>            if cannot_compute then raise Not_found else value
> 
>     try
>         let value = some_function ... in
>         ...
>     with Not_found ->
>        handle_exception
> 
> I use this pattern even if the direct caller of "some_function" handles
> the exception. In many cases I could design "some_function" in a way
> that it returns an optional value and check the optional value in the
> caller. Is this significantly (at least 10%) faster? If not, I would
> stay with my present design. However I am pressed for performance in the
> inner loops (I am only interested in compilation to native).
> 
> Is there any information available on the preformance penalty of
> exception handling? Any hints? Thanks in advance.
> 
> Regards
> Helmut
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Performance penalty of exception handling
       [not found]   ` <CAKR7PS9iibAcWVMbjahdkVn6KBXUZa0U88KwZEkqc-LzfaBUKg@mail.gmail.com>
@ 2015-09-16 19:51     ` Milan Stanojević
  0 siblings, 0 replies; 5+ messages in thread
From: Milan Stanojević @ 2015-09-16 19:51 UTC (permalink / raw)
  To: John Whitington, Caml List

[-- Attachment #1: Type: text/plain, Size: 793 bytes --]

(correctly sending to the list)

> > Optional values often require allocation (and, therefore, garbage
collection). Unless your process is so short-lived that the garbage
collector never runs, exceptions are going to be faster. So, if you
benchmark this, make sure your benchmarks run for a while.
> >
> > Someone who knows the compiler better than I will be able to tell you
if (Some x) when immediately pattern-matched across a function call causes
an allocation.
> >
> > Constant exception constructors like Not_found, I believe, require no
allocation in recent versions of OCaml.
> >
>

The cost of an exception is not just its allocation. You also need to setup
the exception handler. Also, backtrace recording is not cheap, so one
should use raise_notrace primitive for cases like this.

[-- Attachment #2: Type: text/html, Size: 924 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Performance penalty of exception handling
  2015-09-16 19:08 Helmut Brandl
@ 2015-09-16 19:15 ` John Whitington
       [not found]   ` <CAKR7PS9iibAcWVMbjahdkVn6KBXUZa0U88KwZEkqc-LzfaBUKg@mail.gmail.com>
  2015-09-16 20:00 ` David Rajchenbach-Teller
  1 sibling, 1 reply; 5+ messages in thread
From: John Whitington @ 2015-09-16 19:15 UTC (permalink / raw)
  To: Helmut Brandl; +Cc: caml-list

Hi,

Helmut Brandl wrote:
> I use exeption handling quite frequently even in inner loops. A common
> pattern:
>
> let some_function ... =
> ...
> if cannot_compute then raise Not_found else value
>
> try
> let value = some_function ... in
> ...
> with Not_found ->
> handle_exception
>
> I use this pattern even if the direct caller of "some_function" handles
> the exception. In many cases I could design "some_function" in a way
> that it returns an optional value and check the optional value in the
> caller. Is this significantly (at least 10%) faster?

Optional values often require allocation (and, therefore, garbage 
collection). Unless your process is so short-lived that the garbage 
collector never runs, exceptions are going to be faster. So, if you 
benchmark this, make sure your benchmarks run for a while.

Someone who knows the compiler better than I will be able to tell you if 
(Some x) when immediately pattern-matched across a function call causes 
an allocation.

Constant exception constructors like Not_found, I believe, require no 
allocation in recent versions of OCaml.

Cheers,

-- 
John Whitington
Director, Coherent Graphics Ltd
http://www.coherentpdf.com/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Performance penalty of exception handling
@ 2015-09-16 19:08 Helmut Brandl
  2015-09-16 19:15 ` John Whitington
  2015-09-16 20:00 ` David Rajchenbach-Teller
  0 siblings, 2 replies; 5+ messages in thread
From: Helmut Brandl @ 2015-09-16 19:08 UTC (permalink / raw)
  To: caml-list

Hi all,

I use exeption handling quite frequently even in inner loops. A common 
pattern:

     let some_function ... =
             ...
            if cannot_compute then raise Not_found else value

     try
         let value = some_function ... in
         ...
     with Not_found ->
        handle_exception

I use this pattern even if the direct caller of "some_function" handles 
the exception. In many cases I could design "some_function" in a way 
that it returns an optional value and check the optional value in the 
caller. Is this significantly (at least 10%) faster? If not, I would 
stay with my present design. However I am pressed for performance in the 
inner loops (I am only interested in compilation to native).

Is there any information available on the preformance penalty of 
exception handling? Any hints? Thanks in advance.

Regards
Helmut

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-09-16 20:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-16 19:26 [Caml-list] Performance penalty of exception handling "Mark Adams"
  -- strict thread matches above, loose matches on Subject: below --
2015-09-16 19:08 Helmut Brandl
2015-09-16 19:15 ` John Whitington
     [not found]   ` <CAKR7PS9iibAcWVMbjahdkVn6KBXUZa0U88KwZEkqc-LzfaBUKg@mail.gmail.com>
2015-09-16 19:51     ` Milan Stanojević
2015-09-16 20:00 ` David Rajchenbach-Teller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).