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>
This commit is contained in:
parent
8384ad3215
commit
c74099f5f8
2 changed files with 26 additions and 4 deletions
|
|
@ -4,6 +4,7 @@ export property Queue<T> {
|
||||||
items: []T = null
|
items: []T = null
|
||||||
tag: QueueTag = null
|
tag: QueueTag = null
|
||||||
none: []T = null # what draining an empty queue hands back, never pushed to
|
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
|
# what a queue says about itself, whatever it holds: its name and how many facts wait in it
|
||||||
export property QueueTag {
|
export property QueueTag {
|
||||||
|
|
@ -24,6 +25,7 @@ export function queue_new<T>(name: string) -> Queue<T> {
|
||||||
q.tag = new QueueTag
|
q.tag = new QueueTag
|
||||||
q.tag.name = name
|
q.tag.name = name
|
||||||
q.none = new []T
|
q.none = new []T
|
||||||
|
q.spare = new []T
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,13 +34,16 @@ export function q_push<T>(q: Queue<T>, v: T) -> void {
|
||||||
q.tag.pending = len(q.items)
|
q.tag.pending = len(q.items)
|
||||||
}
|
}
|
||||||
|
|
||||||
# the facts in the order they were pushed; the queue is empty afterwards. Most queues are empty
|
# the facts in the order they were pushed; the queue is empty afterwards. Ludic frees nothing, so
|
||||||
# most frames, and Ludic frees nothing: an empty drain hands back one shared empty list rather
|
# a drain allocates nothing: the queue keeps two lists and hands one out while the other fills, and
|
||||||
# than a new one a frame. Read what a drain returns; never push to it.
|
# 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 {
|
export function q_drain<T>(q: Queue<T>) -> []T {
|
||||||
if len(q.items) == 0 { return q.none }
|
if len(q.items) == 0 { return q.none }
|
||||||
let out = q.items
|
let out = q.items
|
||||||
q.items = new []T
|
List.clear(q.spare)
|
||||||
|
q.items = q.spare
|
||||||
|
q.spare = out
|
||||||
q.tag.pending = 0
|
q.tag.pending = 0
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,23 @@ program QueueTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
test "a queue drains in the order it was pushed" () { fifo_case() }
|
test "a queue drains in the order it was pushed" () { fifo_case() }
|
||||||
|
# two lists, reused: a drained list is the caller's until the next drain of that queue
|
||||||
|
function reuse_case() -> void {
|
||||||
|
let q: Queue<int> = queue_new("reused")
|
||||||
|
q_push(q, 1)
|
||||||
|
let a = q_drain(q)
|
||||||
|
q_push(q, 2)
|
||||||
|
expect_eq(a[0], 1)
|
||||||
|
let b = q_drain(q)
|
||||||
|
expect_eq(b[0], 2)
|
||||||
|
q_push(q, 3)
|
||||||
|
let c = q_drain(q)
|
||||||
|
expect(c == a)
|
||||||
|
expect_eq(len(c), 1)
|
||||||
|
expect_eq(c[0], 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a drain reuses the queue's two lists" () { reuse_case() }
|
||||||
test "an empty drain allocates nothing, and a drained list is the caller's" () { empty_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 "a queue holds records" () { record_case() }
|
||||||
test "clear drops what is waiting" () { clear_case() }
|
test "clear drops what is waiting" () { clear_case() }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue