Skip to content

Python 3.12 runs much slower than Python 3.11 #1496

Closed

Description

@cvzi

Environment data

  • debugpy version: 1.8.0
  • OS and version: Windows 10
  • Python version: 3.12 64bit
  • Using VS Code

Actual behavior

Importing the emoji module (v2.10.0) with debugging enabled takes a long time with Python 3.12 compared to Python 3.11.

When running from VS Code with the default debug configuration via F5, the import takes 4 minutes on Python 3.12 but only 50ms on Python 3.11

The emoji module contains a large Python file, here. I suspect the slow debugging has something to do with this file.

Expected behavior

Similar run times in Python 3.11 and Python 3.12

Steps to reproduce:

  1. Install emoji module: pip install emoji==2.10.0
  2. Create a test file in VS Code that imports the emoji module:
    import time
    start = time.time()
    
    import emoji
    
    end = time.time()
    print("import time", end - start)
  3. Set VS Code to Python 3.12
  4. Run in VS Code by pressing F5

Activity

  1. int19h commented on Jan 22, 2024

    @int19h
    Contributor

    I suspect it's the large dict literal specifically that is the issue there. It sounds like in 3.12 it is reporting a debugger "line" event (which the debugger then has to process to check for breakpoints etc) for every subexpression in the literal, whereas before it was treated as a single atomic statement?

    If so, then this is to be expected - settrace-based Python debugging is inherently slow (~20x overhead per line of pure Python code, such as here). However, we're working on debugpy v2 utilizing the new sys.monitoring API in Python 3.12 for near-zero-overhead debugging; that should address this issue. And I think we should add this import as one of the test cases for that.

  2. lsmith77 commented on Jan 23, 2024

    @lsmith77

    Pavel Minaev (@int19h) it sounds like this will take a bit until it is ready. is there any quick fix/work-around for this? it also appears to affect spacy.

  3. bersbersbers commented on Mar 15, 2024

    @bersbersbers
    Contributor

    I think the original issue has been fixed by emoji v2.10.1. For anyone working on this, here's another example:

    bug.py:

    import timeit
    print(timeit.timeit("import colorcet", number=1))

    Then run:

    pip install colorcet debugpy
    python bug.py
    python -m debugpy --listen 5678 bug.py
    

    For me, this prints

    0.23384420000002137
    0.01s - Debugger warning: It seems that frozen modules are being used, which may
    0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
    0.00s - to python to disable frozen modules.
    0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
    15.49023370000009
    

    Python 3.11 takes ~0.03s vs. 0.05s, so even without the debugger, Python 3.11 seems to be an order of magnitude faster. But the difference is much more striking with the debugger.

    Implementation is at https://github.com/holoviz/colorcet/blob/main/colorcet/__init__.py. I also opened holoviz/colorcet#121.

  4. cvzi commented on Mar 15, 2024

    @cvzi
    Author

    Yes it was "solved" in emoji module by compressing the dict literal into a single line, previously the dict was spread over ~87k lines

  5. cvzi commented on May 17, 2024

    @cvzi
    Author

    FYI for anyone considering this solution, unfortunately compressing the dict into a single line has created a new problem for us, it causes coverage report on Python 3.12 to use excessive amounts of memory (in our case more than 20 GB).

  6. bersbersbers commented on Jun 7, 2024

    @bersbersbers
    Contributor

    causes coverage report on Python 3.12 to use excessive amounts of memory

    For the record, this issue (coveragepy/coveragepy#1785) should be gone with Python 3.12.4 thanks to python/cpython#119118, so the practicality of one-line workarounds should be improved. That is relevant since the original issue continues to exist in Python 3.12.4 (long runtime importing large dicts with debugger attached)

  7. Denelbe commented on Aug 29, 2024

    @Denelbe

    I have a similar problem with module python-opcua.
    The simple following script in vscode debugger runs in 2.7 seconds using Python 3.11.2 but 68 seconds with Python 3.12.5.

    from opcua import Server 
    server = Server()
    

    OS Linux (Debian 12)
    VsCode 1.92.2
    Python Debugger 2024.10.0

  8. snmishra commented on Aug 29, 2024

    @snmishra

    I've moved to pydevd. debugpy is unusable in Python 3.12, and I didn't want to go back to Python 3.11

  9. rchiodo commented on Aug 29, 2024

    @rchiodo
    Contributor

    Debugpy actually uses pydevd under the covers, but hasn't yet updated to pydevd's latest which has support for 3.12.

  10. laipz8200 commented on Sep 2, 2024

    @laipz8200

    I am currently debugging the repository langgenius/dify and am encountering a similar issue with the latest version, where the project takes an unusually long time to start (approximately 5 minutes) on my M1 Pro machine.

    I would like to understand the underlying cause of this delay and request guidance on how to resolve it.

    Environment Details:

    • Python Version: 3.12.5
    • Operating System: macOS 15.0
    • Chip: Apple M1 Pro

    Thank you for your assistance!

    Feel free to let me know if you would like any additional information.

  11. rchiodo commented on Sep 3, 2024

    @rchiodo
    Contributor

    I would like to understand the underlying cause of this delay and request guidance on how to resolve it.

    Sorry there's nothing you can do. We need to finish moving to the new 3.12 sys.monitoring support.

  12. deleted a comment from derzinn on Sep 4, 2024
  13. rteitge commented on Sep 9, 2024

    @rteitge

    Is there an approximate timetable for when we can start developing with vscode and devcontainer again? Since we are dependent on 3.12, vscode and devcontainer with the delays are unfortunately completely useless at the moment. That is not efficient working.

  14. rchiodo commented on Sep 9, 2024

    @rchiodo
    Contributor

    All I can say is porting the Pydevd changes for 3.12 to debugpy are in progress. Can't say how long it will take though.

  15. rchiodo commented on Sep 9, 2024

    @rchiodo
    Contributor

    This item:
    #1640

  16. rchiodo commented on Sep 18, 2024

    @rchiodo
    Contributor

    I'm pretty confident the pydev port will fix the performance issue.

    Here's the difference for me between the two with the colorcet example:

    With sys.monitoring

    PS C:\Users\rchiodo\source\testing\test_pylance>  c:; cd 'c:\Users\rchiodo\source\testing\test_pylance'; & 'c:\Users\rchiodo\source\testing\test_pylance\.venv312\Scripts\python.exe' 'C:\Users\rchiodo\source\repos\debugpy\src\debugpy\adapter/../..\debugpy\launcher' '63855' '--' 'C:\Users\rchiodo\source\testing\test_pylance\test_slowdebugger.py' 
    1.5255662000272423
    

    With sys.trace

    PS C:\Users\rchiodo\source\testing\test_pylance> ^C
    PS C:\Users\rchiodo\source\testing\test_pylance> 
    PS C:\Users\rchiodo\source\testing\test_pylance>  c:; cd 'c:\Users\rchiodo\source\testing\test_pylance'; & 'c:\Users\rchiodo\source\testing\test_pylance\.venv312\Scripts\python.exe' 'c:\Users\rchiodo\.vscode\extensions\ms-python.debugpy-2024.11.2024082901-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '63993' '--' 'C:\Users\rchiodo\source\testing\test_pylance\test_slowdebugger.py' 
    18.01146870001685
    
  17. rchiodo commented on Sep 23, 2024

    @rchiodo
    Contributor

    Reopening, hasn't shipped yet.

  18. added a commit that references this issue on Sep 23, 2024
    ae6812b
  19. rchiodo commented on Sep 24, 2024

    @rchiodo
    Contributor

    This issue should be fixed in the latest build of debugpy. It should ship with VS code in a future version of the Python Debugger Extension or you can install your own debugpy from pypi

  20. bersbersbers commented on Sep 25, 2024

    @bersbersbers
    Contributor

    Wow. 1.8.6 goes from 24s to 4s launching my app with debugger attached. Great work!

    I see this has made its way into https://github.com/microsoft/vscode-python-debugger already via microsoft/vscode-python-debugger@afd0d2b.

    Until that is released, this is how I was able to update the bundled debugpy in vscode-python-debugger:

    pip install "debugpy>=1.8.6" --upgrade --target "%USERPROFILE%\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs"
    
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions