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>
46 lines
1.5 KiB
Text
46 lines
1.5 KiB
Text
# ludic.base/ecs_plan.ludic - a query that has both a grid and a kind index picks the cheaper way:
|
|
# a rare kind's own list scanned, a common one found by rings with the kind as the match
|
|
const PLAN_LIST_MAX: int = 192 # a kind with at most this many rows is scanned from its list
|
|
|
|
export function tb_nearest_of<T>(tb: Table<T>, g: Grid, ix: IntIndex, x: float, z: float, maxr: float, v: int) -> int {
|
|
let n = ix_count(ix, v)
|
|
if n == 0 { return -1 }
|
|
if n > PLAN_LIST_MAX { return tb_nearest(tb, g, x, z, maxr, ix.col, v) }
|
|
let rows = ix.lists[v]
|
|
let xs = tb.f[g.cx]
|
|
let zs = tb.f[g.cz]
|
|
var best = -1
|
|
var bd = 1000000000000.0
|
|
if maxr > 0.0 { bd = maxr * maxr }
|
|
for k in 0 .. n {
|
|
let r = rows[k]
|
|
if g.gate >= 0 and tb.i[g.gate][r] == 0 { continue }
|
|
let dx = xs[r] - x
|
|
let dz = zs[r] - z
|
|
let d2 = dx * dx + dz * dz
|
|
if d2 < bd or (d2 == bd and best < 0) {
|
|
bd = d2
|
|
best = r
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
# within r of (x, z), of kind v, into `out`
|
|
export function tb_within_of<T>(tb: Table<T>, g: Grid, ix: IntIndex, x: float, z: float, r: float, v: int, out: words) -> int {
|
|
let n = ix_count(ix, v)
|
|
if n > PLAN_LIST_MAX { return tb_within(tb, g, x, z, r, ix.col, v, out) }
|
|
List.clear(out)
|
|
if n == 0 { return 0 }
|
|
let rows = ix.lists[v]
|
|
let xs = tb.f[g.cx]
|
|
let zs = tb.f[g.cz]
|
|
for k in 0 .. n {
|
|
let row = rows[k]
|
|
if g.gate >= 0 and tb.i[g.gate][row] == 0 { continue }
|
|
let dx = xs[row] - x
|
|
let dz = zs[row] - z
|
|
if dx * dx + dz * dz <= r * r { push(out, row) }
|
|
}
|
|
return len(out)
|
|
}
|