release mode optimization

monarch_dodra monarchdodra at gmail.com
Thu Jan 10 01:13:49 PST 2013


On Thursday, 10 January 2013 at 00:21:48 UTC, Charles Hixson 
wrote:
> Would the following code:
> for (int i = 1; i < di.count; i++)
> {  assert (node.di.entry[i - 1].key < node.di.entry[i].key);  }
>
> be optimized away if compiled under -release?

If you write it as:
//----
version(assert)
{
     for (int i = 1; i < di.count; i++)
         assert (node.di.entry[i - 1].key < node.di.entry[i].key);
}
//----

Then I 100% guarantee that it will not appear in release mode ;)

I know it's *kind of* off topic in regards to the original 
question, but if you have a chunk of code that is destined only 
for assertive validation, you might as well put it in an assert 
block:
Not only is it guaranteed behavior (as opposed to relying on the 
compiler). It also makes reviewing simpler, as it is self 
documenting. Without it, a reviewer might waste effort thinking 
about the loop conditions*

*Also: What is "di"? If it is a user defined type, and if "count" 
is not declared as const, then the compiler may not optimize away 
the loop itself, because it is worried about any side effect from 
calling di.count.


More information about the Digitalmars-d-learn mailing list