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

serialize one cell at a time #214158

Closed
wants to merge 1 commit into from
Closed
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
45 changes: 41 additions & 4 deletions extensions/ipynb/src/notebookSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
return data;
}

public serializeNotebook(data: vscode.NotebookData, _token: vscode.CancellationToken): Uint8Array {
return new TextEncoder().encode(this.serializeNotebookToString(data));
public async serializeNotebook(data: vscode.NotebookData, _token: vscode.CancellationToken): Promise<Uint8Array> {
return new TextEncoder().encode(await this.serializeNotebookToString(data));
}

public serializeNotebookToString(data: vscode.NotebookData): string {
public async serializeNotebookToString(data: vscode.NotebookData): Promise<string> {
const notebookContent = getNotebookMetadata(data);
// use the preferred language from document metadata or the first cell language as the notebook preferred cell language
const preferredCellLanguage = notebookContent.metadata?.language_info?.name ?? data.cells.find(cell => cell.kind === vscode.NotebookCellKind.Code)?.languageId;
Expand All @@ -94,8 +94,45 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
const indentAmount = data.metadata && 'indentAmount' in data.metadata && typeof data.metadata.indentAmount === 'string' ?
data.metadata.indentAmount :
' ';

const startTime = performance.now();

// Your existing code here

// ipynb always ends with a trailing new line (we add this so that SCMs do not show unnecessary changes, resulting from a missing trailing new line).
return JSON.stringify(sortObjectPropertiesRecursively(notebookContent), undefined, indentAmount) + '\n';
const orderedObject = sortObjectPropertiesRecursively(notebookContent);
console.log(`Sort object - Elapsed time: ${performance.now() - startTime} milliseconds`);

const stringified = JSON.stringify(orderedObject, undefined, indentAmount) + '\n';
console.log(`JSON.stringify'd - Elapsed time: ${performance.now() - startTime} milliseconds`);

let result = '{\n';
let outerFirst = true;
for (const key of Object.keys(orderedObject)) {
if (!outerFirst) {
result += ',\n';
}
outerFirst = false;
if (key === 'cells') {
result += `${indentAmount}"${key}": [\n`;
let first = true;
for (const cell of orderedObject[key]) {
if (!first) {
result += ',\n';
}
first = false;
result += `${indentAmount}${indentAmount}${JSON.stringify(cell, undefined, indentAmount)}`;
await new Promise(resolve => setTimeout(resolve, 0));
}
result += `${indentAmount}]`;
} else {
result += `${indentAmount}"${key}": ${JSON.stringify(orderedObject[key], undefined, indentAmount)}`;
}
}
result += '}';
console.log(`iter stringified - Elapsed time: ${performance.now() - startTime} milliseconds`);

return result;
}
}

Expand Down
Loading