feat(ecs): ludic.base Table<T> - dense rows of hot columns, generational handles, a spatial grid, kind indexes and an id map kept current by the setters; ludic.things on it
A mechanic that keeps many of something keeps them as rows of a Table<T> rather than a list it scans. Removal swaps the last row in; a handle (22-bit slot, 9-bit generation) goes stale when its entity is removed. tb_set_f / tb_set_xz / tb_set_i stamp a change tick and refile the row in every index over that column in O(1): tb_grid (a doubly linked spatial hash, rings outward for nearest, rehashing as it grows), tb_index (a cached query: the rows of each value of a kind column, gated by an active column), tb_nearest_of / tb_within_of (a rare kind from its own list), tb_nearest_where (a predicate on the record), tb_within_recs (into the caller's list), tb_changed_since / tb_added_since, IntMap. No question allocates or writes: Ludic frees nothing, and the old lists' per-call copies leaked every frame. ludic.things keeps its Things as a Table<Thing> with x, z, kind and active as columns; every verb writes the record and the row together (a Thing carries its handle and table, so thing_hide(t) still needs no state), thing_set_on / thing_set_xz / thing_place_at are the silent forms the game used to do by assignment, and things_verify holds the columns against the records. things_near(_of) fill a caller's list, thing_of_kind walks a kind, things_count_of counts one, and things_tick visits only the kinds that tick. thing_find answers the first-placed by uid. Against a []Record scanned (M4 Pro): nearest 0.37 / 1.5 / 7.2 us at 10k / 100k / 1M (list 30 / 307 / 3075), by id 0.09 us at 100k (list 17), a move refiled in 11-44 ns. ecs_fuzz_test holds the grid, the kind index and the record queries against a scan through 9000 random changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e3813b1ea4
commit
f802bdd3ec
23 changed files with 1367 additions and 125 deletions
66
packages/ludic.base/ecs_table_set.ludic
Normal file
66
packages/ludic.base/ecs_table_set.ludic
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# ludic.base/ecs_table_set.ludic - the setters: a write stamps the row's tick and refiles it in
|
||||
# every index that reads that column, so an index is never a frame behind what it indexes
|
||||
export function tb_f<T>(tb: Table<T>, c: int, row: int) -> float { return tb.f[c][row] }
|
||||
export function tb_i<T>(tb: Table<T>, c: int, row: int) -> int { return tb.i[c][row] }
|
||||
|
||||
export function tb_set_f<T>(tb: Table<T>, c: int, row: int, v: float) -> void {
|
||||
tb.f[c][row] = v
|
||||
tb.changed[row] = tb.tick
|
||||
for g in 0 .. len(tb.grids) {
|
||||
let gr = tb.grids[g]
|
||||
if gr.cx == c or gr.cz == c { tb_grid_refile(tb, gr, row) }
|
||||
}
|
||||
}
|
||||
|
||||
# two float columns at once, filed once: a move
|
||||
export function tb_set_xz<T>(tb: Table<T>, cx: int, cz: int, row: int, x: float, z: float) -> void {
|
||||
tb.f[cx][row] = x
|
||||
tb.f[cz][row] = z
|
||||
tb.changed[row] = tb.tick
|
||||
for g in 0 .. len(tb.grids) {
|
||||
let gr = tb.grids[g]
|
||||
if gr.cx == cx or gr.cz == cz or gr.cx == cz or gr.cz == cx { tb_grid_refile(tb, gr, row) }
|
||||
}
|
||||
}
|
||||
|
||||
export function tb_set_i<T>(tb: Table<T>, c: int, row: int, v: int) -> void {
|
||||
tb.i[c][row] = v
|
||||
tb.changed[row] = tb.tick
|
||||
for g in 0 .. len(tb.grids) { if tb.grids[g].gate == c { tb_grid_refile(tb, tb.grids[g], row) } }
|
||||
for k in 0 .. len(tb.idx) {
|
||||
let ix = tb.idx[k]
|
||||
if ix.col == c or ix.gate == c { tb_ix_refile(tb, ix, row) }
|
||||
}
|
||||
}
|
||||
|
||||
# a row written without a setter (tb.f / tb.i directly, for speed) is refiled everywhere by this
|
||||
export function tb_refile<T>(tb: Table<T>, row: int) -> void {
|
||||
tb.changed[row] = tb.tick
|
||||
for g in 0 .. len(tb.grids) { tb_grid_refile(tb, tb.grids[g], row) }
|
||||
for k in 0 .. len(tb.idx) { tb_ix_refile(tb, tb.idx[k], row) }
|
||||
}
|
||||
|
||||
export function tb_mark<T>(tb: Table<T>, row: int) -> void { tb.changed[row] = tb.tick }
|
||||
|
||||
function tb_gate_on<T>(tb: Table<T>, gate: int, row: int) -> bool { return gate < 0 or tb.i[gate][row] != 0 }
|
||||
|
||||
function tb_grid_refile<T>(tb: Table<T>, g: Grid, row: int) -> void {
|
||||
grid_refile(g, row, tb.f[g.cx][row], tb.f[g.cz][row], tb_gate_on(tb, g.gate, row))
|
||||
}
|
||||
|
||||
function tb_ix_refile<T>(tb: Table<T>, ix: IntIndex, row: int) -> void {
|
||||
if tb_gate_on(tb, ix.gate, row) { ix_file(ix, row, tb.i[ix.col][row]) } else { ix_unfile(ix, row) }
|
||||
}
|
||||
|
||||
# change detection: the rows a setter wrote after `since`, into `out` (cleared first); no allocation
|
||||
export function tb_changed_since<T>(tb: Table<T>, since: int, out: words) -> int {
|
||||
List.clear(out)
|
||||
for r in 0 .. tb.n { if tb.changed[r] > since { push(out, r) } }
|
||||
return len(out)
|
||||
}
|
||||
|
||||
export function tb_added_since<T>(tb: Table<T>, since: int, out: words) -> int {
|
||||
List.clear(out)
|
||||
for r in 0 .. tb.n { if tb.added[r] > since { push(out, r) } }
|
||||
return len(out)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue