Skip to content

cProfile with list comprehension is less obvious with Python 3.12 #107088

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

Closed
tim-schilling opened this issue Jul 23, 2023 · 5 comments
Closed

cProfile with list comprehension is less obvious with Python 3.12 #107088

tim-schilling opened this issue Jul 23, 2023 · 5 comments
Labels
3.12 only security fixes 3.13 bugs and security fixes docs Documentation in the Doc dir pending The issue will be closed if no feedback is provided

Comments

@tim-schilling
Copy link

Bug report

I believe that the cProfiler is less helpful now in python 3.12 when attempting to point a developer to a list comprehension as the bottleneck. With python 3.11, it points out that there is a listcomp that is responsible for the bulk of the duration. In 3.12, that line is missing.

Python 3.11.4

>>> import cProfile
>>> cProfile.run('[i for i in range(50000) if i % 2 == 0]')
         4 function calls in 0.009 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.009    0.009    0.009    0.009 <string>:1(<listcomp>)
        1    0.000    0.000    0.009    0.009 <string>:1(<module>)
        1    0.000    0.000    0.009    0.009 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Python 3.12.0b4

>>> cProfile.run('[i for i in range(50000) if i % 2 == 0]')
         3 function calls in 0.006 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.006    0.006    0.006    0.006 <string>:1(<module>)
        1    0.000    0.000    0.006    0.006 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Your environment

  • CPython versions tested on: 3.12.0b4
  • Operating system and architecture: Ubuntu 22.04.2 LTS
@AlexWaygood
Copy link
Member

At a guess, probably a result of PEP-709? (Cc. @carljm)

@AlexWaygood AlexWaygood added 3.12 only security fixes 3.13 bugs and security fixes labels Jul 23, 2023
@tim-schilling
Copy link
Author

I'm sorry, I didn't catch this. https://peps.python.org/pep-0709/#tracing-profiling-will-no-longer-show-a-call-return-for-the-comprehension

Tracing/profiling will no longer show a call/return for the comprehension

Naturally, since list/dict/set comprehensions will no longer be implemented as a call to a nested function, tracing/profiling using sys.settrace or sys.setprofile will also no longer reflect that a call and return have occurred.

@AlexWaygood
Copy link
Member

AlexWaygood commented Jul 23, 2023

I'm sorry, I didn't catch this.

Don't be sorry — it's our job to call out backwards-incompatible changes in the docs!

@carljm, do you think the impact on tracing/profiling from PEP-709 maybe deserves a bullet point in https://docs.python.org/3.12/whatsnew/3.12.html#pep-709-comprehension-inlining?

It already has a bullet point in the PEP-709 section of "What's New in Python 3.12". Hmm, not sure how we could document this anymore thoroughly than we already have. Unless we put a note in the cProfile docs as well? Not sure where that would go, though.

@AlexWaygood AlexWaygood added docs Documentation in the Doc dir pending The issue will be closed if no feedback is provided and removed type-bug An unexpected behavior, bug, or error labels Jul 23, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented Jul 24, 2023

Since this is already called out in PEP-709, and in "What's New in Python 3.12", I don't think there's any way we can further improve the docs here :/

when attempting to point a developer to a list comprehension as the bottleneck.

On the plus side, list comprehensions should also be much faster in Python 3.12 as a result of this change :-)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2023
@tim-schilling
Copy link
Author

Yup, yup. This makes sense. I was looking for a change in cProfile in 3.12, not with the comprehensions. Thank you @AlexWaygood!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes docs Documentation in the Doc dir pending The issue will be closed if no feedback is provided
Projects
None yet
Development

No branches or pull requests

2 participants