-
Notifications
You must be signed in to change notification settings - Fork 771
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
Pylance consumes 100-200% CPU and takes long time to process code with many layers of nested parentheses #2299
Comments
I didn't know if this belonged in the bug report or a comment, so I figured comment: the actual statement in my production code (which I anonymized in the above code) hasn't changed in many months, and I'm pretty sure this worked in a previous version of Pylance. Currently going back thru older versions to confirm this. |
Can confirm: Version 2021.12.22 works as expected. Starting in version 2022.1.0, this file takes a long time to parse and analyze. |
Thanks for the bug report. I'm able to repro the problem, and I understand the cause. It has to do with a new syntactic construct that is being added to Python 3.11 for callable types. The new syntax creates a situation in the parser that requires an arbitrary amount of lookahead. The current implementation is O(n^m) where "m" is the number of nested parentheses. In your sample, "m" is very large. I'll need to see if I can come up with a different approach in the parser. In the meantime, you can avoid this problem by breaking up your code into something that is not so deeply nested. |
I found a solution that mitigates the performance overhead in the parser. This change may not make it into this week's release of pylance, in which case it will be in next week's release. |
Thanks Eric! The code is actually generated and I don't control the generator; I've reverted back to 2021.12.2 for the time being and will take a look at next week's version when it is ready. |
This issue has been fixed in version 2022.2.0, which we've just released. You can find the changelog here: CHANGELOG.md |
Sorry, but I got a similar problem over the last weekend too... If I click on the appended py file under vs code, Pylance (2022.2.1) goes on a 100% CPU vacation and never comes back. I have to change the py file and restart the language server. All other py files work fine. But this file (and modified version of it) break Pylance. Version: 1.64.2 |
@davrot, we had a recent perf regression which caused the same symptom. Can you see if your issue is fixed in today's release of Pylance -- 2022.2.3? |
Just tested it with 2022.2.3 (see the log). Same problem. Sorry! |
@davrot, I'm able to repro the problem you're seeing. I've created a separate tracking bug in the pyright repo: microsoft/pyright#3082. |
Thank you! |
Environment data
Expected behaviour
When I make a change to a statement in a file, Pylance analyzes the file quickly (within a couple seconds max) and updates VSCode with the results.
Actual behaviour
When I change a specific statement with many layers of nested params (15 in the code below), Pylance takes >40 seconds to run per change, while still reporting the old error messages in VSCode. During this time, changes to other python files in the project are not error checked, and an Electron process is using 100-200% CPU time.
This will apparently stack up, e.g. if I make 2 changes to the statement, it will queue a second parse/analysis up to run after the first, which could lead to a scenario where it never (for all practical purposes) finishes.
XXX
Logs
Python Language Server Log
Code Snippet / Additional information
This is the statement, formatted using Black for readability. My project consists of an empty folder with test.py that contains this code. When I open this folder in VSCode, I see the above actual behavior.
The text was updated successfully, but these errors were encountered: