A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: json parse to be more performant for non-edge cases (#371)
Myles Scolnick5ced33e2fdfc
diff --git a/frontend/src/core/codemirror/completion/Autocompleter.ts b/frontend/src/core/codemirror/completion/Autocompleter.tsindex c4828fb333..34011b4f88 100644--- a/frontend/src/core/codemirror/completion/Autocompleter.ts+++ b/frontend/src/core/codemirror/completion/Autocompleter.ts@@ -25,13 +25,19 @@ function constructCompletionInfoNode(innerHtml?: string): HTMLElement | null {export const AUTOCOMPLETER = new DeferredRequestRegistry<Omit<CodeCompletionRequest, "id">,- CompletionResultMessage->("function-call-result", async (requestId, req) => {- await sendCodeCompletionRequest({- id: requestId,- ...req,- });-});+ CompletionResultMessage | null+>(+ "autocomplete-result",+ async (requestId, req) => {+ await sendCodeCompletionRequest({+ id: requestId,+ ...req,+ });+ },+ // We don't care about previous requests+ // so we just resolve them with an empty response.+ { resolveExistingRequests: () => null }+);export const Autocompleter = {/**@@ -70,20 +76,7 @@ export const Autocompleter = {excludeTypes?: string[];exactName?: string;}): Tooltip | undefined {- const options = [...message.options];-- let firstOption: CompletionOption | undefined;- // If there are no options, don't show a tooltip- if (options.length === 0) {- return undefined;- } else if (options.length === 1) {- // One option- firstOption = options[0];- } else if (exactName) {- // Tie break to a matching name- firstOption = options.find((option) => option.name === exactName);- }-+ const firstOption = getFirstOption(message.options, exactName);if (!firstOption) {return undefined;}@@ -110,3 +103,19 @@ export const Autocompleter = {};},};++function getFirstOption(+ options: CompletionOption[],+ tieBreak?: string+): CompletionOption | undefined {+ if (options.length === 0) {+ return undefined;+ } else if (options.length === 1) {+ // One option+ return options[0];+ } else if (tieBreak) {+ // Tie break to a matching name+ return options.find((option) => option.name === tieBreak);+ }+ return undefined;+}diff --git a/frontend/src/core/codemirror/completion/completer.ts b/frontend/src/core/codemirror/completion/completer.tsindex 33447aed7c..32c10bb601 100644--- a/frontend/src/core/codemirror/completion/completer.ts+++ b/frontend/src/core/codemirror/completion/completer.ts@@ -29,6 +29,9 @@ export async function completer(document: query,cellId: cellId,});+ if (!result) {+ return null;+ }// If it is a tooltip, show it as a Tooltip instead of a completionconst tooltip = Autocompleter.asHoverTooltip({diff --git a/frontend/src/core/codemirror/completion/hints.ts b/frontend/src/core/codemirror/completion/hints.tsindex ddf5b94fe0..7a3d229af1 100644--- a/frontend/src/core/codemirror/completion/hints.ts+++ b/frontend/src/core/codemirror/completion/hints.ts@@ -33,6 +33,9 @@ export function hintTooltip() {document: view.state.doc.slice(0, endToken).toString(), // convert Text to stringcellId: cellId,});+ if (!result) {+ return null;+ }const fullWord = view.state.doc.slice(startToken, endToken).toString();const tooltip = Autocompleter.asHoverTooltip({diff --git a/frontend/src/core/dom/htmlUtils.ts b/frontend/src/core/dom/htmlUtils.tsindex d7d78bf9f2..3f75898fc0 100644--- a/frontend/src/core/dom/htmlUtils.ts+++ b/frontend/src/core/dom/htmlUtils.ts@@ -1,7 +1,7 @@/* Copyright 2023 Marimo. All rights reserved. */import { assertExists } from "@/utils/assertExists";import { UI_ELEMENT_REGISTRY } from "./uiregistry";-import { jsonParseWithSpecialChar } from "@/utils/json-parser";+import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";import { Objects } from "@/utils/objects";/**diff --git a/frontend/src/core/network/DeferredRequestRegistry.ts b/frontend/src/core/network/DeferredRequestRegistry.tsindex 310fa0f534..f5a78b45bc 100644--- a/frontend/src/core/network/DeferredRequestRegistry.ts+++ b/frontend/src/core/network/DeferredRequestRegistry.ts@@ -21,10 +21,22 @@ export class DeferredRequestRegistry<REQ, RES> {constructor(public operation: string,- private makeRequest: (id: RequestId, req: REQ) => Promise<void>+ private makeRequest: (id: RequestId, req: REQ) => Promise<void>,+ private opts: {+ /**+ * Resolve existing requests with an empty response.+ */+ resolveExistingRequests?: () => RES;+ } = {}) {}async request(opts: REQ): Promise<RES> {+ if (this.opts.resolveExistingRequests) {+ const result = this.opts.resolveExistingRequests();+ this.requests.forEach((deferred) => deferred.resolve(result));+ this.requests.clear();+ }+const requestId = RequestId.create();const deferred = new Deferred<RES>();diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsxindex bac298180a..94c3c0131d 100644--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx@@ -21,10 +21,10 @@ import { useVariablesActions } from "../variables/state";import { toast } from "@/components/ui/use-toast";import { renderHTML } from "@/plugins/core/RenderHTML";import { FUNCTIONS_REGISTRY } from "../functions/FunctionRegistry";-import { jsonParseWithSpecialChar } from "@/utils/json-parser";import { prettyError } from "@/utils/errors";import { isStaticNotebook } from "../static/static-state";import { useRef } from "react";+import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";/*** WebSocket that connects to the Marimo kernel and handles incoming messages.diff --git a/frontend/src/utils/__tests__/json-parser.test.ts b/frontend/src/utils/__tests__/json-parser.test.tsindex 66fc0a5b7e..71d4bb45b0 100644--- a/frontend/src/utils/__tests__/json-parser.test.ts+++ b/frontend/src/utils/__tests__/json-parser.test.ts@@ -1,6 +1,6 @@/* Copyright 2023 Marimo. All rights reserved. */import { expect, it } from "vitest";-import { jsonParseWithSpecialChar } from "../json-parser";+import { jsonParseWithSpecialChar } from "../json/json-parser";it("can jsonParseWithSpecialChar happy path", () => {expect(jsonParseWithSpecialChar('"hello"')).toEqual("hello");diff --git a/frontend/src/utils/json-parser.ts b/frontend/src/utils/json/json-parser.tssimilarity index 86%rename from frontend/src/utils/json-parser.tsrename to frontend/src/utils/json/json-parser.tsindex 595aa779e3..def264c7b9 100644--- a/frontend/src/utils/json-parser.ts+++ b/frontend/src/utils/json/json-parser.ts@@ -5,6 +5,14 @@* This also handles NaN, Infinity, and -Infinity.*/export function jsonParseWithSpecialChar<T>(value: string): T {+ // This regex handling is expensive and often not needed.+ // We try to parse with JSON.parse first, and if that fails, we use the regex.+ try {+ return JSON.parse(value) as T;+ } catch {+ // Do nothing+ }+// Random unicode character that is unlikely to be used in the JSON stringconst CHAR = "◐";