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
116
packages/ludic.base/tests/ecs_test.ludic
Normal file
116
packages/ludic.base/tests/ecs_test.ludic
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
# ecs_test.ludic - Table<T>: handles that go stale, dense rows after a removal, the setters keeping
|
||||
# the grid and the kind index current, change ticks, and IntMap. Each case is a function because a
|
||||
# generic call is not resolved inside a `test` body yet.
|
||||
import "ludic.base"
|
||||
program EcsTest {
|
||||
numbers float
|
||||
property Toy { name: string = "" }
|
||||
const CX: int = 0
|
||||
const CZ: int = 1
|
||||
const KIND: int = 0
|
||||
const ON: int = 1
|
||||
|
||||
function toy(name: string) -> Toy {
|
||||
let t = new Toy
|
||||
t.name = name
|
||||
return t
|
||||
}
|
||||
|
||||
function put(tb: Table<Toy>, name: string, x: float, z: float, kind: int) -> int {
|
||||
let h = tb_add(tb, toy(name))
|
||||
let r = tb_row(tb, h)
|
||||
tb_set_xz(tb, CX, CZ, r, x, z)
|
||||
tb_set_i(tb, KIND, r, kind)
|
||||
tb_set_i(tb, ON, r, 1)
|
||||
return h
|
||||
}
|
||||
|
||||
function handles_case() -> void {
|
||||
let tb: Table<Toy> = table_new(2, 2)
|
||||
let a = put(tb, "a", 0.0, 0.0, 1)
|
||||
let b = put(tb, "b", 1.0, 0.0, 1)
|
||||
let c = put(tb, "c", 2.0, 0.0, 2)
|
||||
expect_eq(tb_len(tb), 3)
|
||||
expect(tb_remove(tb, a))
|
||||
expect(not tb_alive(tb, a))
|
||||
expect(not tb_remove(tb, a))
|
||||
expect_eq(tb_len(tb), 2)
|
||||
expect_eq(tb_rec(tb, tb_row(tb, c)).name, "c")
|
||||
expect_eq(tb_rec(tb, tb_row(tb, b)).name, "b")
|
||||
let d = put(tb, "d", 3.0, 0.0, 1)
|
||||
expect(d != a)
|
||||
expect_eq(d & ECS_SLOT, a & ECS_SLOT)
|
||||
expect(not tb_alive(tb, a))
|
||||
expect_eq(tb_rec(tb, tb_row(tb, d)).name, "d")
|
||||
}
|
||||
|
||||
function grid_case() -> void {
|
||||
let tb: Table<Toy> = table_new(2, 2)
|
||||
let g = tb_grid(tb, CX, CZ, ON, 16.0)
|
||||
let near = put(tb, "near", 5.0, 5.0, 1)
|
||||
put(tb, "far", 500.0, -300.0, 1)
|
||||
let fire = put(tb, "fire", -40.0, 2.0, 2)
|
||||
expect_eq(tb_handle(tb, tb_nearest(tb, g, 0.0, 0.0, 0.0, -1, 0)), near)
|
||||
expect_eq(tb_handle(tb, tb_nearest(tb, g, 0.0, 0.0, 0.0, KIND, 2)), fire)
|
||||
expect_eq(tb_nearest(tb, g, 0.0, 0.0, 10.0, KIND, 2), -1)
|
||||
tb_set_i(tb, ON, tb_row(tb, near), 0)
|
||||
expect_eq(tb_handle(tb, tb_nearest(tb, g, 0.0, 0.0, 0.0, KIND, 1)), tb_handle(tb, tb_nearest(tb, g, 499.0, -300.0, 0.0, -1, 0)))
|
||||
let out = words(0)
|
||||
expect_eq(tb_within(tb, g, 0.0, 0.0, 50.0, -1, 0, out), 1)
|
||||
tb_set_i(tb, ON, tb_row(tb, near), 1)
|
||||
expect_eq(tb_within(tb, g, 0.0, 0.0, 50.0, -1, 0, out), 2)
|
||||
tb_set_xz(tb, CX, CZ, tb_row(tb, fire), 900.0, 900.0)
|
||||
expect_eq(tb_within(tb, g, 0.0, 0.0, 50.0, -1, 0, out), 1)
|
||||
}
|
||||
|
||||
function index_case() -> void {
|
||||
let tb: Table<Toy> = table_new(2, 2)
|
||||
let ix = tb_index(tb, KIND, ON)
|
||||
let a = put(tb, "a", 0.0, 0.0, 3)
|
||||
put(tb, "b", 0.0, 0.0, 3)
|
||||
put(tb, "c", 0.0, 0.0, 5)
|
||||
expect_eq(ix_count(ix, 3), 2)
|
||||
expect_eq(ix_count(ix, 5), 1)
|
||||
expect_eq(ix_count(ix, 99), 0)
|
||||
tb_set_i(tb, ON, tb_row(tb, a), 0)
|
||||
expect_eq(ix_count(ix, 3), 1)
|
||||
tb_remove(tb, a)
|
||||
tb_set_i(tb, KIND, ix_first(ix, 3), 5)
|
||||
expect_eq(ix_count(ix, 3), 0)
|
||||
expect_eq(ix_count(ix, 5), 2)
|
||||
}
|
||||
|
||||
function ticks_case() -> void {
|
||||
let tb: Table<Toy> = table_new(2, 2)
|
||||
let a = put(tb, "a", 0.0, 0.0, 1)
|
||||
put(tb, "b", 0.0, 0.0, 1)
|
||||
let seen = tb_tick(tb)
|
||||
tb_advance(tb)
|
||||
let out = words(0)
|
||||
expect_eq(tb_changed_since(tb, seen, out), 0)
|
||||
tb_set_f(tb, CX, tb_row(tb, a), 4.0)
|
||||
expect_eq(tb_changed_since(tb, seen, out), 1)
|
||||
expect_eq(out[0], tb_row(tb, a))
|
||||
expect_eq(tb_added_since(tb, seen, out), 0)
|
||||
}
|
||||
|
||||
function map_case() -> void {
|
||||
let m = imap_new()
|
||||
for k in 0 .. 5000 { imap_put(m, k * 7, k) }
|
||||
expect_eq(imap_len(m), 5000)
|
||||
expect_eq(imap_get(m, 700, -1), 100)
|
||||
expect_eq(imap_get(m, 701, -1), -1)
|
||||
for k in 0 .. 2500 { imap_del(m, k * 14) }
|
||||
expect_eq(imap_len(m), 2500)
|
||||
expect_eq(imap_get(m, 14, -1), -1)
|
||||
expect_eq(imap_get(m, 21, -1), 3)
|
||||
imap_put(m, 14, 9)
|
||||
expect_eq(imap_get(m, 14, -1), 9)
|
||||
}
|
||||
|
||||
test "a removed handle misses, rows stay dense, a reused slot is a new handle" () { handles_case() }
|
||||
test "the grid answers nearest and within, gated, after moves" () { grid_case() }
|
||||
test "the kind index follows the setters, the gate and removals" () { index_case() }
|
||||
test "change ticks name the rows written since" () { ticks_case() }
|
||||
test "IntMap puts, finds, deletes and grows" () { map_case() }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue