From 49d8da6984b91422ff910da4ea5a037ee7696547 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 19 May 2026 14:37:30 +0100 Subject: [PATCH] Adapt simulation work to realtime budget --- README.md | 2 +- web/epiphany-graph-viewer/README.md | 11 ++- .../src/lib/EpiphanyGraphViewer.tsx | 78 ++++++++++++++++++- web/epiphany-graph-viewer/src/lib/types.ts | 1 + 4 files changed, 85 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3dfe70b..156615c 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ The public viewer API is intentionally coarse: - `layoutMode`: `layered`, `stress`, `force`, or `combined-force` - `motion`: viewer-owned force tuning for combined-force layouts -- `performance`: `quality`, `balanced`, `fast`, or explicit realtime budget knobs +- `performance`: `quality`, `balanced`, `fast`, or explicit `simulationBudgetMs` realtime knobs - `selection`, `focusSelection`, `expandedNode`, and callbacks for app integration - typed state and event payloads diff --git a/web/epiphany-graph-viewer/README.md b/web/epiphany-graph-viewer/README.md index 5c005aa..123fcf7 100644 --- a/web/epiphany-graph-viewer/README.md +++ b/web/epiphany-graph-viewer/README.md @@ -118,9 +118,9 @@ backdrops that want node-aware effects without owning graph physics. ## Performance -The viewer is tuned as a realtime machine. Clients choose a speed budget, and the -viewer adjusts animation frequency, node motion budget, edge refresh rate, and time -step stability. +The viewer is tuned as a realtime machine. Clients choose a simulation budget in +milliseconds, and the viewer measures each step so it can adjust sampled node count +and edge refresh rate automatically. ```tsx { const minFrameMs = 1000 / performanceOptions.targetFps; if (time - lastStepTime < minFrameMs) { @@ -193,6 +203,7 @@ export function EpiphanyGraphViewer({ Math.max(0.001, (time - lastTime) / 1000), ); lastTime = time; + const stepStart = globalThis.performance.now(); const next = stepDynamicLayouts({ baseLayouts: layouts, states: dynamicStateRef, @@ -204,10 +215,16 @@ export function EpiphanyGraphViewer({ viewportHeight: viewportSize.height, dt, time: time / 1000, - maxAnimatedNodes: performanceOptions.maxAnimatedNodes, - refreshEdges: stepIndex % performanceOptions.edgeRefreshRate === 0, + maxAnimatedNodes: adaptiveBudget.nodeBudget, + refreshEdges: stepIndex % adaptiveBudget.edgeRefreshRate === 0, previousLayout: dynamicLayoutRef.current?.[activeGraphKey] ?? null, }); + adjustAdaptiveSimulationBudget( + adaptiveBudget, + globalThis.performance.now() - stepStart, + layouts[activeGraphKey].nodes.length, + performanceOptions, + ); if (next) { dynamicLayoutRef.current = next.layouts; setDynamicLayouts(next.layouts); @@ -1498,6 +1515,7 @@ function resolvePerformanceOptions( return { preset: overrides.preset ?? base.preset, + simulationBudgetMs: clampFiniteNumber(overrides.simulationBudgetMs ?? base.simulationBudgetMs, 0.25, 24), targetFps: clampWholeNumber(overrides.targetFps ?? base.targetFps, 1, 60), maxAnimatedNodes: clampWholeNumber(overrides.maxAnimatedNodes ?? base.maxAnimatedNodes, 1, 5000), edgeRefreshRate: clampWholeNumber(overrides.edgeRefreshRate ?? base.edgeRefreshRate, 1, 12), @@ -1511,6 +1529,7 @@ function performancePresetDefaults( if (preset === "quality") { return { preset, + simulationBudgetMs: 8, targetFps: 60, maxAnimatedNodes: 5000, edgeRefreshRate: 1, @@ -1521,6 +1540,7 @@ function performancePresetDefaults( if (preset === "fast") { return { preset, + simulationBudgetMs: 2.5, targetFps: 24, maxAnimatedNodes: 90, edgeRefreshRate: 3, @@ -1530,6 +1550,7 @@ function performancePresetDefaults( return { preset, + simulationBudgetMs: 4, targetFps: 40, maxAnimatedNodes: 240, edgeRefreshRate: 2, @@ -1540,6 +1561,7 @@ function performancePresetDefaults( function performanceOptionsCacheKey(options: Required) { return [ options.preset, + options.simulationBudgetMs, options.targetFps, options.maxAnimatedNodes, options.edgeRefreshRate, @@ -1555,6 +1577,58 @@ function clampWholeNumber(value: number, min: number, max: number) { return Math.round(clamp(value, min, max)); } +function clampFiniteNumber(value: number, min: number, max: number) { + if (!Number.isFinite(value)) { + return min; + } + + return clamp(value, min, max); +} + +function createAdaptiveSimulationBudget( + nodeCount: number, + options: Required, +): AdaptiveSimulationBudget { + return { + nodeBudget: Math.min(nodeCount, options.maxAnimatedNodes), + edgeRefreshRate: options.edgeRefreshRate, + averageCostMs: options.simulationBudgetMs, + }; +} + +function adjustAdaptiveSimulationBudget( + budget: AdaptiveSimulationBudget, + costMs: number, + nodeCount: number, + options: Required, +) { + const target = options.simulationBudgetMs; + const measuredCost = Math.max(0.01, costMs); + budget.averageCostMs = budget.averageCostMs * 0.82 + measuredCost * 0.18; + + if (budget.averageCostMs > target * 1.08) { + const ratio = target / budget.averageCostMs; + budget.nodeBudget = clampWholeNumber( + budget.nodeBudget * clamp(ratio * 0.92, 0.35, 0.9), + 1, + Math.min(nodeCount, options.maxAnimatedNodes), + ); + budget.edgeRefreshRate = clampWholeNumber(budget.edgeRefreshRate + 1, 1, 12); + return; + } + + if (budget.averageCostMs < target * 0.55) { + budget.nodeBudget = clampWholeNumber( + budget.nodeBudget * 1.16 + 2, + 1, + Math.min(nodeCount, options.maxAnimatedNodes), + ); + if (budget.averageCostMs < target * 0.35) { + budget.edgeRefreshRate = clampWholeNumber(budget.edgeRefreshRate - 1, 1, 12); + } + } +} + function sampleCombinedForce( x: number, y: number, diff --git a/web/epiphany-graph-viewer/src/lib/types.ts b/web/epiphany-graph-viewer/src/lib/types.ts index 60c4679..36da617 100644 --- a/web/epiphany-graph-viewer/src/lib/types.ts +++ b/web/epiphany-graph-viewer/src/lib/types.ts @@ -58,6 +58,7 @@ export type EpiphanyGraphPerformancePreset = "quality" | "balanced" | "fast"; export interface EpiphanyGraphPerformanceOptions { preset?: EpiphanyGraphPerformancePreset; + simulationBudgetMs?: number; targetFps?: number; maxAnimatedNodes?: number; edgeRefreshRate?: number;