array.fromiter
and array_iter_extend
should take the __len__
and __length_hint__
of the iterator into account.
Otherwise filling the array from the iterator without planning for the array size properly might create unnecessary overhead.
array.fromiter
and array_iter_extend
should take the __len__
and __length_hint__
of the iterator into account.
Otherwise filling the array from the iterator without planning for the array size properly might create unnecessary overhead.
If you want, you could try to implement this and open an issue/PR. I would be careful to benchmark it though: If I remember right, some attempts at this sort of thing make the extra cost it.__length_hint__()
more expensive than any speed that was saved from a few allocations, especially when the arrays are short, so it might not end up actually paying off.
I believe there was also a suggestion to add a tp_length_hint C-level slot for a function pointer that would make the calling overhead reduced, but that does not exist at the moment.
Iterators don’t have either __len__
or __length_hint__
, so making array.fromiter check for one, and fail, and then check for the other, and fail again, will just cost time and effort for no benefit when the input is an iterator.