AgentSkillsCN

dev-patterns-object-pooling

为高性能 R3F 组件(贴图、粒子、弹道)引入对象池技术。

SKILL.md
--- frontmatter
name: dev-patterns-object-pooling
description: Object pooling for high-performance R3F components (decals, particles, projectiles)
category: patterns

Object Pooling Pattern

"Pre-allocate, reuse, recycle – eliminate runtime GC pauses."

When to Use This Skill

Use when:

  • Creating/destroying objects every frame (bullets, particles, decals)
  • Targeting 60 FPS with many transient objects
  • Seeing GC pauses in Chrome DevTools Performance tab
  • Objects have identical initialization (can be pre-created)
  • Maximum simultaneous objects is bounded (~500 or less)

Quick Start

tsx
// Basic object pool pattern
const POOL_SIZE = 500;
const MAX_ACTIVE = 200;

interface PoolSlot<T> {
  obj: T;
  active: boolean;
  lastUsed: number;
}

function useObjectPool<T>(
  create: () => T,
  activate: (obj: T) => void,
  deactivate: (obj: T) => void
) {
  const poolRef = useRef<PoolSlot<T>[]>([]);

  // Initialize pool on mount
  useEffect(() => {
    poolRef.current = Array.from({ length: POOL_SIZE }, () => ({
      obj: create(),
      active: false,
      lastUsed: 0,
    }));
    return () => {
      // Cleanup
      poolRef.current.forEach(slot => {
        if (slot.obj?.dispose) slot.obj.dispose();
      });
    };
  }, []);

  const acquire = useCallback(() => {
    const pool = poolRef.current;
    // Find inactive slot
    let slot = pool.find(s => !s.active);
    // If pool full, recycle LRU
    if (!slot) {
      slot = pool.reduce((oldest, s) =>
        s.lastUsed < oldest.lastUsed ? s : oldest
      );
      deactivate(slot.obj);
    }
    slot.active = true;
    slot.lastUsed = performance.now();
    activate(slot.obj);
    return slot.obj;
  }, [activate]);

  const release = useCallback((obj: T) => {
    const slot = poolRef.current.find(s => s.obj === obj);
    if (slot) slot.active = false;
  }, []);

  return { acquire, release };
}

Decision Framework

ScenarioUse Pool?Reason
Bullets (max ~100 active)YesHigh create/destroy rate
Decals (max ~200 visible)YesGeometry allocation costly
Particles (max ~500)YesPer-frame creation
UI overlays (dynamic count)NoUnpredictable count
Player characters (1-32)NoLow churn, complex init
Static propsNoNever destroyed

LRU Eviction Pattern

When the pool is full, evict the Least Recently Used item:

tsx
// LRU recycling
let slot = pool.find(s => !s.active);
if (!slot) {
  // Pool exhausted - recycle oldest decal
  slot = pool.reduce((oldest, s) =>
    s.lastUsed < oldest.lastUsed ? s : oldest
  );
  // Fade out before recycling
  fadeOutDecal(slot.obj);
}

Why LRU?

  • Predictable: older content fades first (less noticeable)
  • Fair: no single hot-spot gets preferential treatment
  • Simple: O(n) scan is fine for pools < 1000

GC-Avoidance: Temp Vector Reuse

tsx
// BAD: Creates new objects every frame
useFrame(() => {
  const position = new Vector3();
  const quaternion = new Quaternion();
  // ... do work
});

// GOOD: Reuse temp objects
const _tempVec = useRef(new Vector3()).current;
const _tempQuat = useRef(new Quaternion()).current;

useFrame(() => {
  _tempVec.set(0, 0, 0);  // Reset, don't reallocate
  _tempQuat.identity();
  // ... do work
});

Pool Size Guidelines

Object TypeSuggested Pool SizeMax ActiveRationale
Bullets200100Fast fire rate ~10/sec
Particles1000500Explosions spawn many at once
Decals500200Persist 60s, but limited visibility
Audio sources3216WebAudio limit

Rule of thumb: poolSize = maxActive * 2 to maxActive * 3

Implementation Checklist

  • Pre-create all objects on mount (useEffect)
  • Use active flag to track in-use slots
  • Use lastUsed timestamp for LRU eviction
  • Properly dispose geometries/materials in cleanup
  • Reuse temp vectors with useRef or class fields
  • Initialize materials per-slot (not shared) when needed
  • Consider frustumCulled={false} for small objects

Common Pitfalls

PitfallSymptomFix
Sharing material across slotsAll decals same colorCreate unique material per slot
Forgetting to reset stateStale data on reuseReset all props in activate()
Pool too smallVisible poppingIncrease pool or maxActive
No disposal in useEffectMemory leakAdd cleanup function
Using new in useFrameGC stutterUse temp refs

Reference Implementation

See: src/components/game/effects/PaintDecalManager.tsx

Key sections:

  • Pool initialization: lines 68-118
  • Acquire/activate: lines 120-141
  • LRU recycling: lines 142-153
  • Release/deactivate: lines 155-162
  • Temp vector reuse: lines 35-37