Skip to content
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

Experimental - running PymodeRopeRegenerate in background #1041

Open
wants to merge 2 commits into
base: develop
from

Conversation

@lieryan
Copy link
Contributor

@lieryan lieryan commented Oct 17, 2019

Ok, this is a bit of a wild experiment. This is currently only for neovim.

This branch offloads slow rope tasks such as :PymodeRopeRegenerate in a background thread. This means that the editor remains responsive while rope is loading, which can take a while in larger projects. Completion and refactoring may not be available until the cache regeneration is actually completed.

The background task handler currently only works in neovim. In regular vim, it currently uses a dummy task handler that still runs rope in foreground so the editor still locks up.

Currently, there are a few errors/exceptions that sometimes pops up in message if you try to use completion/refactoring while the cache is still building. This is ugly and makes nvim looks somewhat crashy, spewing out errors, but it doesn't affect functionality and I already find it very useful to not have the editor locks up unexpectedly.

@lieryan lieryan changed the title Experimental BackgroundProgressHandler Experimental - running PymodeRopeRegenerate in background Oct 17, 2019
@lieryan lieryan force-pushed the lieryan:lieryan-background-handler branch from 2e092f0 to c2aecad Apr 25, 2020
@stale
Copy link

@stale stale bot commented Sep 27, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Sep 27, 2020
@danuker
Copy link

@danuker danuker commented Oct 5, 2020

First of all, thanks for showing interest in this plugin. I am evaluating Vim and also looking at this plugin - fast & automatic refactoring is very important.

I have a question: can a Vim plugin not use traditional Python parallelization, like suprocesses (since I suspect this is CPU-bound, so threading would be subject to the Python GIL)?

It seems even the NeoVim docs says vim.async_call will block for long-running computation.

Pardon my ignorance; I have no experience with Vim plugins, but I'd love to hear your thoughts.

@stale stale bot removed the inactive label Oct 5, 2020
@lieryan
Copy link
Contributor Author

@lieryan lieryan commented Oct 5, 2020

From my experience with refactoring in a fairly large Python project, python-mode and rope is always fast enough once the project is indexed. I never had a refactoring operation takes more than 2-3 seconds to plan and complete a project-wide refactoring, and autocompletion is pretty much always instant.

rope has two kind of indexing operations, the initial indexing can take a while but subsequent reindex on the same vim session is generally quite fast. The problem with the re-index operation is that currently they run on the foreground thread and would block you from doing anything until the indexing operation finishes. What this PR does is to put these indexing operations in a background thread so you can continue browsing and editing the file.

In smaller projects, the initial indexing time is generally quite negligible, but in larger projects it can takes a bit of time. For a sense of scale, scanning Django source code, which weighs at about 120kLOC (minus its own tests suite) of Python code takes about 4 seconds, and pandas which weighs at 400kLOC (with its test suite) of Python code is about 12 seconds (i7-6500U CPU @ 2.50GHz, m.2 SSD).

Generally, the initial indexing operation is only an issue if you start a new Vim instance for each edit you do (unlike most IDEs, Vim starts and stops instantly, so many Vim users are used to this style of editing), and python-mode has to do a full-scan of your project when you just started Vim, since a file might be changed outside of Vim without updating rope index. If you are working on a big project, you'd generally want to keep a long-running edit session. That way, the time needed for the initial indexing is usually not going to be a practical issue, as subsequent re-index usually takes less than half a second or so even in large projects.

can a Vim plugin not use traditional Python parallelization, like suprocesses (since I suspect this is CPU-bound, so threading would be subject to the Python GIL)?

I am not aware of anything that would prevent Python from using threading or multiprocessing in either NeoVim or Vim. However, AFAIK, rope is only single-threaded, so you'll have to re-engineer rope to somehow split up its code analysis logic and deal with merging the results. That needs to happen at rope level, so python-mode wouldn't be the place to have that discussion. That said, I don't think parallelizing rope is going to be really necessary, even for large projects such as those I mentioned above, rope's refactoring is already fast enough to usually not be the bottleneck when refactoring.

It seems even the NeoVim docs says vim.async_call will block for long-running computation.

That shouldn't really be an issue. async_call is used to run a callback in the main foreground thread, which should never need to do anything more than updating the UI. The bulk of the indexing work is done by the background thread.

since I suspect this is CPU-bound, so threading would be subject to the Python GIL?

The background thread in Python is a real OS thread, so the OS will pre-empt the background thread and schedule its with the frontend as necessary. GIL would only be an issue if you try to parallelize the code analysis itself, and that's a rope issue.

@danuker
Copy link

@danuker danuker commented Oct 6, 2020

Wow, thank you for the awesome explanation! I had no idea the indexing is so fast; I am excited to try it out as soon as I get the chance! Hats off to you, sir!

@lieryan
Copy link
Contributor Author

@lieryan lieryan commented Oct 6, 2020

Just be careful that you don't try to edit a Python file with your home directory (or root or other such large, non project directories) as your current working directory while python-mode is enabled. It will try to scan your entire home directory and that might lock up Vim for a very long while if you have lots of files at home.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.