release mode optimization
Charles Hixson
charleshixsn at earthlink.net
Thu Jan 10 12:16:45 PST 2013
On 01/10/2013 08:41 AM, Ali Çehreli wrote:
> On 01/09/2013 04:14 PM, 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?
>
> It looks like you can use std.algorithm.isSorted instead:
>
> http://dlang.org/phobos/std_algorithm.html#isSorted
>
> assert(isSorted(di));
>
> Or perhaps:
>
> assert(isSorted(node.di.entry[0 .. di.count]));
>
> That code will disappear in release mode.
>
> Additionally, if the O(N) complexity of that check is not acceptable
> even in non-release mode there is std.range.assumeSorted:
>
> http://dlang.org/phobos/std_range.html#.assumeSorted
>
> assumeSorted makes very few comparisons so it may miss occasional sort
> issues. Also, it is not really for checking but more for range
> algorithms that require or work better with a sorted range.
>
> Ali
Unfortunately, entry is also a struct. key isn't its only member. If
there's some other reason to define a opCmp on entry, that would be a
good approach.
OTOH, if :
version(assert)
{...}
works as suggested by monarch_dodra above, that will allow me to avoid
the problem.
More information about the Digitalmars-d-learn
mailing list