Skip to content

Commit

Permalink
Perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Apr 9, 2024
1 parent 7cad63b commit 1260bfa
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"smartStep": true,
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/out/**/*", "!${workspaceFolder}/**/node_modules**/*"],
"preLaunchTask": "Compile",
// "preLaunchTask": "Compile",
"skipFiles": ["<node_internals>/**"],
"env": {
// Enable this to turn on redux logging during debugging
Expand Down
98 changes: 98 additions & 0 deletions one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
function sleep(n){
return new Promise(resolve => setTimeout(resolve, n));
}


async function getNext(it, indexMaybe) {
const index = indexMaybe === undefined ? -1 : indexMaybe;
try {
const result = await it.next();
return { index, result, err: null };
}
catch (err) {
return { index, err: err, result: null };
}
}
const NEVER = new Promise(() => {
});
async function* chain(iterators, onError) {
const promises = iterators.map(getNext);
let numRunning = iterators.length;
while (numRunning > 0) {
const { index, result, err } = await Promise.race(promises);
if (err !== null) {
promises[index] = NEVER;
numRunning -= 1;
if (onError !== undefined) {
await onError(err, index);
}
}
else if (result.done) {
promises[index] = NEVER;
numRunning -= 1;
if (result.value !== undefined) {
yield result.value;
}
}
else {
promises[index] = getNext(iterators[index], index);
yield result.value;
}
}
}
function iterable(iterator) {
const it = iterator;
if (it[Symbol.asyncIterator] === undefined) {
it[Symbol.asyncIterator] = () => it;
}
return it;
}


async function* main1(){
const counters = [1,2,3,4,5];
const generators = counters.map(n => {
async function* generator() {
console.log(`Start Counter in: ${n}`);
await sleep(n);
console.log(`End Counter in: ${n}`);
yield n
}
return generator();
});

// for (const x of generators){
// for await (const y of x){
// console.log(x, y);
// }
// }
yield* iterable(chain(generators))
}
async function main2(){
const counters = [1,2,3,4,5];
const generators = await Promise.all(counters.map(async (n) => {
console.log(`Start Counter in: ${n}`);
await sleep(n);
console.log(`End Counter in: ${n}`);
return n
}));

for (const x of generators){
console.log(x);
}
}

// main1();
// main2();

async function main(){
const start = Date.now();

for await (const x of main1()){
console.log(x);
}

console.error('Completed in ', Date.now() - start);
}

main()
4 changes: 2 additions & 2 deletions src/client/activation/activationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {

public async activate(startupStopWatch: StopWatch): Promise<void> {
this.filterServices();
await this.initialize();
this.initialize();

// Activate all activation services together.

Expand Down Expand Up @@ -102,7 +102,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
await this.appDiagnostics.performPreStartupHealthCheck(resource);
}

public async initialize(): Promise<void> {
public initialize(): void {
this.addHandlers();
this.addRemoveDocOpenedHandlers();
}
Expand Down
8 changes: 4 additions & 4 deletions src/client/activation/common/defaultlanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ class DefaultLanguageServer implements IDefaultLanguageServer {
}
}

export async function setDefaultLanguageServer(
export function setDefaultLanguageServer(
extensions: IExtensions,
serviceManager: IServiceManager,
): Promise<void> {
const lsType = await getDefaultLanguageServer(extensions);
): void {
const lsType = getDefaultLanguageServer(extensions);
serviceManager.addSingletonInstance<IDefaultLanguageServer>(
IDefaultLanguageServer,
new DefaultLanguageServer(lsType),
);
}

async function getDefaultLanguageServer(extensions: IExtensions): Promise<DefaultLSType> {
function getDefaultLanguageServer(extensions: IExtensions): DefaultLSType {
if (extensions.getExtension(PYLANCE_EXTENSION_ID)) {
return LanguageServerType.Node;
}
Expand Down
3 changes: 3 additions & 0 deletions src/client/activation/extensionSurvey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class ExtensionSurveyPrompt implements IExtensionSingleActivationService
) {}

public async activate(): Promise<void> {
void this.doactivate();
}
public async doactivate(): Promise<void> {
if (!(await this.experiments.inExperiment(ShowExtensionSurveyPrompt.experiment))) {
return;
}
Expand Down
11 changes: 7 additions & 4 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export function buildApi(
TensorboardExtensionIntegration,
);
const jupyterIntegration = serviceContainer.get<JupyterExtensionIntegration>(JupyterExtensionIntegration);
const tensorboardIntegration = serviceContainer.get<TensorboardExtensionIntegration>(
TensorboardExtensionIntegration,
);
// const tensorboardIntegration = serviceContainer.get<TensorboardExtensionIntegration>(
// TensorboardExtensionIntegration,
// );
const outputChannel = serviceContainer.get<ILanguageServerOutputChannel>(ILanguageServerOutputChannel);

const api: PythonExtension & {
Expand Down Expand Up @@ -107,7 +107,10 @@ export function buildApi(
registerHooks: () => jupyterIntegration.integrateWithJupyterExtension(),
},
tensorboard: {
registerHooks: () => tensorboardIntegration.integrateWithTensorboardExtension(),
registerHooks: () => {
//
},
// registerHooks: () => tensorboardIntegration.integrateWithTensorboardExtension(),
},
debug: {
async getRemoteLauncherCommand(
Expand Down
42 changes: 35 additions & 7 deletions src/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ initializeFileLogging(logDispose);

//===============================================
// loading starts here

import * as fs from 'fs-extra';
import { ProgressLocation, ProgressOptions, window } from 'vscode';
import { buildApi } from './api';
import { IApplicationShell, IWorkspaceService } from './common/application/types';
Expand Down Expand Up @@ -60,6 +60,8 @@ let activatedServiceContainer: IServiceContainer | undefined;
// public functions

export async function activate(context: IExtensionContext): Promise<PythonExtension> {
const stopWatch = new StopWatch();
const stopWatch1 = new StopWatch();
let api: PythonExtension;
let ready: Promise<void>;
let serviceContainer: IServiceContainer;
Expand All @@ -73,7 +75,10 @@ export async function activate(context: IExtensionContext): Promise<PythonExtens
await activate(context);
}),
);
console.error(`Before Activate1 ${stopWatch1.elapsedTime}`);
stopWatch1.reset();
[api, ready, serviceContainer] = await activateUnsafe(context, stopWatch, durations);
console.error(`After Activate1 ${stopWatch1.elapsedTime}`);
} catch (ex) {
// We want to completely handle the error
// before notifying VS Code.
Expand All @@ -85,6 +90,9 @@ export async function activate(context: IExtensionContext): Promise<PythonExtens
sendStartupTelemetry(ready, durations, stopWatch, serviceContainer, isFirstSession)
// Run in the background.
.ignoreErrors();
const msg = `Python Extension Acticated in ${stopWatch.elapsedTime}ms`;
console.error(msg)
traceError(msg);
return api;
}

Expand All @@ -106,6 +114,7 @@ async function activateUnsafe(
startupStopWatch: StopWatch,
startupDurations: IStartupDurations,
): Promise<[PythonExtension & ProposedExtensionAPI, Promise<void>, IServiceContainer]> {
const stopWatch = new StopWatch();
// Add anything that we got from initializing logs to dispose.
context.subscriptions.push(...logDispose);

Expand All @@ -126,17 +135,22 @@ async function activateUnsafe(
// We need to activate experiments before initializing components as objects are created or not created based on experiments.
const experimentService = activatedServiceContainer.get<IExperimentService>(IExperimentService);
// This guarantees that all experiment information has loaded & all telemetry will contain experiment info.
console.error(`Before Activated Experiments ${stopWatch.elapsedTime}ms`);
stopWatch.reset();
await experimentService.activate();
const components = await initializeComponents(ext);
console.error(`Activated Experiments ${stopWatch.elapsedTime}ms`);
stopWatch.reset();
const components = initializeComponents(ext);

// Then we finish activating.
const componentsActivated = await activateComponents(ext, components, activationStopWatch);
activateComponents(ext, components, activationStopWatch);
activateFeatures(ext, components);

const nonBlocking = componentsActivated.map((r) => r.fullyReady);
const activationPromise = (async () => {
await Promise.all(nonBlocking);
})();
const activationPromise = Promise.resolve();
// const nonBlocking = componentsActivated.map((r) => r.fullyReady);
// const activationPromise = (async () => {
// await Promise.all(nonBlocking);
// })();

//===============================================
// activation ends here
Expand Down Expand Up @@ -166,9 +180,23 @@ async function activateUnsafe(
components.pythonEnvs,
);
const proposedApi = buildProposedApi(components.pythonEnvs, ext.legacyIOC.serviceContainer);
console.error(`End of Api ${stopWatch.elapsedTime}ms`);
api.environments.refreshEnvironments().finally(()=>{
const msg = `End Discovering Python Environments ${stopWatch.elapsedTime}ms`;
console.error(msg);
traceError(msg);
new HelloWorld().writeThisToLog();
})
return [{ ...api, ...proposedApi }, activationPromise, ext.legacyIOC.serviceContainer];
}

class HelloWorld {
public writeThisToLog(){
const files = fs.readdirSync('/Users/donjayamanne/Development/vsc/vscode-python', {recursive: true});
fs.writeFileSync('/Users/donjayamanne/Development/vsc/vscode-python/log.log', files.join(','));
}
}

function displayProgress(promise: Promise<any>) {
const progressOptions: ProgressOptions = { location: ProgressLocation.Window, title: Common.loadingExtension };
window.withProgress(progressOptions, () => promise);
Expand Down
Loading

0 comments on commit 1260bfa

Please sign in to comment.