GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sys.settrace dramatic slowdown in 3.12 · Issue #107674 · python/cpython

sys.settrace dramatic slowdown in 3.12 #107674

New issue
Copy link
New issue
Copy link
Closed
Closed
sys.settrace dramatic slowdown in 3.12#107674
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@nedbat
nedbat
opened on Aug 5, 2023
Issue body actions

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker, and am confident this bug has not been reported before

A clear and concise description of the bug

A bug report in coverage (coveragepy/coveragepy#1665) is reduced here to a pure do-nothing trace function for sys.settrace.

Reproduction:

% git clone https://github.com/nedbat/ndindex

% cd ndindex

% python3.11 -VV
Python 3.11.4 (main, Jun  7 2023, 08:42:37) [Clang 14.0.3 (clang-1403.0.22.14.1)]

% python3.11 -m venv v311

% ./v311/bin/pip install numpy

% python3.12 -VV
Python 3.12.0b4 (main, Jul 11 2023, 20:38:26) [Clang 14.0.3 (clang-1403.0.22.14.1)]

% python3.12 -m venv v312

% ./v312/bin/pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy

% # Run the code without the trace function.
% for x in 1 2 3; time ./v311/bin/python -m ndindex.tests.justdoit notrace
./v311/bin/python -m ndindex.tests.justdoit notrace  7.05s user 0.37s system 108% cpu 6.813 total
./v311/bin/python -m ndindex.tests.justdoit notrace  7.04s user 0.34s system 109% cpu 6.724 total
./v311/bin/python -m ndindex.tests.justdoit notrace  7.00s user 0.35s system 109% cpu 6.696 total

% # 3.12 is slightly faster without the trace function.
% for x in 1 2 3; time ./v312/bin/python -m ndindex.tests.justdoit notrace
./v312/bin/python -m ndindex.tests.justdoit notrace  6.56s user 0.30s system 106% cpu 6.422 total
./v312/bin/python -m ndindex.tests.justdoit notrace  6.48s user 0.31s system 106% cpu 6.359 total
./v312/bin/python -m ndindex.tests.justdoit notrace  6.38s user 0.28s system 107% cpu 6.217 total

% # 3.11 with tracing is about 3x slower.
% for x in 1 2 3; time ./v311/bin/python -m ndindex.tests.justdoit trace
./v311/bin/python -m ndindex.tests.justdoit trace  22.64s user 0.51s system 101% cpu 22.772 total
./v311/bin/python -m ndindex.tests.justdoit trace  21.92s user 0.46s system 101% cpu 21.979 total
./v311/bin/python -m ndindex.tests.justdoit trace  21.55s user 0.35s system 102% cpu 21.379 total

% # 3.12 with tracing is 7x slower.
% for x in 1 2 3; time ./v312/bin/python -m ndindex.tests.justdoit trace
./v312/bin/python -m ndindex.tests.justdoit trace  49.47s user 0.40s system 100% cpu 49.676 total
./v312/bin/python -m ndindex.tests.justdoit trace  49.53s user 0.39s system 100% cpu 49.784 total
./v312/bin/python -m ndindex.tests.justdoit trace  50.44s user 0.38s system 100% cpu 50.739 total

I don't know if the unusual numpy build has something to do with this.

Linked PRs

  • GH-107674: Avoid allocating boxed ints for sys.settrace line events #107780
  • [3.12] GH-107674: Avoid allocating boxed ints for sys.settrace line events (GH-107780) #107841
  • gh-107674: Improve performance of sys.settrace #114986
  • gh-107674: Improve performance of sys.settrace #117133
  • gh-107674: Remove some unnecessary code in instrumentation code #117393
  • gh-107674: Lazy load line number to improve performance of tracing #118127
Reacted by Niko Föhr and Cornelius Roemer

Activity

Next
  1. nedbat
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 5, 2023
  2. neonene commented on Aug 6, 2023

    @neonene
    neonene
    on Aug 6, 2023
    Contributor
    More actions

    This issue can be seen starting from the commit 411b169 with usual numpy-1.25.2.

  3. AlexWaygood commented on Aug 7, 2023

    @AlexWaygood
    AlexWaygood
    on Aug 7, 2023
    Member
    More actions

    cc. @markshannon

  4. Eclips4
    added
    performancePerformance or resource usagePerformance or resource usage
    on Aug 8, 2023
  5. markshannon
    added a commit that references this issue on Aug 10, 2023

    GH-107674: Avoid allocating boxed ints for `sys.settrace` line events (…

    37d8b90
  6. miss-islington
    added a commit that references this issue on Aug 10, 2023

    pythonGH-107674: Avoid allocating boxed ints for `sys.settrace` line …

    7d8d876
  7. nedbat commented on Oct 24, 2023

    @nedbat
    nedbat
    on Oct 24, 2023
    MemberAuthor
    More actions

    @markshannon Is there any news about this?

  8. nedbat commented on Oct 30, 2023

    @nedbat
    nedbat
    on Oct 30, 2023
    MemberAuthor
    More actions

    @iritkatriel perhaps you have some idea?

  9. iritkatriel commented on Oct 31, 2023

    @iritkatriel
    iritkatriel
    on Oct 31, 2023
    Member
    More actions

    @iritkatriel perhaps you have some idea?

    Did #107780 help?

  10. iritkatriel
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Dec 1, 2023
  11. gaogaotiantian commented on Feb 4, 2024

    @gaogaotiantian
    gaogaotiantian
    on Feb 4, 2024
    Member
    More actions

    Hi @nedbat , I circled back to this issue. If you are still interested, could you try the patch of #114986 and see if the result is improved?

  12. nedbat commented on Feb 15, 2024

    @nedbat
    nedbat
    on Feb 15, 2024
    MemberAuthor
    More actions

    @gaogaotiantian Thanks. I tried #114986, and it didn't run faster. I used the https://github.com/Quansight-Labs/ndindex repo test suite:
    3.11: 160s
    3.12: 255s
    3.13 main: 248s
    pr114986: 245s

    I wish I had better news.

  13. gaogaotiantian commented on Feb 15, 2024

    @gaogaotiantian
    gaogaotiantian
    on Feb 15, 2024
    Member
    More actions

    Oh, did you test it with an empty trace function?

  14. nedbat commented on Feb 15, 2024

    @nedbat
    nedbat
    on Feb 15, 2024
    MemberAuthor
    More actions

    I will do more tests, but the ultimate goal is to perform better in real-world scenarios. Here is more data from a subset of the test suite (-k test_chunking), which looks much more promising. It's hard to get consistent numbers, but this looks good. I would merge this change.

    Python version no tracing Python trace C trace
    3.11.8 1.19s 7.39s 2.46s
    3.12.2 1.12s 11.57s 3.71s
    3.13.0a3+ heads/main:a95b1a56bb 1.14s 10.37s 3.89s
    3.13.0a3+ heads/pr/114986:cb09c55758 1.14s 6.48s 2.91s
  15. gaogaotiantian commented on Feb 15, 2024

    @gaogaotiantian
    gaogaotiantian
    on Feb 15, 2024
    Member
    More actions

    Thank you for the test. Yes the PR only improves the performance of the actual trace function - which is normally the major cost for tracing tools. Good to know it helps with the more real-life benchmark. Unfortunately, due to the change of the tracing mechanism, the overhead for sys.settrace will always be larger than the old way - I did not see a quick solution to that.

  16. 46 remaining items

  17. nedbat commented on Feb 12, 2025

    @nedbat
    nedbat
    on Feb 12, 2025
    MemberAuthor
    More actions

    I continue to get people reporting serious performance problems on 3.12. Is there any chance of an improvement?

  18. junkmd commented on Feb 14, 2025

    @junkmd
    junkmd
    on Feb 14, 2025
    Contributor
    More actions

    Considering the issue observed in enthought/comtypes#775, where execution time increased nearly tenfold, there is a performance "regression" in Python 3.12.

    Only one bugfix release remains for Python 3.12:
    https://pythonpeps.python.org/pep-0693/

    If #107841 is not merged by then, I believe the Python community will miss an opportunity to reconsider this issue.

    If "the fix patch" is merged before Python 3.12 transitions to security-fix only status, I would accept enabling coverage measurement in the comtypes CI tests for all Python versions (and waiting until 3.12.10 is released). Otherwise, only Python 3.12 will be tested without coverage measurement in the comtypes CI pipeline.

    As discussed in #107841, I understand that this issue doesn't involve "broken" programs but rather a "performance improvement", and that the changes are complex to accept during the bugfix phase. However, fixing this could potentially benefit projects and the wider ecosystem running on Python 3.12.

    I would like to, once again, hear the opinions of the core developers and the community on this matter.

    Reacted by Cornelius Roemer
  19. gaogaotiantian commented on Feb 14, 2025

    @gaogaotiantian
    gaogaotiantian
    on Feb 14, 2025
    Member
    More actions

    Ah, @markshannon merged #128350 in main but did not backport it to 3.12 and 3.13 due to conflicts. #107841 is not the problem fixer. I believe backporting #128350 should fix the issue in 3.12 and 3.13.

    Reacted by Jun Komoda
  20. junkmd
    added 2 commits that reference this issue on Feb 17, 2025

    poc: python/cpython#107674

    a0172e0

    poc:

    23ffb19
  21. junkmd commented on Feb 18, 2025

    @junkmd
    junkmd
    on Feb 18, 2025
    Contributor
    More actions

    I tested whether #107841 is the problem fixer.

    I built Python from v3.12.8 and also from the 3.12 branch with 37d8b90 cherry-picked.
    And I compared the execution time of comtypes test cases.

    with cov 37d8b90 changes Python 3.12.8
    yes 1101.106s 1254.337s
    no 57.869s 46.481s

    The build with 37d8b90 changes is about 153s faster than v3.12.8.
    However, this performance improvement is still not significant.

    I haven't tested backporting #128350 yet because I'm not sure how to resolve conflicts.

  22. facebook-github-bot
    added 2 commits that reference this issue on Apr 1, 2025

    python 3.12 coverage regression workaround

    e5afdd4

    python 3.12 coverage regression workaround

    f2b8759
  23. Yhg1s
    added a commit that references this issue on Apr 6, 2025

    [3.12] GH-107674: Avoid allocating boxed ints for `sys.settrace` line…

    55b2303
  24. gaogaotiantian commented on Apr 25, 2025

    @gaogaotiantian
    gaogaotiantian
    on Apr 25, 2025
    Member
    More actions

    I think we can close this issue now. The fix did not make 3.12 as it missed the last bug fix release. It was backported to 3.13 though. I don't think we have more tasks for this issue anymore.

  25. jack-mcivor commented on Jun 17, 2025

    @jack-mcivor
    jack-mcivor
    on Jun 17, 2025
    More actions

    The fix did not make 3.12 as it missed the last bug fix release.

    Is this true? I found this issue from the changelog of 3.12.10, which seems to suggest the fix made it in?

    gh-107674: Fixed performance regression in sys.settrace.

  26. skshetry
    added 2 commits that reference this issue on Jul 11, 2025

    improve performance of tests in Python>=3.12 when used with coverage

    4c13997

    improve performance of tests in Python>=3.12 when used with coverage

    0c850f7
  27. corneliusroemer commented on Jul 20, 2025

    @corneliusroemer
    corneliusroemer
    on Jul 20, 2025
    Contributor
    More actions

    I think we can close this issue now. The fix did not make 3.12 as it missed the last bug fix release. It was backported to 3.13 though. I don't think we have more tasks for this issue anymore.

    Yep, closed as not planned/won't fix. The 3.13 performance looks as good as 3.11, even better in my own benchmarks against https://github.com/nextstrain/augur with pytest on an M4 Max using conda-forge binaries.

    Python 3.13.5

    no-coverage: Time (mean ± σ):      5.777 s ±  0.178 s    [User:  6.464 s, System: 2.464 s]
    coverage   : Time (mean ± σ):      8.381 s ±  0.217 s    [User: 11.473 s, System: 4.635 s]
    

    Python 3.12.11

    no-coverage: Time (mean ± σ):      5.913 s ±  0.079 s    [User:  6.347 s, System: 2.519 s]
    coverage   : Time (mean ± σ):     25.778 s ±  0.190 s    [User: 28.985 s, System: 4.644 s]
    

    Python 3.11.13

    no-coverage: Time (mean ± σ):      5.912 s ±  0.228 s    [User:  6.500 s, System: 2.480 s]
    coverage   : Time (mean ± σ):      8.806 s ±  0.332 s    [User: 12.167 s, System: 4.493 s]
    
    Reacted by H Włodarczyk
  28. hugovk
    closed this as completedon Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.