Perfomance: Exception or dynamic return value ?

35 views
Skip to first unread message

Gen

unread,
Aug 6, 2012, 6:49:20 AM8/6/12
to mi...@dartlang.org
Hi,

concerning program performance and dynamic code optimization (JIT) done in Javascript (V8) and Dart now and in the future,
is is better to pass an error message object as exception or as normal method return object ?

Best regards

Gen

unread,
Aug 6, 2012, 7:00:08 AM8/6/12
to mi...@dartlang.org
In case a function can dynamically return only one of these objects:
 - either a regular object of one type/interface that has only one class as implementation.
 - or an error message of the always the same Error type.
 
What would be the technically most efficient solution ?
A dynamically typed return value or passing the error as exception ?
 

Florian Loitsch

unread,
Aug 6, 2012, 7:51:06 AM8/6/12
to Gen, mi...@dartlang.org
If you have performance critical code you are usually better off not using try/catch. In the current v8 a try/catch statement makes the function non-optimizable. This should (hopefully) eventually be fixed, but even then it will probably still be slower than a function that does not contain any try/catch. Note however that this only concerns the function that actually contains the try/catch statement. Throwing an exception should be fine.
V8 and Dart use type-feedback to optimize code. So if your function never returns the exception-object it will not even know that a different type could be returned. Even if the exception object is returned it could have nearly no impact on performance (depends on the new live code and how the optimizer arranges the type-tests).

If your code is not performance critical try to stick with try/catch since this is the agreed upon way to deal with errors, and makes your code more readable and maintainable.
// florian



 

--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 



--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

Gen

unread,
Aug 6, 2012, 8:49:37 AM8/6/12
to mi...@dartlang.org, Gen

Am Montag, 6. August 2012 13:51:06 UTC+2 schrieb Florian Loitsch:
If you have performance critical code you are usually better off not using try/catch. In the current v8 a try/catch statement makes the function non-optimizable. This should (hopefully) eventually be fixed, but even then it will probably still be slower than a function that does not contain any try/catch. Note however that this only concerns the function that actually contains the try/catch statement. Throwing an exception should be fine.
V8 and Dart use type-feedback to optimize code. So if your function never returns the exception-object it will not even know that a different type could be returned. Even if the exception object is returned it could have nearly no impact on performance (depends on the new live code and how the optimizer arranges the type-tests).

If your code is not performance critical try to stick with try/catch since this is the agreed upon way to deal with errors, and makes your code more readable and maintainable.
// florian


 
Thanks for the quick reply.
 
I am spending some time on automatic code generation by an interpreter; so the most readable form (whatever it may be) is not of a major concern.
 
I have asked because have found, but not entirely read nor understood, this old paper "A Study of Exception Handling and Its Dynamic Optimization in Java" from 2001.
And I have wondered if in the special case that I have described in my second post, that the JIT could maybe maintain efficient code inlining per alternative code path identified as either:
- the regular path
- the exceptional path 
 
Reply all
Reply to author
Forward
0 new messages