AGENT SKILL

structural-change-cost-model

Choose between value mutation, enableable component toggle, and structural add/remove based on change frequency, providing the canonical decision model for ECS state transitions.

License 许可
MIT
最近更新
2026-07-25
Stars
★ 8
Forks
0

01 前言 Front Matter

name
structural-change-cost-model
tags
[ "structural-change", "performance" ]
metadata
{ "tier": 3, "internal-only": true }
description
Choose between value mutation, enableable component toggle, and structural add/remove based on change frequency, providing the canonical decision model for ECS state transitions.

03 SKILL.md 正文

Structural Change Cost Model

Intent

Choose between value mutation, enableable component toggle, and structural add/remove based on change frequency, providing the canonical decision model for ECS state transitions.

Use When

Designing any system that needs to change entity state. This is the architectural decision that most directly affects ECS runtime performance.

Avoid When

There is no "avoid when" — this model applies to every state change decision in ECS.

Senior Pattern (Decision Tree)

  1. Value mutation (field on existing component): zero structural change, O(1) write, cheapest. Use when the state is a continuous quantity (health, position, speed) or a discrete enum field.
  2. Enableable component (IEnableableComponent toggle): O(1) bit flip per entity, no chunk migration. Use when binary active/inactive state toggles more than once per second per entity.
  3. Structural change (AddComponent / RemoveComponent): O(entity) chunk migrations. Use when the state change is infrequent (less than once per second per entity) AND semantic clarity from different archetypes is valuable.

Frequency Rule

  • Frequency > 1/sec per entity → value or enableable.
  • Frequency 1/sec – 1/minute per entity → enableable preferred, structural acceptable.
  • Frequency < 1/minute per entity → structural is fine and semantically clearest.
  • When in doubt: measure. The three-mode benchmark below is the definitive reference.

Code Template

// VALUE — continuous state, always cheapest
public struct EnemyState : IComponentData
{
    public float Health;         // mutate directly
    public float Speed;          // mutate directly
    public EnemyBehavior Mode;   // enum field — value mutation
}

// ENABLEABLE — binary toggle, high-frequency
public struct Stunned : IComponentData, IEnableableComponent
{
    public float RemainingDuration;
}
// Toggle: enabledRef.ValueRW = false  (O(1), no chunk move)

// STRUCTURAL — infrequent semantic archetype boundary
public struct Dead : IComponentData { }
// Add: ecb.AddComponent<Dead>(entity)  (once, when entity dies)
// Natural exclusion: all AI/movement queries use .WithNone<Dead>()

Anti-Patterns

  • AddComponent<Dead> / RemoveComponent<Dead> every frame for thousands of enemies — massive per-frame archetype churn.
  • Using structural changes for continuous numeric state (health, ammo) — these should always be plain fields.
  • Using value mutation when different archetypes would allow natural query filtering — sometimes structural change is the right semantic choice; use the frequency rule to decide.

Runtime Risks

  • High-frequency structural changes on large populations: frame spikes, chunk fragmentation, GC pressure from archetype metadata reallocation.
  • At 10,000 entities changing state per frame via structural change: ~80ms overhead (CPU-dependent).
  • Enableable component equivalent: ~0.01ms for 10,000 entities.

Performance Notes

  • Structural change cost = entity must be copied from old chunk to new chunk + chunk metadata updates on both sides.
  • Enableable component cost = 1 bit flip per entity in chunk bitmask.
  • Value mutation = single cache-coherent write per entity — fastest possible.

Architecture Guidance

The archetype boundary created by structural changes is a feature, not just a cost. Dead enemies in a separate archetype are naturally excluded from AI/movement/animation queries with zero query-time overhead. Use structural changes for state that defines "what kind of entity this is," not for state that defines "what value this entity has."

Related Skills

[[enableable-component]], [[tag-component]], [[icomponentdata-value-component]]

04 同仓库更多 Skills

dyCuong03
dyCuong03/unity-agent-team

dots-update-groups

How to choose the right ECS system group and ordering so per-frame and per-fixed-step work is deterministic and never races its own readers. Covers InitializationSystemGroup / SimulationSystemGroup / PresentationSystemGroup / FixedStepSimulationSystemGroup / VariableRateSimulationSystemGroup, UpdateBefore / UpdateAfter peer ordering, OrderFirst / OrderLast group-boundary placement, RateManager, and the cross-group ordering trap. Use when adding an ISystem, fixing "value visible one frame late" bugs, or coupling simulation to a fixed timestep.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-transform-patterns

The Entities 1.x transform model — LocalTransform (writable input) vs LocalToWorld (system-owned matrix), Parent/Child hierarchy, reparenting cost, uniform vs non-uniform scale (PostTransformMatrix), and when to opt out with custom transform systems. Use when designing parent/child hierarchies, runtime reparenting (mount/dismount/socket), or choosing transform components at the Baker boundary.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-singleton-patterns

Model cross-system shared configuration and game state as a single-entity component with a strict single-writer contract. Covers RequireForUpdate gating, SystemAPI.GetSingleton / GetSingletonRW / HasSingleton / TryGetSingleton, bake-time vs runtime-created vs ECB-system singletons, the "exactly one entity" runtime invariant, and the single-writer rule. Use when designing frame-stable global config, shared mutable state, or any component intended to exist on exactly one entity.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-hybrid-bridge

The one-way bridge between DOTS-owned runtime state and Unity-owned presentation. Covers UnityObjectRef for managed asset refs in components, the DOTS-writes / Unity-reads ownership rule, when to use managed IComponentData vs UnityObjectRef vs CompanionComponent, baker-bridge vs runtime-bridge, and SubScene boundary rules. Use when an entity must reference Unity assets (Mesh, Material, AudioClip, ScriptableObject), a MonoBehaviour view must read entity state, or input/UI must trigger ECS work.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-event-driven-ecs

Model one-frame events and external requests in ECS without dual ownership, polling, or persistent state leaks. Covers request-tag pattern with ECB, enableable command components (no structural change), event buffers (IBufferElementData), explicit consumer ownership, one-frame latency rules, and the banned C# event / UnityEvent / static bus anti-patterns. Use when input/UI/network must trigger ECS work, when a one-frame signal fans out to N consumers (damage, hit, collision), or when state changes must be processed exactly once.

★ 8
dyCuong03
dyCuong03/unity-agent-team

valid_skill

A complete, well-formed test fixture skill for SkillHub validation testing. Covers basic task routing and code review for Unity projects.

★ 8