Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

assertion code overhead when disabled  RSS feed

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this statement from the assertion spec.
Programmers developing applications for resource-constrained devices may wish to strip assertions out of class files entirely. While this makes it impossible to enable assertions in the field, it also reduces class file size, possibly leading to improved class loading performance. In the absence of a high quality JIT, it could lead to decreased footprint and improved runtime performance.

Assertions disabled, compared to the absence of assertion code, could degrade performance. Is that right?
Does your favorite exam prep book say assertions disabled do not cause any overhead?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Sun documentation I have seen, running with assertions disabled should cause no performance degradation.
I ran a test with 1,000,000 iterations and the difference between no assertions and assertions turned off was 1 millisecond. YMMV.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene, I'm moving this discussion to the Performance forum as the SCJP exam does not deal with performance issues.
You can continue this discussion there. Thank you
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alas, this question does not belong in the performance forum.
I was trying to show people studying for the SCJP that the spec says one thing and the certification books say another.
I am worried I will get the answer wrong on the exam if I answer according to the spec.
[ June 16, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assert statement is syntatic sugar for this:

So that is how assertions when disabled could affect performance, depending on the compiler.
Oh well, since I am here... I am curious to know what a footprint is.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the key phrases in that statement is: "In the absence of a high quality JIT."
There is no unnecessary footprint with a half-decent JIT compiler when assertions are disabled. Since assertions cannot be turned on or off in a class that's already been loaded, the JIT can either omit or include them at class loading time and never think about it again. In fact, the optimization is so simple that even a half-decent interpretter could do it.
I supposed a JVM could translate "assert Expression1 : Expression2" so that it's fundamentally equivalent to
if ($assertionEnabled && !Expression1)
throw new java.lang.AssertionError(Expression2);
but there's no reason to do so unless that JVM offers the ability to change $assertionEnabled's value for an already loaded class at runtime, which would be very non-standard.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:
Alas, this question does not belong in the performance forum.
I was trying to show people studying for the SCJP that the spec says one thing and the certification books say another.
I am worried I will get the answer wrong on the exam if I answer according to the spec.

As was previously stated, performance questions such as these are not on the exam since performance is highly dependent on JVM implementation.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you David. Very helpful. It did not occur to me the JIT would actually strip out the assert statements.
Thank you Thomas. I blew it again. I'll try not to post out-of-scope questions. But if I do, you can reply Out of Scope, if you would like.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene,
I was trying to show people studying for the SCJP that the spec says one thing and the certification books say another.
I am worried I will get the answer wrong on the exam if I answer according to the spec.

Always trust the spec as it is the ultimate authoritative document
I'm sorry if you think that I should not have moved your post. No hard feelings
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Valentin. I need some more practice writing crystal clear posts. I also need a better filter for out-of-scope questions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!