ludic/packages/ludic.base/queue.ludic
Orkuncakilkaya c74099f5f8 fix(base): a queue drain allocates nothing - two lists, reused
The rest of the leak Physics found: a drain that carried facts gave its list away and made a new
one, every queue every frame something happened. The queue keeps two lists and hands one out while
the other fills; a drained list is good until the next drain of that queue (nothing in the game or
the packages keeps one past it). With ab34f82's empty drain and in-place clear, a queue allocates
nothing in steady state. queue_test holds the reuse; every package (403) and lab/unit (90) pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 22:55:43 +03:00

70 lines
2.4 KiB
Text

# ludic.base/queue.ludic - a mechanic reports what HAPPENED as facts in a queue it owns, and the
# game drains it in a later phase. First in, first out; nothing here rolls dice or reorders.
export property Queue<T> {
items: []T = null
tag: QueueTag = null
none: []T = null # what draining an empty queue hands back, never pushed to
spare: []T = null # the list the last drain handed out: cleared and reused by the next
}
# what a queue says about itself, whatever it holds: its name and how many facts wait in it
export property QueueTag {
name: string = ""
pending: int = 0
}
# BaseState is the system runner's (system.ludic). A queue keeps its own count, so pushing and
# draining one takes only the state that owns the queue.
export state BaseState {
cs_list: []System = null
}
# a queue; the name is only for core_undrained
export function queue_new<T>(name: string) -> Queue<T> {
let q = new Queue<T>
q.items = new []T
q.tag = new QueueTag
q.tag.name = name
q.none = new []T
q.spare = new []T
return q
}
export function q_push<T>(q: Queue<T>, v: T) -> void {
push(q.items, v)
q.tag.pending = len(q.items)
}
# the facts in the order they were pushed; the queue is empty afterwards. Ludic frees nothing, so
# a drain allocates nothing: the queue keeps two lists and hands one out while the other fills, and
# an empty drain hands back one shared empty list. What a drain returns is good until the NEXT
# drain of the same queue - read it, never push to it, never keep it.
export function q_drain<T>(q: Queue<T>) -> []T {
if len(q.items) == 0 { return q.none }
let out = q.items
List.clear(q.spare)
q.items = q.spare
q.spare = out
q.tag.pending = 0
return out
}
export function q_len<T>(q: Queue<T>) -> int { return len(q.items) }
# in place: the live list was never handed out (a drain gives its list away and starts another)
export function q_clear<T>(q: Queue<T>) -> void {
List.clear(q.items)
q.tag.pending = 0
}
# a queue's tag, for core_undrained: queues of different facts in one list
export function q_tag<T>(q: Queue<T>) -> QueueTag { return q.tag }
# the names of the given queues still holding facts; ask it at the end of a frame, where a fact
# left behind is a route nobody wrote
export function core_undrained(tags: []QueueTag) -> []string {
let out = new []string
for i in 0 .. len(tags) {
if tags[i] != null and tags[i].pending > 0 { push(out, tags[i].name) }
}
return out
}