Performance optimization for minElement and maxElement

Steven Schveighoffer schveiguy at gmail.com
Mon Sep 12 15:27:25 UTC 2022


On 9/12/22 10:59 AM, H. S. Teoh wrote:
> On Mon, Sep 12, 2022 at 10:15:45AM -0400, Steven Schveighoffer via Digitalmars-d wrote:

>> A possibility is to provide an overload that accepts the absolute
>> minimum or maximum, and only then does it do the extra comparisons.
> [...]
> 
> Please don't. This smells like one of those well-intentioned extensions
> that 5 years later will have us wondering "how did Phobos become this
> hairy?".

This isn't that crazy though. Checking against an absolute minimum or 
maximum has a cost, one which you may not want to incur.

For instance, uint.min is 0, this might be a common occurrence in some 
ranges. But int.min is -2billion. That might *never* occur in some 
ranges. Why waste time checking for something that will never occur in 
the name of "performance"? For many types and data sets, this is a net 
pessimization.

And there's not a way to do this shortcut via type wrapping. The only 
option is to alter the algorithm. Making it opt-in isn't a bad idea, but 
of course, the API could be badly designed.

-Steve


More information about the Digitalmars-d mailing list