forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speed up interpreter locators (#676)
performance improvements (fixes #666)
- Loading branch information
1 parent
d596beb
commit b09a421
Showing
21 changed files
with
192 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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
59 changes: 59 additions & 0 deletions
59
src/client/interpreter/locators/services/cacheableLocatorService.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { injectable } from 'inversify'; | ||
import { Uri } from 'vscode'; | ||
import { createDeferred, Deferred } from '../../../common/helpers'; | ||
import { IPersistentStateFactory } from '../../../common/types'; | ||
import { IServiceContainer } from '../../../ioc/types'; | ||
import { IInterpreterLocatorService, PythonInterpreter } from '../../contracts'; | ||
|
||
@injectable() | ||
export abstract class CacheableLocatorService implements IInterpreterLocatorService { | ||
private getInterpretersPromise: Deferred<PythonInterpreter[]>; | ||
private readonly cacheKey: string; | ||
constructor(name: string, | ||
protected readonly serviceContainer: IServiceContainer) { | ||
this.cacheKey = `INTERPRETERS_CACHE_${name}`; | ||
} | ||
public abstract dispose(); | ||
public async getInterpreters(resource?: Uri): Promise<PythonInterpreter[]> { | ||
if (!this.getInterpretersPromise) { | ||
this.getInterpretersPromise = createDeferred<PythonInterpreter[]>(); | ||
this.getInterpretersImplementation(resource) | ||
.then(async items => { | ||
await this.cacheInterpreters(items); | ||
this.getInterpretersPromise.resolve(items); | ||
}) | ||
.catch(ex => this.getInterpretersPromise.reject(ex)); | ||
} | ||
if (this.getInterpretersPromise.completed) { | ||
return this.getInterpretersPromise.promise; | ||
} | ||
|
||
const cachedInterpreters = this.getCachedInterpreters(); | ||
return Array.isArray(cachedInterpreters) ? cachedInterpreters : this.getInterpretersPromise.promise; | ||
} | ||
|
||
protected abstract getInterpretersImplementation(resource?: Uri): Promise<PythonInterpreter[]>; | ||
|
||
private getCachedInterpreters() { | ||
const persistentFactory = this.serviceContainer.get<IPersistentStateFactory>(IPersistentStateFactory); | ||
// tslint:disable-next-line:no-any | ||
const globalPersistence = persistentFactory.createGlobalPersistentState<PythonInterpreter[]>(this.cacheKey, undefined as any); | ||
if (!Array.isArray(globalPersistence.value)) { | ||
return; | ||
} | ||
return globalPersistence.value.map(item => { | ||
return { | ||
...item, | ||
cachedEntry: true | ||
}; | ||
}); | ||
} | ||
private async cacheInterpreters(interpreters: PythonInterpreter[]) { | ||
const persistentFactory = this.serviceContainer.get<IPersistentStateFactory>(IPersistentStateFactory); | ||
const globalPersistence = persistentFactory.createGlobalPersistentState<PythonInterpreter[]>(this.cacheKey, []); | ||
await globalPersistence.updateValue(interpreters); | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.