:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(notebooks): reuse unchanged cell parses while editing a SQL cell (#96182)
Sinan Kucukkoseler5eb60a382dae
diff --git a/frontend/src/lib/components/MarkdownNotebook/markdown.ts b/frontend/src/lib/components/MarkdownNotebook/markdown.tsindex 69d858ca5d1a..e9b80b89d977 100644--- a/frontend/src/lib/components/MarkdownNotebook/markdown.ts+++ b/frontend/src/lib/components/MarkdownNotebook/markdown.ts@@ -23,6 +23,7 @@ import {isNotebookPropValue,normalizeInlineMarks,normalizeInlineNodes,+ seedNodeFingerprint,} from './utils'type BlockParseResult = {@@ -1379,7 +1380,29 @@ function makeComponentFallbackParagraph(raw: string): NotebookTextBlockNode {}}+// Parsing a component tag JSON-decodes its props and fingerprinting re-encodes them, so for a+// cell that stores a result envelope both costs track the stored result, not the code. Every+// document parse re-reads every tag, so an unchanged tag (identical raw source) reuses the+// previously parsed node and its fingerprint. Prop objects are shared between the copies; that+// is safe because parsed props are never mutated in place (edits build new props objects).+// Evicted oldest-first under a total size budget, since one tag can carry large cached results.+const COMPONENT_TAG_CACHE_MAX_ENTRIES = 512+const COMPONENT_TAG_CACHE_MAX_TOTAL_CHARS = 16_000_000+const componentTagCache = new Map<string, { node: NotebookComponentBlockNode; fingerprint: string }>()+let componentTagCacheTotalChars = 0+function parseComponentTag(raw: string): { node: NotebookComponentBlockNode | null; error?: NotebookParseError } {+ const cached = componentTagCache.get(raw)+ if (cached) {+ componentTagCache.delete(raw)+ componentTagCache.set(raw, cached)+ // A shallow copy per use: the parse assigns each occurrence its own id, and the cached+ // template must not see that (or any later startsGroup flag).+ const node = { ...cached.node }+ seedNodeFingerprint(node, cached.fingerprint)+ return { node }+ }+const match = raw.match(/^<([A-Z][A-Za-z0-9]*)([\s\S]*?)(?:\/>|>[\s\S]*<\/\1>)$/)if (!match) {return {@@ -1393,16 +1416,28 @@ function parseComponentTag(raw: string): { node: NotebookComponentBlockNode | nu}const propParseResult = parseComponentProps(match[2] ?? '')- return {- node: {- id: '',- type: 'component',- tagName: match[1],- props: propParseResult.props,- raw,- errors: propParseResult.errors.length ? propParseResult.errors : undefined,- },+ const node: NotebookComponentBlockNode = {+ id: '',+ type: 'component',+ tagName: match[1],+ props: propParseResult.props,+ raw,+ errors: propParseResult.errors.length ? propParseResult.errors : undefined,+ }+ componentTagCache.set(raw, { node: { ...node }, fingerprint: getNodeFingerprint(node) })+ componentTagCacheTotalChars += raw.length+ while (+ componentTagCache.size > COMPONENT_TAG_CACHE_MAX_ENTRIES ||+ componentTagCacheTotalChars > COMPONENT_TAG_CACHE_MAX_TOTAL_CHARS+ ) {+ const oldestRaw = componentTagCache.keys().next().value+ if (oldestRaw === undefined) {+ break+ }+ componentTagCache.delete(oldestRaw)+ componentTagCacheTotalChars -= oldestRaw.length}+ return { node }}function parseComponentProps(source: string): PropParseResult {@@ -1640,6 +1675,11 @@ function getOrderedComponentPropEntries(props: NotebookComponentProps): [string,]}+// The whole tag re-serializes when any prop changes, so a big unchanged value (the result+// envelope of a cell whose code is being typed into) would re-encode on every keystroke.+// Keyed by object identity, which is safe because prop values are never mutated in place.+const serializedPropObjectCache = new WeakMap<object, string>()+function serializePropValue(value: NotebookPropValue): string {if (typeof value === 'string') {return JSON.stringify(value)@@ -1647,7 +1687,13 @@ function serializePropValue(value: NotebookPropValue): string {if (typeof value === 'number' || typeof value === 'boolean' || value === null) {return `{${String(value)}}`}- return `{${JSON.stringify(value)}}`+ const cachedValue = serializedPropObjectCache.get(value)+ if (cachedValue !== undefined) {+ return cachedValue+ }+ const serialized = `{${JSON.stringify(value)}}`+ serializedPropObjectCache.set(value, serialized)+ return serialized}function serializeImageNode(node: NotebookComponentBlockNode): string {diff --git a/frontend/src/lib/components/MarkdownNotebook/markdownRoundTrip.test.ts b/frontend/src/lib/components/MarkdownNotebook/markdownRoundTrip.test.tsindex 801bf54eddea..eb8c0b7288f4 100644--- a/frontend/src/lib/components/MarkdownNotebook/markdownRoundTrip.test.ts+++ b/frontend/src/lib/components/MarkdownNotebook/markdownRoundTrip.test.ts@@ -288,6 +288,22 @@ describe('markdown round trip', () => {})})+ describe('repeated parses', () => {+ it('gives duplicate tags distinct ids and never shares nodes between parses', () => {+ // The component tag cache reuses parse results by raw source. If it handed out the+ // cached node itself instead of a copy, the second occurrence (or a later parse of+ // the same markdown) would overwrite the first one's id in place.+ const markdown = '<SQLV2 code="select 1" />\n\n<SQLV2 code="select 1" />'+ const first = parseMarkdownNotebook(markdown)+ const second = parseMarkdownNotebook(markdown)++ expect(first.nodes).toHaveLength(2)+ expect(first.nodes[0].id).not.toEqual(first.nodes[1].id)+ expect(second.nodes.map((node) => node.id)).toEqual(first.nodes.map((node) => node.id))+ expect(second.nodes[0]).not.toBe(first.nodes[0])+ })+ })+describe('html comments', () => {it('parses a standalone comment line into a Comment node', () => {const document = parseMarkdownNotebook('# Title\n\n<!-- reviewer note -->\n\nBody')diff --git a/frontend/src/lib/components/MarkdownNotebook/utils.ts b/frontend/src/lib/components/MarkdownNotebook/utils.tsindex c743c2166b11..a0368a3f815e 100644--- a/frontend/src/lib/components/MarkdownNotebook/utils.ts+++ b/frontend/src/lib/components/MarkdownNotebook/utils.ts@@ -118,6 +118,12 @@ export function getNodeFingerprint(node: NotebookBlockNode): string {return fingerprint}+/** Adopt a known fingerprint for a node copy whose fingerprint inputs are shared with the+ * original (same tagName and props), so the copy skips re-encoding them. */+export function seedNodeFingerprint(node: NotebookBlockNode, fingerprint: string): void {+ nodeFingerprintCache.set(node, fingerprint)+}+function getUncachedNodeFingerprint(node: NotebookBlockNode): string {if (node.type === 'paragraph' || node.type === 'heading' || node.type === 'blockquote') {return JSON.stringify({