ludic/packages/ludic.base/ecs_grid_file.ludic
Orkuncakilkaya f802bdd3ec 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>
2026-09-27 15:52:51 +03:00

40 lines
1.3 KiB
Text

# ludic.base/ecs_grid_file.ludic - where a row is filed follows its columns: a row that has not
# left its cell is not touched, a moved one is relinked, and the buckets double as the rows grow
function grid_refile(g: Grid, r: int, x: float, z: float, on: bool) -> void {
if not on {
grid_unlink(g, r)
return
}
let ix = grid_cell(g, x)
let iz = grid_cell(g, z)
let k = grid_key(ix, iz)
if g.key[r] == k { return }
grid_unlink(g, r)
grid_link(g, r, k, grid_bucket(g, ix, iz))
if g.count > (g.mask + 1) * 2 { grid_rehash(g) }
}
# row `from` becomes row `to` (which is not filed): its neighbours and its bucket follow it
function grid_moved(g: Grid, from: int, to: int) -> void {
let k = g.key[from]
g.key[to] = k
if k < 0 { return }
let p = g.prev[from]
let n = g.next[from]
g.prev[to] = p
g.next[to] = n
if p > 0 { g.next[p - 1] = to + 1 } else { g.head[grid_bucket(g, grid_key_x(k), grid_key_z(k))] = to + 1 }
if n > 0 { g.prev[n - 1] = to + 1 }
g.key[from] = -1
}
# twice the buckets, every filed row linked again where its key now hashes
function grid_rehash(g: Grid) -> void {
g.mask = g.mask * 2 + 1
g.head = words(g.mask + 1)
g.count = 0
for r in 0 .. len(g.key) {
let k = g.key[r]
if k >= 0 { grid_link(g, r, k, grid_bucket(g, grid_key_x(k), grid_key_z(k))) }
}
}