-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-26828: Add __length_hint__() to builtin map iterator #14432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
ccdd8d9
to
3229bda
Compare
This change is an optimization. We require benchmarks to prove that it makes Python faster. Test very shorts lists (ex: 0, 5 and 10 items), and some other lengthd (ex: 100, 1000, 10 000). |
3229bda
to
d6d90d1
Compare
Thank you, @vstinner , for your comment. I can run some ad-hoc benchmarks with timeit, and also try running It is conceivable that this change would significantly improve performance for large sizes, but make performance worse for very short iterators (under 8 elements), since more work is being done to compute the length hint, with no (re)allocation savings. |
Micro-benchmarks using pyperf time would be acceptable for me ;-) (I don't expect any significant impact on pyperformance benchmarks.)
Well, https://bugs.python.org/issue26828 has been closed... |
Here are the results of a microbenchmark as suggested by @vstinner. It took me some time to get a PGO/LTO build working. master: commit 97d15b1 Both were built with:
System specs: iMac with 3.8 GHz Intel Core i5, 16 GB DDR4 memory, mac OS 10.13.6 Profiled the following expression with
My takeaways are:
Next step will be pyperformance benchmarks. |
Here's output for the pyperformance comparison, filtering on only changes labeled "Significant":
Output shows two benchmarks slower and two faster.
|
That's why the idea of using PyObject_LengthHint() has been abandoned. Instead, _PyObject_HasLen() was introduced to check if it's possible to get an object length without having to call a Python function (a Python function call is "slow" in such context):
As I wrote previously, if you would like to explore the length hint path, you should experiment to add a new tp_length_hint slot to PyTypeObject (or maybe to PySequenceMethods and/or PyMappingMethods). The overhead of a Python function call is too high. |
cc @pablogsal |
https://bugs.python.org/issue26828 was rejected. Can this be closed? |
Yes, we can close this MR. I never had the change to add |
Add
__length_hint__()
method to the iterator returned by builtin functionmap
. This method returns a length hint if all the user-supplied arguments tomap
have length hints available.https://bugs.python.org/issue26828