ludic.physics, ludic.character, ludic.vehicles: fact records reused, and phys_sink allocates nothing

The per-frame sweep over physics, character, vehicles and nav found three fact makers that made a record per fact: contacts and splashes, the character's and the vehicles'. Each keeps a pool now, as wildlife's and npc's do (q_unheld). phys_sink, asked on every removal, built two new lists each time; it filters into a kept spare pair and swaps. ludic.nav's paths and crowds already allocate nothing, and none of the four builds a string per call. What remains at load, reset or a world swap is not per frame. vehicle_feed takes its state mut. vehicles_test: 1000 fed facts, drained turn by turn, use at most 4 records. All packages 421.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-28 02:03:09 +03:00
parent fe2d2bacd7
commit fffedf71d0
10 changed files with 92 additions and 11 deletions

View file

@ -4,7 +4,7 @@
export function vehicle_own(v: Vehicle) -> void { if v != null { v.owned = true } }
# hay for a horse, up to a full belly
export function vehicle_feed(vehicles_st: VehiclesState, v: Vehicle, amount: float) -> void {
export function vehicle_feed(vehicles_st: mut VehiclesState, v: Vehicle, amount: float) -> void {
if v == null { return }
v.food = Math.min(v.food + amount, 100.0)
ve_say(vehicles_st, VEHICLE_FED, v, amount)

View file

@ -63,6 +63,9 @@ export state VehiclesState {
ve_ly: float = 0.0
ve_lz: float = 0.0
ve_fact_q: Queue<VehicleFact> = ve_fact_q__new()
ve_fpool: []VehicleFact = new []VehicleFact # every fact record made, reused once no drain holds it
ve_ffree: []VehicleFact = new []VehicleFact
ve_fnext: int = 0
}
function ve__dry(x: float, z: float) -> float { return -10000.0 }
function ve__calm(x: float, z: float, fx: float, fz: float) -> float { return 0.0 }

View file

@ -6,8 +6,26 @@ export function vehicle_facts(vehicles_st: VehiclesState) -> Queue<VehicleFact>
return vehicles_st.ve_fact_q
}
function ve_say(vehicles_st: VehiclesState, what: int, v: Vehicle, n: float) -> void {
# a fact record no drain holds any more, made fresh (and kept) only when every one is held: a
# fact can come every frame, and a record a frame is never given back
function ve_fact_record(vehicles_st: mut VehiclesState) -> VehicleFact {
if vehicles_st.ve_fnext >= len(vehicles_st.ve_ffree) {
q_unheld(vehicle_facts(vehicles_st), vehicles_st.ve_fpool, vehicles_st.ve_ffree)
vehicles_st.ve_fnext = 0
}
if vehicles_st.ve_fnext < len(vehicles_st.ve_ffree) {
let f = vehicles_st.ve_ffree[vehicles_st.ve_fnext]
vehicles_st.ve_fnext += 1
return f
}
let f = new VehicleFact
push(vehicles_st.ve_fpool, f)
return f
}
# how many fact records have ever been made: the peak held at once, not the facts pushed
export function vehicle_fact_records(vehicles_st: VehiclesState) -> int { return len(vehicles_st.ve_fpool) }
function ve_say(vehicles_st: mut VehiclesState, what: int, v: Vehicle, n: float) -> void {
let f = ve_fact_record(vehicles_st)
f.what = what
f.nid = v.nid
f.kind = v.kind

View file

@ -375,4 +375,17 @@ program VehiclesTest {
expect_eq(len(vehicles_all(vehicles_st)), 0)
expect(vehicle_by_nid(vehicles_st, b.nid) == null)
}
test "a fed fact a turn, drained each turn, makes a few records and then reuses them" (vehicles_st: mut VehiclesState, vehicles_test_st: mut VehiclesTestState) {
fresh(vehicles_st, vehicles_test_st)
vehicle_call_for(vehicles_st, 0, VEHICLE_HORSE, 0)
let h = vehicle_find(vehicles_st, VEHICLE_HORSE)
var fed = 0
for k in 0 .. 1000 {
vehicle_feed(vehicles_st, h, 1.0)
fed += count(facts(vehicles_st), VEHICLE_FED)
}
expect_eq(fed, 1000)
expect(vehicle_fact_records(vehicles_st) <= 4)
}
}