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
45
packages/ludic.base/ecs_table_remove.ludic
Normal file
45
packages/ludic.base/ecs_table_remove.ludic
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# ludic.base/ecs_table_remove.ludic - removing a row moves the last one into its place, so the
|
||||
# rows stay dense; every index is told, and the slot's generation moves on
|
||||
export function tb_remove<T>(tb: Table<T>, h: int) -> bool {
|
||||
let r = tb_row(tb, h)
|
||||
if r < 0 { return false }
|
||||
for g in 0 .. len(tb.grids) { grid_unlink(tb.grids[g], r) }
|
||||
for k in 0 .. len(tb.idx) { ix_unfile(tb.idx[k], r) }
|
||||
let last = tb.n - 1
|
||||
if r != last { tb_move(tb, last, r) }
|
||||
tb_pop(tb)
|
||||
let s = h & ECS_SLOT
|
||||
tb.slot_row[s] = -1
|
||||
tb.slot_gen[s] = (tb.slot_gen[s] + 1) & ECS_GEN
|
||||
push(tb.free, s)
|
||||
return true
|
||||
}
|
||||
|
||||
function tb_move<T>(tb: Table<T>, from: int, to: int) -> void {
|
||||
tb.rec[to] = tb.rec[from]
|
||||
tb.ent[to] = tb.ent[from]
|
||||
tb.added[to] = tb.added[from]
|
||||
tb.changed[to] = tb.changed[from]
|
||||
for c in 0 .. len(tb.f) { tb.f[c][to] = tb.f[c][from] }
|
||||
for c in 0 .. len(tb.i) { tb.i[c][to] = tb.i[c][from] }
|
||||
tb.slot_row[tb.ent[to] & ECS_SLOT] = to
|
||||
for g in 0 .. len(tb.grids) { grid_moved(tb.grids[g], from, to) }
|
||||
for k in 0 .. len(tb.idx) { ix_moved(tb.idx[k], from, to) }
|
||||
}
|
||||
|
||||
function tb_pop<T>(tb: Table<T>) -> void {
|
||||
List.pop(tb.rec)
|
||||
List.pop(tb.ent)
|
||||
List.pop(tb.added)
|
||||
List.pop(tb.changed)
|
||||
for c in 0 .. len(tb.f) { List.pop(tb.f[c]) }
|
||||
for c in 0 .. len(tb.i) { List.pop(tb.i[c]) }
|
||||
for g in 0 .. len(tb.grids) { grid_shrink(tb.grids[g]) }
|
||||
for k in 0 .. len(tb.idx) { ix_shrink(tb.idx[k]) }
|
||||
tb.n -= 1
|
||||
}
|
||||
|
||||
# every row gone; the slots keep their generations, so an old handle still misses
|
||||
export function tb_clear<T>(tb: Table<T>) -> void {
|
||||
while tb.n > 0 { tb_remove(tb, tb.ent[tb.n - 1]) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue