forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Performance improvements to load times of extension #931
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,79 +5,48 @@ import { inject, injectable } from 'inversify'; | |
import * as path from 'path'; | ||
import { Uri } from 'vscode'; | ||
import { IApplicationShell, IWorkspaceService } from '../../../common/application/types'; | ||
import { createDeferred, Deferred } from '../../../common/helpers'; | ||
import { IFileSystem } from '../../../common/platform/types'; | ||
import { IProcessService } from '../../../common/process/types'; | ||
import { getPythonExecutable } from '../../../debugger/Common/Utils'; | ||
import { IServiceContainer } from '../../../ioc/types'; | ||
import { IInterpreterLocatorService, IInterpreterVersionService, InterpreterType, PythonInterpreter } from '../../contracts'; | ||
import { IInterpreterVersionService, InterpreterType, PythonInterpreter } from '../../contracts'; | ||
import { CacheableLocatorService } from './cacheableLocatorService'; | ||
|
||
const execName = 'pipenv'; | ||
const CACHE_TIMEOUT = 2000; | ||
|
||
@injectable() | ||
export class PipEnvService implements IInterpreterLocatorService { | ||
export class PipEnvService extends CacheableLocatorService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified to inherit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I thought about cacheable too |
||
private readonly versionService: IInterpreterVersionService; | ||
private readonly process: IProcessService; | ||
private readonly workspace: IWorkspaceService; | ||
private readonly fs: IFileSystem; | ||
|
||
private pendingPromises: Deferred<PythonInterpreter[]>[] = []; | ||
private readonly cachedInterpreters = new Map<string, PythonInterpreter>(); | ||
|
||
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) { | ||
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) { | ||
super('PipEnvService', serviceContainer); | ||
this.versionService = this.serviceContainer.get<IInterpreterVersionService>(IInterpreterVersionService); | ||
this.process = this.serviceContainer.get<IProcessService>(IProcessService); | ||
this.workspace = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); | ||
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem); | ||
} | ||
|
||
public getInterpreters(resource?: Uri): Promise<PythonInterpreter[]> { | ||
// tslint:disable-next-line:no-empty | ||
public dispose() { } | ||
protected getInterpretersImplementation(resource?: Uri): Promise<PythonInterpreter[]> { | ||
const pipenvCwd = this.getPipenvWorkingDirectory(resource); | ||
if (!pipenvCwd) { | ||
return Promise.resolve([]); | ||
} | ||
|
||
// Try cache first | ||
const interpreter = this.cachedInterpreters[pipenvCwd]; | ||
if (interpreter) { | ||
return Promise.resolve([interpreter]); | ||
} | ||
// We don't want multiple requests executing pipenv | ||
const deferred = createDeferred<PythonInterpreter[]>(); | ||
this.pendingPromises.push(deferred); | ||
if (this.pendingPromises.length === 1) { | ||
// First call, start worker | ||
this.getInterpreter(pipenvCwd) | ||
.then(x => this.resolveDeferred(x ? [x] : [])) | ||
.catch(e => this.resolveDeferred([])); | ||
} | ||
return deferred.promise; | ||
} | ||
|
||
public dispose() { | ||
this.resolveDeferred([]); | ||
} | ||
|
||
private resolveDeferred(result: PythonInterpreter[]) { | ||
this.pendingPromises.forEach(p => p.resolve(result)); | ||
this.pendingPromises = []; | ||
} | ||
|
||
private async getInterpreter(pipenvCwd: string): Promise<PythonInterpreter | undefined> { | ||
const interpreter = await this.getInterpreterFromPipenv(pipenvCwd); | ||
if (interpreter) { | ||
this.cachedInterpreters[pipenvCwd] = interpreter; | ||
setTimeout(() => this.cachedInterpreters.clear(), CACHE_TIMEOUT); | ||
} | ||
return interpreter; | ||
return this.getInterpreterFromPipenv(pipenvCwd) | ||
.then(item => item ? [item] : []) | ||
.catch(() => []); | ||
} | ||
|
||
private async getInterpreterFromPipenv(pipenvCwd: string): Promise<PythonInterpreter | undefined> { | ||
const interpreterPath = await this.getInterpreterPathFromPipenv(pipenvCwd); | ||
if (!interpreterPath) { | ||
return; | ||
} | ||
|
||
const pythonExecutablePath = getPythonExecutable(interpreterPath); | ||
const ver = await this.versionService.getVersion(pythonExecutablePath, ''); | ||
return { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MikhailArkhipov
I reverted the change you made. I've added a
catch
indisplay/index.ts
which would handle cases where interpreter is invalid (does not exist).This fs check alone takes over 1.5 seconds on my Mac.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single file existence check? Wow.