Notebooks: Use much faster json parser when loading notebook initially (#6552)

* Use much faster json parser

* fallback to old json parser
This commit is contained in:
Chris LaFreniere
2019-08-02 17:23:49 -07:00
committed by GitHub
parent 7f5a72cdab
commit 402b50c03b

View File

@@ -25,8 +25,12 @@ export class LocalContentManager implements nb.ContentManager {
constructor(@IFileService private readonly fileService: IFileService) { }
public async loadFromContentString(contentString: string): Promise<nb.INotebookContents> {
let contents: JSONObject = json.parse(contentString);
let contents: JSONObject;
if (contentString === '' || contentString === undefined) {
return v4.createEmptyNotebook();
} else {
contents = this.parseFromJson(contentString);
}
if (contents) {
if (contents.nbformat === 4) {
return v4.readNotebook(<any>contents);
@@ -36,9 +40,6 @@ export class LocalContentManager implements nb.ContentManager {
if (contents.nbformat) {
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any));
}
} else if (contentString === '' || contentString === undefined) {
// Empty?
return v4.createEmptyNotebook();
}
// else, fallthrough condition
@@ -53,7 +54,13 @@ export class LocalContentManager implements nb.ContentManager {
// Note: intentionally letting caller handle exceptions
let notebookFileBuffer = await this.fileService.readFile(notebookUri);
let stringContents = notebookFileBuffer.value.toString();
let contents: JSONObject = json.parse(stringContents);
let contents: JSONObject;
if (stringContents === '' || stringContents === undefined) {
// Empty?
return v4.createEmptyNotebook();
} else {
contents = this.parseFromJson(stringContents);
}
if (contents) {
if (contents.nbformat === 4) {
@@ -64,9 +71,6 @@ export class LocalContentManager implements nb.ContentManager {
if (contents.nbformat) {
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any));
}
} else if (stringContents === '' || stringContents === undefined) {
// Empty?
return v4.createEmptyNotebook();
}
// else, fallthrough condition
@@ -81,6 +85,15 @@ export class LocalContentManager implements nb.ContentManager {
return notebook;
}
private parseFromJson(contentString: string): JSONObject {
let contents: JSONObject;
try {
contents = JSON.parse(contentString);
} catch {
contents = json.parse(contentString);
}
return contents;
}
}
namespace v4 {