release mode optimization

Charles Hixson charleshixsn at earthlink.net
Thu Jan 10 08:27:54 PST 2013


di is a struct containing *NO* functions.  count, however, is a 
@property, it just returns a counter variable, but that's not a local 
property.

I *LIKE* the
version(assert)
{
...
}
choice.  I'd been thinking about debug.  I didn't know that version (or 
possibly assert) could be used that way.
On 01/10/2013 01:13 AM, monarch_dodra wrote:
> 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