Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([issue-705]): fill trail buffer in place, drop redundant anchor copy

[._.]/ Adam Eivye4448f3faff0
diff --git a/client/src/components/city/AgentEntity.jsx b/client/src/components/city/AgentEntity.jsx
index 354c507bb8..7c8b244582 100644
--- a/client/src/components/city/AgentEntity.jsx
+++ b/client/src/components/city/AgentEntity.jsx
@@ -4,6 +4,7 @@ import { Sparkles } from '@react-three/drei';
import * as THREE from 'three';
import { AGENT_STATES } from '../cos/constants';
import {
+ AGENT_MOTION,
computeAgentOrbit,
computeAgentTrailPoints,
computeTrailColors,
@@ -43,10 +44,6 @@ export default function AgentEntity({ agent, position, index = 0, settings }) {
return geom;
}, [color, trailSamples]);
- // The group sits at the building anchor; the agent body orbits within it so
- // its motion trail samples in anchor-relative space.
- const anchor = [position[0], position[1], position[2]];
-
useFrame(({ clock }) => {
const t = clock.getElapsedTime();
const p = computeAgentOrbit(t, { index });
@@ -59,15 +56,17 @@ export default function AgentEntity({ agent, position, index = 0, settings }) {
}
if (trailRef.current && trailSamples > 0) {
- const pts = computeAgentTrailPoints(t, { index }, trailSamples);
const attr = trailRef.current.geometry.getAttribute('position');
- attr.array.set(pts);
+ // Fill the geometry buffer in place — no per-frame allocation.
+ computeAgentTrailPoints(t, { index }, trailSamples, AGENT_MOTION.trailSeconds, attr.array);
attr.needsUpdate = true;
}
});
+ // The group sits at the building anchor; the agent body orbits within it so
+ // its motion trail samples in anchor-relative space.
return (
- <group position={anchor}>
+ <group position={position}>
{trailSamples > 0 && (
<line ref={trailRef} geometry={trailGeom}>
<lineBasicMaterial
diff --git a/client/src/utils/cityAgentMotion.js b/client/src/utils/cityAgentMotion.js
index af620d5e38..22217ebc0e 100644
--- a/client/src/utils/cityAgentMotion.js
+++ b/client/src/utils/cityAgentMotion.js
@@ -40,16 +40,20 @@ export function resolveTrailSamples(particleDensity = 1, maxSamples = AGENT_MOTI
}
// Sample the orbit path backwards from time `t` into `samples` points, newest
-// first. Returns a flat [x0,y0,z0, x1,y1,z1, ...] array of offsets relative to
-// the anchor — the caller adds the anchor position when placing the trail.
+// first. Writes a flat [x0,y0,z0, x1,y1,z1, ...] of offsets relative to the
+// anchor — the caller adds the anchor position when placing the trail. Pass a
+// pre-allocated `out` array (e.g. the geometry's Float32Array) to fill it in
+// place and avoid a per-frame allocation on the render hot path; otherwise a
+// fresh Array is allocated and returned.
export function computeAgentTrailPoints(
t,
opts = {},
samples = AGENT_MOTION.trailSamples,
trailSeconds = AGENT_MOTION.trailSeconds,
+ out = null,
) {
const n = Math.max(2, samples);
- const pts = new Array(n * 3);
+ const pts = out || new Array(n * 3);
for (let i = 0; i < n; i++) {
const dt = (i / (n - 1)) * trailSeconds; // 0 at head (newest), trailSeconds at tail
const p = computeAgentOrbit(t - dt, opts);
diff --git a/client/src/utils/cityAgentMotion.test.js b/client/src/utils/cityAgentMotion.test.js
index af79749d94..6c8103bccd 100644
--- a/client/src/utils/cityAgentMotion.test.js
+++ b/client/src/utils/cityAgentMotion.test.js
@@ -103,6 +103,16 @@ describe('computeAgentTrailPoints', () => {
it('clamps to a minimum of 2 points', () => {
expect(computeAgentTrailPoints(0, {}, 1)).toHaveLength(6);
});
+
+ it('fills a pre-allocated out buffer in place and returns it (no allocation)', () => {
+ const out = new Float32Array(8 * 3);
+ const ret = computeAgentTrailPoints(2.5, { index: 1 }, 8, AGENT_MOTION.trailSeconds, out);
+ expect(ret).toBe(out);
+ const fresh = computeAgentTrailPoints(2.5, { index: 1 }, 8);
+ for (let i = 0; i < fresh.length; i++) {
+ expect(out[i]).toBeCloseTo(fresh[i], 5);
+ }
+ });
});
describe('computeTrailColors', () => {