From 4b9f8db762fffabdfa41aedc3168af857c002f72 Mon Sep 17 00:00:00 2001 From: cc Date: Thu, 3 Dec 2026 17:12:38 +0100 Subject: [PATCH] perf(find): batch +N overflow counting; sort a row's values Each row measured its own chips (reset width, read, set width), which forced one layout per row: resizing an expanded tree of 935 rows took ~4.5 s a frame (6,257 layouts), and toggling the value filter took seconds. One shared ResizeObserver now batches every row in one frame: reset all, read all, write all. Resize: ~140 ms a frame, 49 layouts. The button is just +N (no ellipsis). Values in a row sort with filter hits first, then alphabetically, so rows holding the same items read the same. Co-Authored-By: Claude Opus 5.5 --- src/components/value-preview.tsx | 134 +++++++++++++++++++++++-------- 1 file changed, 99 insertions(+), 35 deletions(-) diff --git a/src/components/value-preview.tsx b/src/components/value-preview.tsx index 74815e5..7aa0e19 100644 --- a/src/components/value-preview.tsx +++ b/src/components/value-preview.tsx @@ -1,6 +1,5 @@ "use client"; -import { MoreHorizontal } from "lucide-react"; import { useLayoutEffect, useRef, useState } from "react"; import { @@ -13,6 +12,76 @@ import { cn } from "@/lib/utils"; const TITLE_LIMIT = 4000; +const collator = new Intl.Collator(undefined, { sensitivity: "base" }); + +/** + * Overflow counting (+N), batched across every row. A row's chips wrap onto + * a hidden second line; after layout we count those and end the chip box at + * the last visible chip so +N follows it. Done per row, each measure (reset + * width, read, set width) forced its own layout: ~900 layouts per resize or + * filter change. Here all rows reset, then all read, then all write, in one + * frame: one forced layout for the whole tree. + */ +interface Target { + box: HTMLElement; + setHidden: (hidden: number) => void; +} + +/** Room kept after the last visible chip for the +N button. */ +const MORE_RESERVE = 48; + +const targets = new Map(); +let pending: Set | null = null; +let observer: ResizeObserver | null = null; + +function measureAll() { + const batch = pending ? [...pending] : []; + pending = null; + for (const { box } of batch) box.style.width = ""; + const results = batch.map(({ box }) => { + const items = [...box.children]; + if (items.length < 2) return null; + const rects = items.map((el) => el.getBoundingClientRect()); + const top = rects[0].top; + if (rects.every((r) => r.top === top)) return null; + // Some chips wrapped: keep those that fit with room for +N. + const limit = box.getBoundingClientRect().right - MORE_RESERVE; + const visible = Math.max( + 1, + rects.findIndex((r) => r.top !== top || r.right > limit), + ); + const left = box.getBoundingClientRect().left; + return { + hidden: items.length - visible, + width: Math.ceil(rects[visible - 1].right - left), + }; + }); + batch.forEach(({ box, setHidden }, i) => { + const result = results[i]; + box.style.width = result ? `${result.width}px` : ""; + setHidden(result?.hidden ?? 0); + }); +} + +function schedule(target: Target) { + if (!pending) { + pending = new Set(); + requestAnimationFrame(measureAll); + } + pending.add(target); +} + +/** One observer for every row; its callback batches all resized rows. */ +function sharedObserver() { + observer ??= new ResizeObserver((entries) => { + for (const entry of entries) { + const target = targets.get(entry.target); + if (target) schedule(target); + } + }); + return observer; +} + function titleFor(value: unknown) { const json = JSON.stringify(value, null, 2); return json.length > TITLE_LIMIT ? `${json.slice(0, TITLE_LIMIT)}\n…` : json; @@ -80,39 +149,30 @@ export function ValuePreview({ /** Every item, for the overflow menu. */ let all: React.ReactNode = null; let count: number | null = null; + const outer = useRef(null); const chips = useRef(null); // Chips wrapped onto the hidden second line, counted after layout. const [hidden, setHidden] = useState(0); - // Count the chips that wrapped out of sight, and end the box at the last - // visible one so the … menu follows it instead of the box's full width - // (a wrapped flex box keeps the width of all its chips on one line). - // Re-measured when the chips change (value, or filter hits moving first), - // when the menu appears (it takes room), and when the row resizes. - const more = hidden > 0; + useLayoutEffect(() => { + const el = outer.current; + const box = chips.current; + if (!el || !box) return; + const target: Target = { box, setHidden }; + targets.set(el, target); + sharedObserver().observe(el); + return () => { + targets.delete(el); + observer?.unobserve(el); + pending?.delete(target); + }; + }, []); + // The chips changed (value, or filter hits moving first): re-measure. // biome-ignore lint/correctness/useExhaustiveDependencies: triggers only useLayoutEffect(() => { - const box = chips.current; - const row = box?.parentElement?.parentElement; - if (!box || !row) return; - const measure = () => { - box.style.width = ""; - const items = [...box.children] as HTMLElement[]; - const top = items[0]?.offsetTop ?? 0; - const visible = items.filter((el) => el.offsetTop === top); - const last = visible.at(-1); - if (last && visible.length < items.length) { - const width = - last.getBoundingClientRect().right - box.getBoundingClientRect().left; - box.style.width = `${Math.ceil(width)}px`; - } - setHidden(items.length - visible.length); - }; - measure(); - const observer = new ResizeObserver(measure); - observer.observe(row); - return () => observer.disconnect(); - }, [value, highlight, more]); + const target = outer.current && targets.get(outer.current); + if (target) schedule(target); + }, [value, highlight]); if (isList || isDict) { // Nested arrays/dicts show as JSON and aren't atoms, so not pickable. @@ -124,10 +184,13 @@ export function ValuePreview({ ) : Object.keys(value as object).map((key) => ({ text: key, atom: true })); count = items.length; - const ordered = [ - ...items.filter((item) => hits.has(item.text)), - ...items.filter((item) => !hits.has(item.text)), - ]; + // Filter hits first, then alphabetical, so rows with the same items + // read the same. + const ordered = items.toSorted( + (a, b) => + Number(hits.has(b.text)) - Number(hits.has(a.text)) || + collator.compare(a.text, b.text), + ); body = ordered.map((item, i) => ( {/* Chips that don't fit wrap onto a hidden second line, so none is - cut mid-word; the … menu right after the last one lists them all. */} + cut mid-word; the +N menu right after the last one lists them all. */}