While you’re at it, similar improvements should be possible for min
, max
, any
and all
this way: any
and all
can check multiple small integral values at once by type punning; min
and max
can bail out if they encounter the extreme values for the type. (There are some bit-masking techniques to allow rejecting multiple small integral values at once with min
and max
, too, but the overhead might not be worth it.)
(You might also be interested in my corresponding previous (not well received) proposal, to add dunder hooks so that other types can specify these kinds of optimizations - or implement those methods for e.g. non-iterable container abstractions, such as an interval on the real number line. Of course, that latter doesn’t have a sum, but you get the idea.)