cost of Java "assert" when disabled?
Maxym Pendyshchuk
gotischerose at yahoo.de
Thu Feb 16 14:20:18 PST 2012
In "A Simple Assertion Facility For the Javatm Programming Language" ( java.sun.com/docs/books/jls/assert-spec.html )
is mentioned (Appendix IV, Implementation Notes), that assert is
actually just a sugar (ok, that's my understanding of what is mentioned
there),
You write
SomeClass {
public void someMethod() {
assert condition : message;
}
}
and compiler does:
SomeClass {
static final boolean $assertionsDisabled = !SomeClass.class.desiredAssertionStatus();
public void someMethod() {
if ( ! ( $assertionsDisabled || condition ) )
throw new java.lang.AssertionError( message );
}
}
}
so you still have if-block even if assert is disabled, but HotSpot quickly can find out that this block is never executed, and in runtime it will
be thrown away if this method is "hot", otherwise if this method is not executed frequently, then there is no drawback anyway...
hope somebody will correct me if I am wrong
Max
________________________________
Von: Srinivas Ramakrishna <ysr1729 at gmail.com>
An: core-libs-dev at openjdk.java.net; hotspot-dev at openjdk.java.net
Gesendet: 22:42 Donnerstag, 16.Februar 2012
Betreff: cost of Java "assert" when disabled?
A Java language newbie question:
Does anyone have any ballpark numbers on the cost (and its scaling) of peppering assert's in your Java code, but with asserts disabled (-da) ?
In other words, is the disabled cost so vanishingly small that we need not think twice when using them, or should one be
careful because the cost is non-negligible and can affect (bytecode) footprint or rutime performace even when switched off?
thanks for any advice.
-- ramki
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-dev/attachments/20120216/ec77f641/attachment-0001.html
More information about the hotspot-dev
mailing list