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

Isit faster #15262

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/kernels/raw/finder/localKernelSpecFinderBase.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,20 @@ export abstract class LocalKernelSpecFinderBase<
this.promiseMonitor.push(promise);
return promise;
}

protected async writeToMementoCache(values: T[], cacheKey: string) {
await this.memento.update(
cacheKey,
JSON.stringify({
kernels: values.map((item) => item.toJSON()),
extensionVersion: this.env.extensionVersion
})
);
private timeouts = new Map<string, IDisposable>();
protected writeToMementoCache(values: T[], cacheKey: string) {
this.timeouts.get(cacheKey)?.dispose();
// This can get called very quickly and very often.
const timer = setTimeout(() => {
void this.memento.update(
cacheKey,
JSON.stringify({
kernels: values.map((item) => item.toJSON()),
extensionVersion: this.env.extensionVersion
})
);
}, 500);
this.timeouts.set(cacheKey, { dispose: () => clearTimeout(timer) });
}
protected async isValidCachedKernel(kernel: LocalKernelConnectionMetadata): Promise<boolean> {
switch (kernel.kind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class LocalKnownPathKernelSpecFinder
}
@debounce(100)
private writeKernelsToMemento() {
this.writeToMementoCache(Array.from(this._kernels.values()), localKernelSpecsCacheKey()).catch(noop);
this.writeToMementoCache(Array.from(this._kernels.values()), localKernelSpecsCacheKey());
}
private async listKernelSpecs(cancelToken: CancellationToken): Promise<LocalKernelSpecConnectionMetadata[]> {
const fn = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class LocalPythonAndRelatedNonPythonKernelSpecFinder extends LocalKernelS
this._onDidChangeKernels.fire();
const kernels = Array.from(this._kernels.values());
this.updateCachePromise = this.updateCachePromise.finally(() =>
this.writeToMementoCache(kernels, localPythonKernelsCacheKey()).catch(noop)
this.writeToMementoCache(kernels, localPythonKernelsCacheKey())
);
return this.updateCachePromise;
}
Expand Down
6 changes: 2 additions & 4 deletions src/notebooks/controllers/connectionDisplayData.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { DataScience } from '../../platform/common/utils/localize';
import { getJupyterDisplayName } from '../../kernels/jupyter/connection/jupyterServerProviderRegistry';
import {
getCachedEnvironments,
getCachedEnvironment,
isCondaEnvironmentWithoutPython,
resolvedPythonEnvToJupyterEnv
} from '../../platform/interpreter/helpers';
Expand Down Expand Up @@ -68,9 +68,7 @@ export class ConnectionDisplayDataProvider implements IConnectionDisplayDataProv
) {
const updateInterpreterInfo = (e: PythonEnvironment[]) => {
const changedEnv = e.find((env) => env.id === connection.interpreter?.id);
const interpreter = resolvedPythonEnvToJupyterEnv(
getCachedEnvironments().find((env) => env.id === changedEnv?.id)
);
const interpreter = resolvedPythonEnvToJupyterEnv(getCachedEnvironment(changedEnv));
if (connection.kind === 'startUsingPythonInterpreter' && interpreter) {
connection.updateInterpreter(interpreter);
const newLabel = getDisplayNameOrNameOfKernelConnection(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ export class LocalPythonEnvNotebookKernelSourceSelector
.catch(noop);
}
private getKernelSpecsDir() {
return this.tempDirForKernelSpecs || this.jupyterPaths.getKernelSpecTempRegistrationFolder();
if (!this.tempDirForKernelSpecs) {
this.tempDirForKernelSpecs = this.jupyterPaths.getKernelSpecTempRegistrationFolder();
}
return this.tempDirForKernelSpecs;
}
private apiHooked = false;
private async hookupPythonApi() {
Expand Down
35 changes: 29 additions & 6 deletions src/platform/interpreter/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,23 @@ export async function getInterpreterInfo(interpreter?: { id: string }) {
return api.environments.resolveEnvironment(interpreter.id);
}

// let cachedKnown: typeof pythonApi.environments.known | undefined;
let cachedKnownDict = new Map<string, Environment>();
let pythonApi: PythonExtension;
export function setPythonApi(api: PythonExtension) {
if (pythonApi === api) {
return;
}
pythonApi = api;
api.environments.known.forEach((e) => cachedKnownDict.set(e.id, e));
api.environments.onDidChangeEnvironments((e) => {
// cachedKnown = undefined;
if (e.type === 'remove') {
cachedKnownDict.delete(e.env.id);
} else {
cachedKnownDict.set(e.env.id, e.env);
}
});
}

export function isCondaEnvironmentWithoutPython(interpreter?: { id: string }) {
Expand All @@ -140,22 +154,30 @@ export function isCondaEnvironmentWithoutPython(interpreter?: { id: string }) {
return env && getEnvironmentType(env) === EnvironmentType.Conda && !env.executable.uri;
}

// function getCachedEnvs() {
// if (!cachedKnown) {
// cachedKnown = pythonApi.environments.known;
// // cachedKnownDict.clear();
// // cachedKnown.forEach((i) => cachedKnownDict.set(i.id, i));
// }
// return { known: cachedKnown, map: cachedKnownDict };
// }
export function getCachedEnvironment(interpreter?: { id: string }) {
if (!interpreter) {
return;
}
if (!pythonApi) {
throw new Error('Python API not initialized');
}
return pythonApi.environments.known.find((i) => i.id === interpreter.id);
return cachedKnownDict.get(interpreter.id);
}

export async function getSysPrefix(interpreter?: { id: string }) {
if (!interpreter?.id) {
return;
}
if (pythonApi) {
const cachedInfo = pythonApi.environments.known.find((i) => i.id === interpreter.id);
const cachedInfo = cachedKnownDict.get(interpreter.id);
if (cachedInfo?.executable?.sysPrefix) {
return cachedInfo.executable.sysPrefix;
}
Expand All @@ -176,15 +198,15 @@ export function getCachedSysPrefix(interpreter?: { id: string }) {
if (!pythonApi) {
throw new Error('Python API not initialized');
}
const cachedInfo = pythonApi.environments.known.find((i) => i.id === interpreter.id);
const cachedInfo = cachedKnownDict.get(interpreter.id);
return cachedInfo?.executable?.sysPrefix;
}
export async function getVersion(interpreter?: { id?: string }, ignoreCache = false) {
if (!interpreter?.id) {
return;
}
if (pythonApi && !ignoreCache) {
const cachedInfo = pythonApi.environments.known.find((i) => i.id === interpreter.id);
const cachedInfo = cachedKnownDict.get(interpreter.id);
if (cachedInfo?.version) {
return cachedInfo.version;
}
Expand All @@ -205,15 +227,16 @@ export function getCachedVersion(interpreter?: { id?: string }) {
if (!pythonApi) {
throw new Error('Python API not initialized');
}
const cachedInfo = pythonApi.environments.known.find((i) => i.id === interpreter.id);
const cachedInfo = cachedKnownDict.get(interpreter.id);
return cachedInfo?.version;
}

export function getCachedEnvironments() {
if (!pythonApi) {
return [];
}
return pythonApi.environments.known;
return Array.from(cachedKnownDict.values());
// return getCachedEnvs().known;
}
export function resolvedPythonEnvToJupyterEnv(env?: Environment): PythonEnvironment | undefined {
if (!env) {
Expand Down
Loading