Merge branch 'lang/npc-pool' into lang/leaks2

This commit is contained in:
Orkun ÇAKILKAYA 2026-09-28 15:49:07 +03:00
commit f8358d117a
4 changed files with 32 additions and 5 deletions

View file

@ -4,6 +4,7 @@ module ludic_npc uses ludic_base
numbers float numbers float
import "ludic.base" import "ludic.base"
import "defs.ludic" import "defs.ludic"
import "pool.ludic"
import "person.ludic" import "person.ludic"
import "port.ludic" import "port.ludic"
import "ground.ludic" import "ground.ludic"

View file

@ -13,8 +13,8 @@ export state NpcState {
np_n: int = 0 # walkers about np_n: int = 0 # walkers about
np_dice: Rng = rng_new(20260925) np_dice: Rng = rng_new(20260925)
np_facts: Queue<NpcFact> = np_facts__new() np_facts: Queue<NpcFact> = np_facts__new()
np_fpool: []NpcFact = new []NpcFact # every fact record made, reused once no drain holds it np_fpool: []NpcFact = np_pool_new() # every fact record made, reused once no drain holds it
np_ffree: []NpcFact = new []NpcFact np_ffree: []NpcFact = np_list_sized()
np_fnext: int = 0 np_fnext: int = 0
np_next_h: float = 0.0 # the hour the census is next looked at np_next_h: float = 0.0 # the hour the census is next looked at
np_hold: bool = false # held where they stand (a shot of their poses) np_hold: bool = false # held where they stand (a shot of their poses)
@ -109,4 +109,4 @@ function np_hi(lo: int, hi: int) -> int {
if hi < lo { return lo } if hi < lo { return lo }
return hi return hi
} }
function np_facts__new() -> Queue<NpcFact> { return queue_new("npc.facts") } function np_facts__new() -> Queue<NpcFact> { return np_queue_sized(queue_new("npc.facts")) }

View file

@ -0,0 +1,26 @@
# ludic.npc/pool.ludic - the facts' records and the queue's lists made at their size once, at the
# start: a frame's facts are reused records, and a frame with more than it has seen before still
# makes none until NP_FACTS are held at once
const NP_FACTS: int = 64
function np_pool_new() -> []NpcFact {
let l = new []NpcFact
for i in 0 .. NP_FACTS { push(l, new NpcFact) }
return l
}
# an empty list with room for them all: q_unheld fills it without growing it
function np_list_sized() -> []NpcFact {
let l = np_pool_new()
List.clear(l)
return l
}
function np_queue_sized(q: Queue<NpcFact>) -> Queue<NpcFact> {
let f = new NpcFact
for i in 0 .. NP_FACTS {
push(q.items, f)
push(q.spare, f)
}
List.clear(q.items)
List.clear(q.spare)
return q
}

View file

@ -102,7 +102,7 @@ program NpcWayTest {
expect(p.phase > 100.0) expect(p.phase > 100.0)
} }
test "an arrival a turn, drained each turn, makes a few fact records and then reuses them" (npc_st: mut NpcState, npc_tests_fake_st: mut NpcTestsFakeState, way_test_st: WayTestState) { test "an arrival a turn, drained each turn, makes no fact record past the ones made at the start" (npc_st: mut NpcState, npc_tests_fake_st: mut NpcTestsFakeState, way_test_st: WayTestState) {
fk_setup(npc_st, npc_tests_fake_st) fk_setup(npc_st, npc_tests_fake_st)
npc_tests_fake_st.fk_want = 1 npc_tests_fake_st.fk_want = 1
let p = npc_spawn(npc_st, NPCK_HIKER) let p = npc_spawn(npc_st, NPCK_HIKER)
@ -115,6 +115,6 @@ program NpcWayTest {
for i in 0 .. len(fs) { if fs[i].what == NPC_F_ARRIVED { arrived += 1 } } for i in 0 .. len(fs) { if fs[i].what == NPC_F_ARRIVED { arrived += 1 } }
} }
expect(arrived > 400) expect(arrived > 400)
expect(npc_fact_records(npc_st) <= 4) expect_eq(npc_fact_records(npc_st), 64)
} }
} }