This check is pretty much free in background of __getitem__
.
Btw, I am uncertain about whether it would be possible to retain performance benefits after properly designing such. I.e.:
# What would this do?
a[END - n:]
# END - n ???
class End:
offset = 0
def __init__(self, offset=0):
self.offset = offset
def __sub__(self, other):
return End(self.offset - other)
END = End(0)
And this would certainly kill the performance.
So the only benefiting case would be when exact END
is required: e.g. a[END:]
.
If it is the most common use case, then it be fine, but I prefer solutions that are âone best wayâ for all (or at least vast majority of) casesâŚ
Thus, from performance perspective, I instead would probably try:
a) further improving performance of function calls
b) looking into possibility to specialise len
Regarding (b), I am not sure how realistic is that, but if it is, then it could be applied to other builtins as well.
For example, while obj[item]
can be used programatically, obj.attr
needs to be used differently:
%timeit Ellipsis.__class__ # 14 ns
%timeit getattr(Ellipsis, '__class__') # 46 ns
I would be very satisfied if performance of the latter would catch up with the former.
Could maybe be something along the lines of:
if len is builtins.len:
# Do not do explicit call, but run it more efficiently in C
Given 99% of the time builtin names are not overridden, this might be a good idea.
Such could be applied to certain portion of builtins, which might result in non-negligible performance boost.
And I think this is the wrong thread for thisâŚ