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
79
packages/ludic.base/ecs_map.ludic
Normal file
79
packages/ludic.base/ecs_map.ludic
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
# ludic.base/ecs_map.ludic - IntMap: a key (0 or more) to a value, open addressing with linear
|
||||
# probing; what turns a stable id kept in a save or a message into a handle without a scan
|
||||
export property IntMap {
|
||||
keys: words = null # -1 empty, -2 a key that was deleted
|
||||
vals: words = null
|
||||
mask: int = 63
|
||||
n: int = 0
|
||||
used: int = 0 # live and deleted slots: what makes probing long
|
||||
}
|
||||
|
||||
export function imap_new() -> IntMap {
|
||||
let m = new IntMap
|
||||
imap_alloc(m, 64)
|
||||
return m
|
||||
}
|
||||
|
||||
function imap_alloc(m: IntMap, cap: int) -> void {
|
||||
m.keys = words(cap)
|
||||
m.vals = words(cap)
|
||||
for i in 0 .. cap { m.keys[i] = -1 }
|
||||
m.mask = cap - 1
|
||||
m.n = 0
|
||||
m.used = 0
|
||||
}
|
||||
|
||||
function imap_slot(m: IntMap, k: int) -> int { return ((k * -1640531535) >> 7) & m.mask }
|
||||
|
||||
# the value under k, or `none` when there is none
|
||||
export function imap_get(m: IntMap, k: int, none: int) -> int {
|
||||
var s = imap_slot(m, k)
|
||||
while m.keys[s] != -1 {
|
||||
if m.keys[s] == k { return m.vals[s] }
|
||||
s = (s + 1) & m.mask
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
export function imap_put(m: IntMap, k: int, v: int) -> void {
|
||||
if (m.used + 1) * 4 > (m.mask + 1) * 3 { imap_grow(m) }
|
||||
var s = imap_slot(m, k)
|
||||
var tomb = -1
|
||||
while m.keys[s] != -1 {
|
||||
if m.keys[s] == k {
|
||||
m.vals[s] = v
|
||||
return
|
||||
}
|
||||
if m.keys[s] == -2 and tomb < 0 { tomb = s }
|
||||
s = (s + 1) & m.mask
|
||||
}
|
||||
if tomb >= 0 { s = tomb } else { m.used += 1 }
|
||||
m.keys[s] = k
|
||||
m.vals[s] = v
|
||||
m.n += 1
|
||||
}
|
||||
|
||||
export function imap_del(m: IntMap, k: int) -> bool {
|
||||
var s = imap_slot(m, k)
|
||||
while m.keys[s] != -1 {
|
||||
if m.keys[s] == k {
|
||||
m.keys[s] = -2
|
||||
m.n -= 1
|
||||
return true
|
||||
}
|
||||
s = (s + 1) & m.mask
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function imap_len(m: IntMap) -> int { return m.n }
|
||||
export function imap_clear(m: IntMap) -> void { imap_alloc(m, m.mask + 1) }
|
||||
|
||||
function imap_grow(m: IntMap) -> void {
|
||||
let ok = m.keys
|
||||
let ov = m.vals
|
||||
var cap = m.mask + 1
|
||||
if m.n * 2 > cap { cap = cap * 2 }
|
||||
imap_alloc(m, cap)
|
||||
for i in 0 .. len(ok) { if ok[i] >= 0 { imap_put(m, ok[i], ov[i]) } }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue