mirror of
https://github.com/ChiChou/entdb.git
synced 2026-12-06 11:43:25 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
c8cddb3013
commit
4b9f8db762
1 file changed
+99
-35
@@ -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<Element, Target>();
|
||||
let pending: Set<Target> | 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<HTMLSpanElement>(null);
|
||||
const chips = useRef<HTMLSpanElement>(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) => (
|
||||
<Atom
|
||||
// biome-ignore lint/suspicious/noArrayIndexKey: items may repeat
|
||||
@@ -150,13 +213,14 @@ export function ValuePreview({
|
||||
|
||||
return (
|
||||
<span
|
||||
ref={outer}
|
||||
className={cn(
|
||||
"flex min-w-0 items-center gap-1 font-mono text-xs text-muted-foreground",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{/* 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. */}
|
||||
<span
|
||||
ref={chips}
|
||||
className="flex h-5 min-w-0 flex-wrap items-center gap-1 overflow-hidden"
|
||||
@@ -169,11 +233,11 @@ export function ValuePreview({
|
||||
<PopoverTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex h-5 shrink-0 items-center gap-0.5 rounded px-1 tabular-nums text-muted-foreground ring-1 ring-inset ring-border transition-colors hover:bg-accent hover:text-foreground"
|
||||
className="inline-flex h-5 shrink-0 items-center rounded px-1.5 tabular-nums text-muted-foreground ring-1 ring-inset ring-border transition-colors hover:bg-accent hover:text-foreground"
|
||||
aria-label={`Show all ${count} ${noun}`}
|
||||
title={`${hidden} more of ${count} ${noun}`}
|
||||
>
|
||||
<MoreHorizontal className="h-3.5 w-3.5" />+{hidden}
|
||||
+{hidden}
|
||||
</button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
|
||||
Reference in new issue
Block a user