pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Slowdown when enabling enableExperimentalFeatures on string-heavy files

Open correctmost opened this issue 5 months ago • 1 comments

Describe the bug

I ran Pyright on the psf/black codebase and noticed that it took much longer when enableExperimentalFeatures was set to true.

Code or Screenshots

Most of the time is spent checking one file that has a dictionary with 40,000+ string entries in it: https://github.com/psf/black/blob/94ebcb50851e40cba88c6ad2c14f9dfe45e08921/profiling/dict_huge.py

[tool.pyright]
enableExperimentalFeatures = false

$ time npx pyright ./profiling/dict_huge.py 
real	1m22.881s
user	1m22.398s
sys	0m0.646s
[tool.pyright]
enableExperimentalFeatures = true

$ time npx pyright ./profiling/dict_huge.py 
real	3m21.485s
user	2m54.655s
sys	0m25.637s

I looked at some profiler data and it seems like getTypeOfStringListAsType is a big culprit.

When I apply this dummy patch to avoid getTypeOfStringListAsType calls, I see similar performance with enableExperimentalFeatures = true and enableExperimentalFeatures = false:

diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts
index fa5750def..698198e32 100644
--- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts
+++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts
@@ -1718,7 +1718,8 @@ export function createTypeEvaluator(
         if (
             node.d.strings.length !== 1 ||
             node.d.strings[0].nodeType !== ParseNodeType.String ||
-            !isTypeFormSupported(node)
+            !isTypeFormSupported(node) ||
+            1 == 1
         ) {
             return typeResult;
         }

VS Code extension or command-line 9ed33cf7be6 (command-line version)

correctmost avatar Oct 08 '25 16:10 correctmost

so I think we need to set a 'maxInferredListLength' like tuples. https://github.com/microsoft/pyright/blob/9ed33cf7be6150dc53244e8d4009c06e4daedeae/packages/pyright-internal/src/analyzer/tuples.ts#L243-L264

bschnurr avatar Oct 08 '25 17:10 bschnurr