o3 0.36.2

shared-nothing primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
pub mod common;

use std::{array, io::Read as _, mem, num::NonZero};

use common::{
    alloc::system::Tracker,
    buffer::{try_item, try_many},
};
use o3::{
    buffer::{
        raw::{Chunks as _, Scatter as _},
        segments::{self, queue, scatter},
        storage,
        write::consume::Consume as _,
    },
    depot::capacity::{self, Count},
};

#[global_allocator]
static ALLOCATOR: Tracker = Tracker;

#[test]
fn insert_amortizes_growth() {
    for front in [false, true] {
        let items: [_; 32] =
            array::from_fn(|index| try_item(&[index as u8]).expect("nonempty owner"));
        let pointers: [_; 32] = array::from_fn(|index| items[index].as_slice().as_ptr());
        let mut items = items.into_iter();
        let first = items.next().expect("first owner");
        let second = items.next().expect("second owner");
        let mut chain = segments::Many::try_from((first, second)).expect("initial pair");
        let (chain, allocs) = Tracker::count(|| {
            for item in items {
                chain = if front {
                    segments::Many::try_from((item, chain)).expect("growing chain")
                } else {
                    segments::Many::try_from((chain, item)).expect("growing chain")
                };
            }
            chain
        });
        assert!(
            allocs <= 5,
            "repeated insertion must amortize metadata growth"
        );
        assert_eq!(chain.as_chain().segments().len(), pointers.len());
        for (index, owner) in chain.as_chain().segments().enumerate() {
            let original = if front {
                if index < 30 { 31 - index } else { index - 30 }
            } else {
                index
            };
            assert_eq!(owner.as_slice().as_ptr(), pointers[original]);
        }
    }
}

#[test]
fn joining_amortizes_growth() {
    let chains: [_; 16] = array::from_fn(|index| {
        let first = try_item(&[(index * 2) as u8]).expect("first owner");
        let second = try_item(&[(index * 2 + 1) as u8]).expect("second owner");
        segments::Many::try_from((first, second)).expect("input pair")
    });
    let mut chains = chains.into_iter();
    let mut chain = chains.next().expect("first pair");
    let (chain, allocs) = Tracker::count(|| {
        for source in chains {
            chain = segments::Many::try_from((chain, source)).expect("joined owners");
        }
        chain
    });
    assert!(allocs <= 5, "repeated joins must amortize metadata growth");
    assert!(
        chain
            .as_chain()
            .segments()
            .map(|owner| owner.as_slice()[0])
            .eq(0..32)
    );
}

#[test]
fn growth_uses_local_count() {
    let pool = segments::Pool::try_new(segments::Plan {
        segments: 64,
        chains: 2,
    })
    .expect("shared metadata pool");
    let chain = try_many(&pool).expect("small chain");
    let mut other = pool.try_chain().expect("other chain");
    for _ in 0..62 {
        other
            .try_insert(try_item(b"x").expect("retained owner"))
            .expect("shared node capacity");
    }
    let item = try_item(b"last").expect("incoming owner");
    let bytes = {
        let measured = Tracker::usage(|| {
            segments::Pool::<storage::Shared>::try_new(segments::Plan {
                segments: 4,
                chains: 1,
            })
            .expect("local reservation reference")
        });
        measured.1.1
    };
    let (chain, activity) =
        Tracker::measure(|| segments::Many::try_from((chain, item)).expect("detached small chain"));
    assert_eq!(activity, [1, bytes, 0, 0]);
    assert_eq!(chain.as_chain().segments().len(), 3);
    assert_eq!(other.segments().len(), 62);
}

#[test]
fn chains_share_nodes() {
    const CAPACITY: usize = 4;
    const CHAINS: usize = 2;
    const BYTES: usize = 72 + CAPACITY * 32 + CHAINS * 40;

    let (result, build) = Tracker::measure(|| {
        segments::Pool::<storage::Shared>::try_new(segments::Plan {
            segments: CAPACITY,
            chains: CHAINS,
        })
    });
    let pool = result.expect("the four-segment two-chain pool must allocate");
    assert_eq!(build, [1, BYTES, 0, 0]);

    let result = storage::Shared::try_copy(b"first");
    let first = result.expect("first retained segment must allocate");
    let result = storage::Shared::try_copy(b"second");
    let second = result.expect("second retained segment must allocate");
    let held_a = (first.try_clone()).expect("retained edge must fit");
    let held_b = (second.try_clone()).expect("retained edge must fit");
    let loan_a = (first.try_clone()).expect("retained edge must fit");
    let loan_b = (second.try_clone()).expect("retained edge must fit");
    let ((), hot) = Tracker::measure(|| {
        {
            let mut retained = (pool.try_chain()).expect("retained edge must fit");
            retained
                .try_push(held_a)
                .as_ref()
                .expect("required fixture outcome");
            retained
                .try_push(held_b)
                .as_ref()
                .expect("required fixture outcome");
            let front = retained
                .front_entry()
                .expect("the pooled chain retains its first owner")
                .pop();
            assert_eq!(front.as_slice(), b"first");
            assert_eq!(retained.contiguous(), Some(&b"second"[..]));
            retained
                .try_push(front)
                .as_ref()
                .expect("required fixture outcome");
            let result = retained.try_take(5);
            let mut prefix = result.expect("one retained segment must move into a prefix");
            retained
                .try_append(&mut prefix)
                .as_ref()
                .expect("required fixture outcome");
            let front = (retained.front_entry()).expect("the joined chain retains its first owner");
            let ptr = front.get().as_slice().as_ptr();
            let owner = front.pop();
            assert_eq!(owner.as_slice().as_ptr(), ptr);
            retained
                .try_push(owner)
                .as_ref()
                .expect("required fixture outcome");
        }

        {
            let mut scoped = pool.scoped();
            scoped
                .try_push(loan_a)
                .as_ref()
                .expect("required fixture outcome");
            scoped
                .try_push(loan_b)
                .as_ref()
                .expect("required fixture outcome");
            assert_eq!(scoped.segments().len(), 2);
            let mut output = [0; 11];
            let mut reader = scatter::Reader::new(scoped.segments());
            reader
                .read_exact(&mut output)
                .as_ref()
                .expect("scatter reader must fill the exact retained byte length");
            assert_eq!(&output, b"firstsecond");
            assert_eq!(reader.read(&mut [0]).expect("exhausted reader"), 0);
        }
    });
    assert_eq!(hot, [0; 4]);
}

#[test]
fn pool_retires_once() {
    const CAPACITY: usize = 4;
    const CHAINS: usize = 2;
    const BYTES: usize = 72 + CAPACITY * 32 + CHAINS * 40;

    let ((), lifetime) = Tracker::measure(|| {
        let last = {
            let result = segments::Pool::<storage::Shared>::try_new(segments::Plan {
                segments: CAPACITY,
                chains: CHAINS,
            });
            let pool = result.expect("the lifetime pool must allocate");
            let first = (pool.try_chain()).expect("retained edge must fit");
            let second = (pool.try_chain()).expect("retained edge must fit");
            assert_eq!(first.segments().len(), 0);
            second
        };
        assert_eq!(last.segments().len(), 0);
    });

    assert_eq!(lifetime, [1, BYTES, 1, BYTES]);
}

#[test]
fn segments_recycle() {
    let capacity = queue::Capacity::new(Count::new(8), Count::new(2));
    let bytes = 2 * mem::size_of::<storage::Shared>();
    let mut fifo =
        queue::Fifo::try_with(capacity).expect("the two-owner segment queue must allocate");

    let abc = (storage::Shared::try_copy(b"abc")).expect("the first segment owner must allocate");
    let def = (storage::Shared::try_copy(b"def")).expect("the second segment owner must allocate");
    let gh = (storage::Shared::try_copy(b"gh")).expect("the recycled segment owner must allocate");
    let first = (abc.try_clone()).expect("the first retained segment must fit");
    let second = (def.try_clone()).expect("the second retained segment must fit");
    let third = (gh.try_clone()).expect("the recycled retained segment must fit");
    let ((), hot) = Tracker::measure(|| {
        fifo.try_push(first)
            .as_ref()
            .expect("the first segment must fit");
        fifo.try_push(second)
            .as_ref()
            .expect("the second segment must fit");
        let () = (fifo.try_consume(4)).expect("the queued four-byte prefix must be consumable");
        fifo.try_push(third)
            .as_ref()
            .expect("the released segment slot must accept the recycled owner");

        let mut output = [0; 4];
        queue::Array::try_new(&fifo.cursor(), 0)
            .expect("retained four-byte suffix")
            .copy(&mut output);
        assert_eq!(output, *b"efgh");
        assert_eq!(fifo.segments(), 2);
    });
    assert_eq!(hot, [0; 4]);

    let ((), drops) = Tracker::measure(|| {
        let released = fifo;
        assert_eq!(released.len(), 4);
    });
    assert_eq!(drops, [0, 0, 1, bytes]);
    assert_eq!(
        (abc.as_slice(), def.as_slice(), gh.as_slice()),
        (&b"abc"[..], &b"def"[..], &b"gh"[..])
    );
}

#[test]
fn releases_front_slot() {
    let pool = (segments::Pool::try_new(segments::Plan {
        segments: 3,
        chains: 1,
    }))
    .expect("the front-transfer pool must allocate");
    let first = (storage::Shared::try_copy(b"first")).expect("the transferred owner must allocate");
    let ptr = first.as_slice().as_ptr();
    let mut chain = (pool.try_chain()).expect("metadata edge must fit");
    chain
        .try_push(first)
        .as_ref()
        .expect("metadata admits the owner");
    let second =
        (storage::Shared::try_copy(b"second")).expect("the retained tail owner must allocate");
    chain
        .try_push(second)
        .as_ref()
        .expect("metadata admits the owner");
    let third = (storage::Shared::try_copy(b"third")).expect("the final owner must allocate");
    chain
        .try_push(third)
        .as_ref()
        .expect("metadata admits the owner");

    let (chain,) = match chain.into_shape() {
        segments::Shape::Many(chain) => Some((chain,)),
        _ => None,
    }
    .expect("the fixture retains three segment owners");
    let (owner, chain) = chain.take_front();
    assert_eq!(owner.as_slice().as_ptr(), ptr);
    assert_eq!(owner.as_slice(), b"first");
    let (chain,) = match chain {
        segments::Nonempty::Many(chain) => Some((chain,)),
        segments::Nonempty::One(_) => None,
    }
    .expect("two remaining owners must keep their descriptor");
    assert!(
        chain
            .as_chain()
            .chunks()
            .take_bytes(usize::MAX)
            .flat_map(|chunk| chunk.bytes())
            .eq(b"secondthird")
    );
    assert_eq!(chain.as_chain().len(), 11);
    assert!(chain.as_chain().count() == segments::Count::TWO);
    assert_eq!(pool.available().segments, 1);
    assert_eq!(pool.available().chains, 0);
    let (owner, chain) = chain.take_front();
    assert_eq!(owner.as_slice(), b"second");
    let (owner,) = match chain {
        segments::Nonempty::One(owner) => Some((owner,)),
        segments::Nonempty::Many(_) => None,
    }
    .expect("the last owner must leave the descriptor inline");
    assert_eq!(owner.as_slice(), b"third");
    assert_eq!(pool.available().segments, 3);
    assert_eq!(pool.available().chains, 1);
}

#[test]
fn keeps_failed_rehome() {
    let spool = (segments::Pool::try_new(segments::Plan {
        segments: 2,
        chains: 1,
    }))
    .expect("the failed-rehome source pool must allocate");
    let tpool = (segments::Pool::try_new(segments::Plan {
        segments: 1,
        chains: 0,
    }))
    .expect("the undersized rehome target pool must allocate");
    let mut source = (spool.try_chain()).expect("metadata edge must fit");
    let first =
        (storage::Shared::try_copy(b"first")).expect("the failed-rehome head must allocate");
    let second =
        (storage::Shared::try_copy(b"second")).expect("the failed-rehome tail must allocate");
    source
        .try_push(first)
        .as_ref()
        .expect("metadata admits the owner");
    source
        .try_push(second)
        .as_ref()
        .expect("metadata admits the owner");
    let mut target = (tpool.try_chain()).expect("metadata edge must fit");

    assert!(matches!(
        source.try_move_to(&mut target, source.len()),
        Err(segments::Error::SegmentCapacity(capacity::Shortage {
            requested,
            available: 1
        })) if requested.get() == 2
    ));
    assert!(
        source
            .chunks()
            .take_bytes(usize::MAX)
            .flat_map(|chunk| chunk.bytes())
            .eq(b"firstsecond")
    );
    assert!(target.is_empty());
    assert_eq!(spool.available().segments, 0);
    assert_eq!(spool.available().chains, 0);
    assert_eq!(tpool.available().segments, 1);
    assert_eq!(tpool.available().chains, 0);
}

#[test]
fn keeps_failed_split() {
    let pool = (segments::Pool::try_new(segments::Plan {
        segments: 4,
        chains: 1,
    }))
    .expect("the failed-split pool must allocate");
    let mut chain = (pool.try_chain()).expect("metadata edge must fit");
    for value in [b"a".as_slice(), b"b", b"c", b"d"] {
        let owner =
            (storage::Shared::try_copy(value)).expect("each failed-split owner must allocate");
        chain
            .try_push(owner)
            .as_ref()
            .expect("metadata admits the owner");
    }

    assert!(matches!(
        chain.try_take(2),
        Err(segments::Error::ChainCapacity(capacity::Shortage {
            requested: NonZero::<usize>::MIN,
            available: 0
        }))
    ));
    assert!(
        chain
            .chunks()
            .take_bytes(usize::MAX)
            .flat_map(|chunk| chunk.bytes())
            .eq(b"abcd")
    );
    assert!(chain.count() == segments::Count::new(4));
}