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

Minor refactoring #7837

Merged
merged 3 commits into from
Oct 6, 2021
Merged

Minor refactoring #7837

merged 3 commits into from
Oct 6, 2021

Conversation

DonJayamanne
Copy link
Contributor

@DonJayamanne DonJayamanne commented Oct 6, 2021

Was looking at fixing #7697, but then realized its not going to be easy.

This PR only refactors some code that I came across while attempting to fix the issue (fixes have been reverted, but left refactoring)

  • When we start debugger, why should we clera the list of variables?
    • I think we should not clear variables every time just because we opened another panel
    • I think we can easily cache the variables & fetch variables ONLY when execution count has changed (or restarted kernels, etc)
  • Right now, when we start debugging, we hide variable view & then display it
    • & now we're fetching variables all over again unnecessarily

This can be solved better by only fetching variables when reqruied.
E.g. when using run by line and running to completion I can see variables being requested 4 times, when it should be fetched just once.

@codecov-commenter
Copy link

codecov-commenter commented Oct 6, 2021

Codecov Report

Merging #7837 (be37e66) into main (d448a02) will decrease coverage by 0%.
The diff coverage is 80%.

❗ Current head be37e66 differs from pull request most recent head ea04d00. Consider uploading reports for the commit ea04d00 to get more accurate results

@@          Coverage Diff           @@
##            main   #7837    +/-   ##
======================================
- Coverage     70%     69%    -1%     
======================================
  Files        363     362     -1     
  Lines      22299   22274    -25     
  Branches    3373    3373            
======================================
- Hits       15726   15486   -240     
- Misses      5215    5450   +235     
+ Partials    1358    1338    -20     
Impacted Files Coverage Δ
src/client/datascience/jupyter/kernels/types.ts 100% <ø> (ø)
src/client/datascience/types.ts 100% <ø> (ø)
src/client/datascience/jupyter/kernels/helpers.ts 71% <33%> (-1%) ⬇️
...client/datascience/commands/activeEditorContext.ts 87% <42%> (-5%) ⬇️
...lient/datascience/variablesView/notebookWatcher.ts 85% <84%> (-4%) ⬇️
...ient/datascience/jupyter/kernels/kernelProvider.ts 97% <90%> (-1%) ⬇️
...atascience/interactive-window/interactiveWindow.ts 54% <100%> (+<1%) ⬆️
src/client/datascience/jupyter/kernels/kernel.ts 73% <100%> (ø)
...t/common/application/webviewPanels/webviewPanel.ts 8% <0%> (-62%) ⬇️
...cience/data-viewing/jupyterVariableDataProvider.ts 12% <0%> (-54%) ⬇️
... and 25 more

@@ -164,6 +164,7 @@ export interface IKernelProvider extends IAsyncDisposable {
readonly kernels: Readonly<IKernel[]>;
onDidRestartKernel: Event<IKernel>;
onDidDisposeKernel: Event<IKernel>;
onKernelStatusChanged: Event<{status: ServerStatus, kernel: IKernel}>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved kernel state change from INotebook to IKernelProvider

@@ -611,7 +609,7 @@ export class Kernel implements IKernel {
}
}

export async function executeSilently(session: IJupyterSession, code: string): Promise<nbformat.IOutput[]> {
export async function executeSilently(session: Pick<JupyterKernel.IKernelConnection, 'requestExecute'>, code: string): Promise<nbformat.IOutput[]> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using Jupyter lab API types directly, not IJupyterSession (makes it easier to re-factor later on to use pure Jupyter npm API)

) {
this.updateContextOfActiveInteractiveWindowKernel();
} else if (activeEditor && activeEditor.document.uri.toString() === notebook.identity.toString()) {
this.updateContextOfActiveNotebookKernel(activeEditor);
} else if (kernel.notebookDocument.notebookType === JupyterNotebookView && kernel.notebookDocument === this.vscNotebook.activeNotebookEditor?.document) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some cleanup to use IKernel
Also comparing notebook documents instead of comparing strings.

@@ -172,56 +190,7 @@ export class NotebookWatcher implements INotebookWatcher {
}

// Check to see if this event was on the active notebook
private async isActiveNotebookEvent(kernelStateEvent: KernelStateEventArgs): Promise<boolean> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up to use a dictionary instead of an array, we always have notebook documents, hence 1-1 relationship.

kernelStateEvent.cell.executionSummary?.executionOrder
);
}

// Next, if this is the active document, send out our notifications
if (
(await this.isActiveNotebookEvent(kernelStateEvent)) &&
this.isActiveNotebookEvent(kernelStateEvent) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed a lot of awaits

@@ -164,6 +164,7 @@ export interface IKernelProvider extends IAsyncDisposable {
readonly kernels: Readonly<IKernel[]>;
onDidRestartKernel: Event<IKernel>;
onDidDisposeKernel: Event<IKernel>;
onKernelStatusChanged: Event<{ status: ServerStatus; kernel: IKernel }>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from INotebook to IKernel, easier & looking at NotebookWatcher, we were using Inotebook to detect changes in IKernel,
this change also makes it easier to remove INotbeook (fewer usages)

@@ -611,7 +609,10 @@ export class Kernel implements IKernel {
}
}

export async function executeSilently(session: IJupyterSession, code: string): Promise<nbformat.IOutput[]> {
export async function executeSilently(
session: Pick<JupyterKernel.IKernelConnection, 'requestExecute'>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer using our types, directly using Jupyter Lab API types.
Much easier to refactor later on (as we have fewer layers)

}
public get notebookEditor(): NotebookEditor | undefined {
return this._notebookEditor;
}
public get notebookDocument(): NotebookDocument | undefined {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposed a property notebookDocument, we have a lot of notebookUri.toString() comparisons of strings, instead of just comparing the object refs.

@DonJayamanne DonJayamanne changed the title Fixes to variable viewer Minor refactoring Oct 6, 2021
@DonJayamanne DonJayamanne marked this pull request as ready for review October 6, 2021 19:26
@DonJayamanne DonJayamanne requested a review from a team as a code owner October 6, 2021 19:26
@@ -116,7 +125,7 @@ export class DataViewer extends WebviewPanelHost<IDataViewerMapping> implements
public get notebook(): INotebook | undefined {
if (this.dataProvider && 'notebook' in this.dataProvider) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (this.dataProvider as any).notebook;
return this.dataProvider.notebook;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the type of dataProvider the benefit is strong typeing yes, but this way when we look at removing INotebook we won't miss such instances (i.e. this change was for future proofing changes related to INotebook)

@DonJayamanne DonJayamanne merged commit 7522433 into main Oct 6, 2021
@DonJayamanne DonJayamanne deleted the removeINotebookAgain branch October 6, 2021 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants