Adapt simulation work to realtime budget
This commit is contained in:
parent
7045ec11b5
commit
49d8da6984
4 changed files with 85 additions and 7 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
<EpiphanyGraphViewer
|
||||
|
|
@ -136,7 +136,9 @@ Presets:
|
|||
- `balanced`: 40 FPS target, capped node motion, edges refreshed every other step
|
||||
- `fast`: 24 FPS target, smaller animated-node budget, edges refreshed every third step
|
||||
|
||||
Use explicit knobs when the host app knows its budget:
|
||||
Use explicit knobs when the host app knows its budget. `simulationBudgetMs` is the
|
||||
authority; the other values are starting ceilings and cadence hints the adaptive loop
|
||||
can tighten when frames get expensive.
|
||||
|
||||
```tsx
|
||||
<EpiphanyGraphViewer
|
||||
|
|
@ -144,6 +146,7 @@ Use explicit knobs when the host app knows its budget:
|
|||
layoutMode="combined-force"
|
||||
performance={{
|
||||
preset: "fast",
|
||||
simulationBudgetMs: 2.5,
|
||||
targetFps: 30,
|
||||
maxAnimatedNodes: 120,
|
||||
edgeRefreshRate: 3,
|
||||
|
|
|
|||
|
|
@ -47,6 +47,12 @@ type DynamicNodeState = {
|
|||
vy: number;
|
||||
};
|
||||
|
||||
type AdaptiveSimulationBudget = {
|
||||
nodeBudget: number;
|
||||
edgeRefreshRate: number;
|
||||
averageCostMs: number;
|
||||
};
|
||||
|
||||
const PANEL_SURFACE = "rgba(7, 16, 30, 0.76)";
|
||||
const PANEL_BORDER = "1px solid rgba(148, 163, 184, 0.18)";
|
||||
|
||||
|
|
@ -180,6 +186,10 @@ export function EpiphanyGraphViewer({
|
|||
let lastTime = globalThis.performance.now();
|
||||
let lastStepTime = lastTime;
|
||||
let stepIndex = 0;
|
||||
const adaptiveBudget = createAdaptiveSimulationBudget(
|
||||
layouts[activeGraphKey].nodes.length,
|
||||
performanceOptions,
|
||||
);
|
||||
const tick = (time: number) => {
|
||||
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<EpiphanyGraphPerformanceOptions>) {
|
||||
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<EpiphanyGraphPerformanceOptions>,
|
||||
): AdaptiveSimulationBudget {
|
||||
return {
|
||||
nodeBudget: Math.min(nodeCount, options.maxAnimatedNodes),
|
||||
edgeRefreshRate: options.edgeRefreshRate,
|
||||
averageCostMs: options.simulationBudgetMs,
|
||||
};
|
||||
}
|
||||
|
||||
function adjustAdaptiveSimulationBudget(
|
||||
budget: AdaptiveSimulationBudget,
|
||||
costMs: number,
|
||||
nodeCount: number,
|
||||
options: Required<EpiphanyGraphPerformanceOptions>,
|
||||
) {
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ export type EpiphanyGraphPerformancePreset = "quality" | "balanced" | "fast";
|
|||
|
||||
export interface EpiphanyGraphPerformanceOptions {
|
||||
preset?: EpiphanyGraphPerformancePreset;
|
||||
simulationBudgetMs?: number;
|
||||
targetFps?: number;
|
||||
maxAnimatedNodes?: number;
|
||||
edgeRefreshRate?: number;
|
||||
|
|
|
|||
Loading…
Reference in a new issue