fix(base): an empty queue drains and clears without allocating

Ludic frees nothing a safe program allocates, and every package's fact queue is drained once a
frame: q_drain handed back its list and made a new one each time, most often of an empty queue.
An empty drain now returns the queue's one shared empty list (never to be pushed to), and q_clear
clears in place - the live list is never one a drain handed out. A queue that held facts still
gives its list away, so what leaks is in proportion to what happened, not to the frame rate.
queue_test holds it (an empty drain is the same list twice; a drained list is untouched by later
pushes and clears).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-27 21:16:19 +03:00
parent f802bdd3ec
commit ab34f8229b
2 changed files with 28 additions and 2 deletions

View file

@ -3,6 +3,7 @@
export property Queue<T> {
items: []T = null
tag: QueueTag = null
none: []T = null # what draining an empty queue hands back, never pushed to
}
# what a queue says about itself, whatever it holds: its name and how many facts wait in it
export property QueueTag {
@ -22,6 +23,7 @@ export function queue_new<T>(name: string) -> Queue<T> {
q.items = new []T
q.tag = new QueueTag
q.tag.name = name
q.none = new []T
return q
}
@ -30,8 +32,11 @@ export function q_push<T>(q: Queue<T>, v: T) -> void {
q.tag.pending = len(q.items)
}
# the facts in the order they were pushed; the queue is empty afterwards
# the facts in the order they were pushed; the queue is empty afterwards. Most queues are empty
# most frames, and Ludic frees nothing: an empty drain hands back one shared empty list rather
# than a new one a frame. Read what a drain returns; never push to it.
export function q_drain<T>(q: Queue<T>) -> []T {
if len(q.items) == 0 { return q.none }
let out = q.items
q.items = new []T
q.tag.pending = 0
@ -40,8 +45,9 @@ export function q_drain<T>(q: Queue<T>) -> []T {
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 {
q.items = new []T
List.clear(q.items)
q.tag.pending = 0
}

View file

@ -67,7 +67,27 @@ program QueueTest {
expect_eq(len(core_undrained(tags)), 0)
}
# an empty drain allocates nothing and hands back the same empty list; a drained list is the
# caller's, untouched by what is pushed or cleared after it
function empty_case() -> void {
let q: Queue<int> = queue_new("quiet")
let a = q_drain(q)
let b = q_drain(q)
expect_eq(len(a), 0)
expect(a == b)
q_push(q, 5)
expect_eq(len(a), 0)
let got = q_drain(q)
q_push(q, 6)
q_clear(q)
expect_eq(len(got), 1)
expect_eq(got[0], 5)
expect_eq(q_len(q), 0)
expect_eq(len(q_drain(q)), 0)
}
test "a queue drains in the order it was pushed" () { fifo_case() }
test "an empty drain allocates nothing, and a drained list is the caller's" () { empty_case() }
test "a queue holds records" () { record_case() }
test "clear drops what is waiting" () { clear_case() }
test "a queue's element type is told by the queue alone" () { infer_case() }