Formatting is slow using VS Code
Describe the bug This is not a bug, it is an efficiency issue. I'm using black to format code in VS Code on my laptop, and it seems to be slow, I can clearly see the temporary file in the workspace and VS Code showing 'Formatting with black...'. Saving a small file takes about 2 seconds.
I have been using black on my desktop computers for a long time (with an E3-1231 CPU, and an R5 5600G, also in VS Code), and I never felt a problem with speed. Now I'm using it on my laptop (with an i5 1240P CPU). I don't know why the formatting becomes slower.
When I use the command line tool to format the code, it runs fast, much faster than using black as a formatter in VS Code. I've tried passing the --fast flag to VS Code but it didn't help.
Environment
- Black's version: 22.10
- OS and Python version: Windows 11, Python3.8
Additional context
I also tried to pass the --verbose flag, but I could not find the output in VS Code. Any advice on identifying the issue or improving speed would be appreciated.
Are you using the new VSCode extension for Black? It might be using the vendored Black binary it ships with; this binary won't be compiled unlike the binaries from PyPI. This would at most cause a 2x slowdown, but the slowdown you're reporting sounds more severe than that. Not sure what's up ...
Hey @ichard26, thanks for your help.
Are you using the new VSCode extension for Black?
No, I'm using "python.formatting.provider": "black" and "editor.formatOnSave": true in VS Code. I used conda to install Black. I don't really know how VS Code formats the files through Black, but I'm pretty sure I'm not using the Black extension.
I tried reinstalling Black and reinstalling VS Code, but that didn't help. It's slower than before, but I can live with that.
Strange. I have never used conda so I don't know how that fits into the picture here.
I've encontered this problem too, using a stopwatch it is about 3.30 seconds (52 lines of code) with ssh remote. The CPU is intel E5-2695 V2 Just install black from conda and using black like the OP information:
$black --version
black, 22.10.0 (compiled: no)
Python (CPython) 3.9.13
Tried black formatter extension, it is running without lag
Running into the same issue. My environment:
M1 Pro, macOS 13.3.1, vscode 1.77.1 (with python plugin, without black plugin)
vscode settings:
"[python]": {
"editor.formatOnType": true,
},
"python.formatting.provider": "black",
$ black --version
black, 0.0 (compiled: no)
Python (CPython) 3.10.9
Latest tensorflow for mac installed using following instructions: https://developer.apple.com/metal/tensorflow-plugin/
Formatting sample 14 LOC from the above setup guide takes a while. The delay between saving the file and seeing formatted changes is very noticable. I would expect better coming from JS/TS and formatting with prettier (it's pretty much instant on a 20x bigger file).
Is this normal behavior?
While I found that black has always been waaay faster compared to flake8
After we moved to ruff a while back, I have found complaints about black being really slow ! :)
I assume this is most likely just because people have gotten used to ruff's insane speed... but maybe black has become slower too ? (We use vscode + wsl)
Some stats from my project for black CLI:
- Cold start on entire project: 50sec
- Warm start on entire project: 3sec
- Cold start on 1 file: 1sec
- Warm start on 1 file: 0.4sec
In comparison - ruff CLI takes:
- Cold start on entire project: 2sec
- Warm start on entire project: 0.05sec
- Cold start on 1 file: 0.05sec
- Warm start on 1 file: 0.01sec
I don't think there's an actionable issue here for us. Possibly there is a problem with some editor's integration.