8000 Use .get_cached_default_environment() from jedi (#406) · brandonwillard/python-lsp-server@63e3cad · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 63e3cad

Browse files
andrew-christiansongatesn
authored andcommitted
Use .get_cached_default_environment() from jedi (python-lsp#406)
In some situations, pyls can call Document.sys_path _very_ frequently. That method calls jedi.api.environment.get_default_environment(), which can call jedi.api.environment.find_system_pythons(). On systems with pyenv and many python versions installed, this can be expensive, as jedi._compatibilty.which returns a shim path for python major version. Each found version spawns a subprocess to check the specific version of the python found at the path (via creation of a jedi Environment). However, if it's only found the pyenv shim, when a subprocess is spawned, the shim fails to find the version, and pyenv spends significant time looking for the requested command in other installed pythons. Calling .get_cached_default_environment insures jedi goes through the above process no more than once per 10 minutes per instance of pyls. Even on systems without pyenv, calling get_default_environment() directly results in at least one subprocess invocation, and a new jedi Environment object for each invocation. As Document.sys_path can be called as often as once per keypress (depending on the client, though this seems to be the case for lsp-mode) reducing work on that path provides noticeable improvement in performance.
1 parent b0b6eeb commit 63e3cad

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

pyls/workspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def sys_path(self):
218218
path = list(self._extra_sys_path)
219219

220220
# TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
221-
environment = jedi.api.environment.get_default_environment()
221+
environment = jedi.api.environment.get_cached_default_environment()
222222
path.extend(environment.get_sys_path())
223223

224224
return path

0 commit comments

Comments
 (0)
0