Landin library reference discuss source

core/spill/spill.ldn

1--  A vector with a private initialized inline prefix. Unused inline slots
2--  have no readable item image. The spill descriptor remains empty until
3--  the first growth allocation.
4
5import core/mem
6import core/vec
7
8spill_value: type (item: type, fixed inline_slots: usize) = struct
9    count: usize
10    inline_values: [inline_slots]item
11    spill: vec.list(item)
12    spilled: bool
13end spill_value
14
15--- Vector with an inline prefix of `inline_slots` items that spills into
16--- allocator-backed storage when it fills. Only initialized slots are
17--- readable; no item needs a zero image.  Its storage is the module's own.
18public list: type (item: type, fixed inline_slots: usize) =
19    spill_value(item, inline_slots)
20
21--- Create an empty inline vector without allocating. Unused inline slots have
22--- no readable item value.
23public new: (item: type, fixed inline_slots: usize)
24            -> (value: list(item, inline_slots)) =
25    fresh := vec.new(item: item)
26    value = (count: 0, inline_values: uninit, spill: fresh, spilled: false)
27end new
28
29--- Return the initialized item count. A read-only pointer accepts addresses
30--- of mutable and immutable containers without copying their inline storage.
31public length: (item: type, fixed inline_slots: usize,
32                value: ptr list(item, inline_slots)) -> (count: usize) =
33    count = value.val.count
34end length
35
36--- Return the inline capacity before spilling, or the backing vector capacity
37--- afterwards.
38public capacity: (item: type, fixed inline_slots: usize,
39                  value: ptr list(item, inline_slots)) -> (count: usize) =
40    if value.val.spilled then
41        count = vec.capacity(value.val.spill)
42    else
43        count = inline_slots
44    end if
45end capacity
46
47first_capacity: (fixed inline_slots: usize)
48                -> (count: usize) ! mem.out_of_memory =
49    current: usize = inline_slots
50    if current == 0 then
51        count = 8
52        return
53    end if
54    maximum: usize = 0 -% 1
55    fail mem.out_of_memory when current > maximum / 2
56    count = current * 2
57end first_capacity
58
59--- Append inline while room remains, then spill to allocated storage.
60--- Allocation failure leaves the vector unchanged. Growth or the first spill
61--- invalidates old item views.
62public push: (item: type, fixed inline_slots: usize,
63              provider: type is mem.allocator,
64              escaping inout value: list(item, inline_slots),
65              memory: provider, escaping into value added: item)
66             -> none ! mem.out_of_memory =
67    if value.spilled then
68        try vec.push(value.spill, memory, added)
69        inc value.count
70        return
71    end if
72    if value.count < inline_slots then
73        value.inline_values[value.count] = added
74        inc value.count
75        return
76    end if
77
78    wanted: usize = try first_capacity(inline_slots: inline_slots)
79    mut fresh := vec.new(item: item)
80    try vec.reserve(fresh, memory, wanted)
81
82    mut at: usize = 0
83    while at < value.count do
84        --  Only the prefix has an item image. Reserve established room for
85        --  every one of these pushes without another allocation.
86        copied: item = value.inline_values[at]
87        try vec.push(fresh, memory, copied)
88        inc at
89    end while
90    try vec.push(fresh, memory, added)
91
92    value.spill = fresh
93    value.spilled = true
94    inc value.count
95end push
96
97--- Lend the initialized mutable prefix from inline or spill storage. The
98--- container and its backing must remain valid for the view's lifetime.
99public used: (item: type, fixed inline_slots: usize,
100              inout value: list(item, inline_slots))
101             -> (view: []mut item from value) =
102    if value.spilled then
103        view = vec.used(value.spill)
104    else
105        view = value.inline_values[0..<value.count]
106    end if
107end used
108
109--- Copy the indexed item without copying the container. Reports
110--- `mem.out_of_bounds` outside its initialized prefix.
111public get: (item: type, fixed inline_slots: usize,
112             value: ptr list(item, inline_slots), index: usize)
113            -> (result: item from held value) ! mem.out_of_bounds =
114    if value.val.spilled then
115        result = try vec.get(value.val.spill, index)
116    else
117        fail mem.out_of_bounds when index >= value.val.count
118        result = value.val.inline_values[index]
119    end if
120end get
121
122--- Lend a writable item pointer, with the same binding lock as `vec.at`
123--- [0800]. Reports `mem.out_of_bounds` before forming an invalid pointer;
124--- end the pointer before a push or release.
125public at: (item: type, fixed inline_slots: usize,
126            inout value: list(item, inline_slots), index: usize)
127           -> (slot: ptr mut item from value) ! mem.out_of_bounds =
128    view: []mut item = used(value)
129    fail mem.out_of_bounds when index >= lenof view
130    slot = addr view[index]
131end at
132
133--- Remove and return the last item, retaining spill capacity. Reports
134--- `mem.empty` when empty; popping does not switch back to inline storage.
135public pop: (item: type, fixed inline_slots: usize,
136             inout value: list(item, inline_slots))
137            -> (removed: item from held value) ! mem.empty =
138    fail mem.empty when value.count == 0
139    if value.spilled then
140        removed = try vec.pop(value.spill)
141        dec value.count
142    else
143        dec value.count
144        removed = value.inline_values[value.count]
145    end if
146end pop
147
148--- Free any spill allocation through its original provider and reset to an
149--- empty inline vector. Does not clean up resources referenced by items.
150public release: (item: type, fixed inline_slots: usize,
151                 provider: type is mem.allocator,
152                 inout value: list(item, inline_slots),
153                 memory: provider) -> none =
154    if value.spilled then
155        vec.release(value.spill, memory)
156    end if
157    value.count = 0
158    value.spilled = false
159end release
160
161--- Forget every item without reading them, keeping any spill storage for
162--- the next pushes, as `vec.clear` does.  Does not clean up resources the
163--- items refer to.
164public clear: (item: type, fixed inline_slots: usize,
165               inout value: list(item, inline_slots)) -> none =
166    if value.spilled then
167        vec.clear(value.spill)
168    end if
169    value.count = 0
170end clear
171
172--- Copy the last item. Reports `mem.empty` when there is none.
173public last: (item: type, fixed inline_slots: usize,
174              value: ptr list(item, inline_slots))
175             -> (result: item from held value) ! mem.empty =
176    fail mem.empty when value.val.count == 0
177    result = get(value, value.val.count - 1) else (problem)
178        _ = problem
179        fail mem.empty
180    end
181end last
182
183--- Ensure room for at least `want` items: inline while they fit, and
184--- otherwise in spill storage of at least `want`, spilling now if the list
185--- has not.  Failure leaves the list as it was.  Spilling or growing
186--- invalidates old item views.
187public reserve: (item: type, fixed inline_slots: usize,
188                 provider: type is mem.allocator,
189                 escaping inout value: list(item, inline_slots),
190                 memory: provider, want: usize)
191                -> none ! mem.out_of_memory =
192    if value.spilled then
193        try vec.reserve(value.spill, memory, want)
194        return
195    end if
196    return when want <= inline_slots
197    mut fresh := vec.new(item: item)
198    try vec.reserve(fresh, memory, want)
199    for at in usize(0)..<value.count do
200        --  Reserve made room for every one of these without allocating.
201        copied: item = value.inline_values[at]
202        try vec.push(fresh, memory, copied)
203    end for
204    value.spill = fresh
205    value.spilled = true
206end reserve