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>
79 lines
2.6 KiB
Text
79 lines
2.6 KiB
Text
# ludic.base/ecs_grid.ludic - a spatial hash over two float columns: square cells hashed into
|
|
# buckets, each a doubly linked list of rows, so filing, moving and unfiling a row is O(1)
|
|
export property Grid {
|
|
cx: int = 0
|
|
cz: int = 1
|
|
gate: int = -1 # an int column: a row is filed only while it is non-zero (-1: always)
|
|
inv: float = 0.0625 # 1 / the cell's side
|
|
mask: int = 255 # buckets - 1, a power of two less one
|
|
head: words = null # bucket -> row + 1, 0 empty
|
|
next: words = null # row -> the next row in its bucket + 1
|
|
prev: words = null # row -> the previous row + 1
|
|
key: words = null # row -> its packed cell, -1 while not filed
|
|
count: int = 0
|
|
}
|
|
|
|
function grid_cell(g: Grid, v: float) -> int { return int(Math.floor(v * g.inv)) }
|
|
function grid_key(ix: int, iz: int) -> int { return ((ix & 32767) << 15) | (iz & 32767) }
|
|
function grid_bucket(g: Grid, ix: int, iz: int) -> int { return ((ix * 73856093) ^ (iz * 19349663)) & g.mask }
|
|
|
|
function grid_make(cx: int, cz: int, gate: int, cell: float, rows: int) -> Grid {
|
|
let g = new Grid
|
|
g.cx = cx
|
|
g.cz = cz
|
|
g.gate = gate
|
|
g.inv = 1.0 / cell
|
|
g.head = words(g.mask + 1)
|
|
g.next = words(0)
|
|
g.prev = words(0)
|
|
g.key = words(0)
|
|
for r in 0 .. rows { grid_grow(g) }
|
|
return g
|
|
}
|
|
|
|
# index a table spatially by columns cx and cz, in cells `cell` wide; its rows are filed at once
|
|
export function tb_grid<T>(tb: Table<T>, cx: int, cz: int, gate: int, cell: float) -> Grid {
|
|
let g = grid_make(cx, cz, gate, cell, tb.n)
|
|
push(tb.grids, g)
|
|
for r in 0 .. tb.n { tb_grid_refile(tb, g, r) }
|
|
return g
|
|
}
|
|
|
|
export function grid_count(g: Grid) -> int { return g.count }
|
|
|
|
function grid_grow(g: Grid) -> void {
|
|
push(g.next, 0)
|
|
push(g.prev, 0)
|
|
push(g.key, -1)
|
|
}
|
|
|
|
function grid_shrink(g: Grid) -> void {
|
|
List.pop(g.next)
|
|
List.pop(g.prev)
|
|
List.pop(g.key)
|
|
}
|
|
|
|
function grid_link(g: Grid, r: int, k: int, b: int) -> void {
|
|
let h = g.head[b]
|
|
g.next[r] = h
|
|
g.prev[r] = 0
|
|
if h > 0 { g.prev[h - 1] = r + 1 }
|
|
g.head[b] = r + 1
|
|
g.key[r] = k
|
|
g.count += 1
|
|
}
|
|
|
|
function grid_unlink(g: Grid, r: int) -> void {
|
|
let k = g.key[r]
|
|
if k < 0 { return }
|
|
let p = g.prev[r]
|
|
let n = g.next[r]
|
|
if p > 0 { g.next[p - 1] = n } else { g.head[grid_bucket(g, grid_key_x(k), grid_key_z(k))] = n }
|
|
if n > 0 { g.prev[n - 1] = p }
|
|
g.key[r] = -1
|
|
g.count -= 1
|
|
}
|
|
|
|
# a packed key back to its cell (sign-extended from 15 bits)
|
|
function grid_key_x(k: int) -> int { return ((k >> 15) & 32767) - (((k >> 29) & 1) * 32768) }
|
|
function grid_key_z(k: int) -> int { return (k & 32767) - (((k >> 14) & 1) * 32768) }
|