Repository navigation
Expand file tree
/
Copy pathdoc.go
More file actions
137 lines (137 loc) · 8.19 KB
/
Copy pathdoc.go
File metadata and controls
137 lines (137 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Package goke provides a high-performance Entity Component System (ECS) engine
// designed for data-oriented programming and mechanical sympathy.
//
// # Core Philosophy
//
// The engine is built on an Archetype-based storage model using Structure of Arrays (SoA).
// By storing components of the same type in contiguous memory columns, the engine
// ensures that entities live "densely" in memory. This layout allows the CPU to
// leverage hardware prefetching and linear cache access, drastically reducing
// cache misses compared to traditional Object-Oriented patterns.
//
// # Key Concepts
//
// 1. Entities & Generation-based Recycling:
// Entities are 64-bit identifiers consisting of an Index (32-bit) and a
// Generation (32-bit). When an entity is removed, its index returns to a pool
// and its generation is incremented. This ensures that stale references to
// deleted entities (ABA problem) are easily detected, while allowing the
// internal storage to reuse memory slots for dense packing and high cache hit rates.
//
// 2. Components as Data Columns:
// Components are user-defined structs registered within the Registry.
// The engine treats them as contiguous blocks of memory. By registering a
// component type, the engine gains metadata (Size and reflect.Type) used to
// build Archetype Columns. This registry-based approach enables zero-allocation
// component access and ensures that data of the same type is perfectly aligned
// for SIMD-like processing speeds.
//
// 3. Systems & Execution Plans:
// Logic is decoupled into Systems (System interface) — [SystemFn] is a
// lightweight System, set as a composite literal instead of a named type.
// Query, Editor, ValueEditor, and Factory can only be constructed inside
// a System's Init (via [SysInit]) or a one-time [ECS.Setup] — never
// directly on ECS — so every read and structural change flows through a
// system. The order and concurrency of execution are defined via a Plan.
//
// 4. Thread Safety & Parallelism:
// The engine allows for synchronous or parallel system execution. While the engine
// provides the tools for high-performance concurrent processing (RunParallel),
// it follows a "Power to the Programmer" philosophy: it is the developer's
// responsibility to ensure that systems running in parallel operate on disjoint
// component sets to avoid data races.
//
// 5. Deferred Commands:
// To maintain state consistency during system updates, modifications to the
// world (like adding components or removing entities) are buffered via
// the CmdBuf and applied during explicit synchronization points (Sync).
// Structural changes can also be applied in bulk: an [Editor] (built via
// [Query.NewEditorBuilder]) migrates whole chunks captured during
// Query.All iteration — [Query.ChunkSnapshot] plus CmdBuf.Migrate
// queue the batch, and Sync executes it with block column copies and
// deferred compaction instead of per-entity moves.
//
// 6. Type-Safe Queries & Cache-Optimized Iteration:
// Data retrieval is handled through [Query] obtained via [SysInit.NewQueryBuilder].
// Component columns are declared with [Comp][T] and accessed via
// [Comp.Slice] (bulk) or [Comp.At] (per-entity). Bulk iteration via
// Query.All yields SoA chunks (Go slices over native memory), while
// subset queries via Query.Pick or single-entity access via Query.Seek
// yield per-entity component pointers resolved via the entity-to-storage
// index. All access is zero-allocation and reflection-free.
//
// 7. Paced Queries:
// A query built [QueryBuilder.With] a [PacedBy] goes over the blocks carrying
// a ruled component as its rules say: [Every] n-th run over a step n times as
// long, [Never]. Query.Paced walks the blocks whose turn it is, [Query.Dt]
// their step; Query.All still walks every one, [Query.Due] telling which
// have their turn. The period of a block is worked out once, as its
// archetype is matched.
//
// # Hardware Constraints & Limits
//
// To maintain extreme performance, the engine operates with certain fixed limits:
//
// - Component Types: The engine supports up to 512 unique component types per registry.
// This is determined by the Mask (8x64-bit fields), ensuring that
// archetype matching remains a fast, constant-time bitwise operation; a
// component ID is 16 bits.
//
// - Memory Pre-allocation: Archetypes and internal structures are initialized
// with predefined capacities (configurable via ECSOption). This reduces
// early memory fragmentation and minimizes GC pressure during the initial
// entity burst.
//
// - Entity Indexing: Entities are 64-bit identifiers, allowing for a virtually
// unlimited number of entities, constrained only by the available system RAM.
//
// - Number of Queries: A world builds as many queries as it needs. Their matchers
// live in blocks (64 by default, [WithMatcherCap]); a full block is followed by a
// new one and no matcher ever moves, so a [Query] keeps its matcher for good. A
// matcher is a few hundred bytes; every new archetype is announced to each of them.
//
// - Query Complexity: A single [Query] can track any number of component columns
// declared via [Comp][T]. Additional types can be used as filter-only
// constraints via Include/Exclude opts without occupying tracked columns,
// or as optionally-tracked columns declared via [OptComp][T] and the
// Optional builder method — readable when present, never required to match.
//
// # Internal Package Dependencies
//
// The internal packages form a strict acyclic dependency graph. Each layer
// may only import packages from layers below it:
//
// Layer 0 iter — column-access primitives: Cursor, ArrayRef[T], OptArrayRef[T]
// Layer 1 comp — shared primitives: ID, Def, Mask, AccessSpec, DefIndex (→ iter)
// Layer 2 chunk — cache-aligned chunked memory layout (→ comp)
// Layer 3 colstore — column-oriented storage (→ comp, chunk, iter)
// Layer 4 arch — archetype ID, Mask, graph (→ comp, colstore)
// Layer 5 addr — entity address book: Entry, Index, Book (→ arch, colstore)
// bulk — bulk-operation contract: ChunkSnapshot, Migrator (→ arch, colstore)
// Layer 6 ent — entity lifecycle & bulk migration: Manager, Factory,
// Editor, Remover, ValueEditor (→ addr, arch, bulk, colstore, comp, iter)
// persist — world snapshot encoding: Save, Load (→ addr, arch, colstore, comp, iter)
// Layer 7 query — query layer, matcher baking (→ addr, arch, bulk, colstore, comp, iter)
// Layer 8 orch — scheduler, plans, command buffers (→ bulk, comp)
// reg — top-level Registry (→ bulk, ent, arch, comp, query, persist)
//
// Expressed as a directed graph (arrow = "is imported by"; addr and bulk
// both feed ent and query, converging at the ├ marks below rather than
// being drawn twice):
//
// iter ──► comp ──► chunk ──► colstore ──► arch ──┬─► addr ─┐
// │ ├─► persist ───┐
// │ └──┐ │
// │ ├─► ent ────┤
// │ ├─► query ──┼─► reg
// │ ┌──┘ │
// └─► bulk ─┼──────────────┘
// └─► orch
//
// [github.com/kjkrol/uid] is an external module used across the stack for
// 64-bit generational entity identifiers.
//
// orch and reg are fully independent of each other. The top-level goke
// package is the only place that wires them together, passing a pointer to
// the embedded reg.Registry to orch.NewScheduler as an orch.Mutator.
package goke