appendvec 0.1.6

A concurrent append-only container of immutable values
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! A concurrent append-only string.

use super::{AppendVec, bucket_len, bucketize};
#[cfg(feature = "get-size2")]
use get_size2::GetSize;
use std::ops::{Index, Range};
use std::sync::atomic::Ordering;

/// A concurrent append-only [`String`]-like container.
#[derive(Default)]
#[cfg_attr(feature = "get-size2", derive(GetSize))]
pub struct AppendStr {
    inner: AppendVec<u8>,
}

impl AppendStr {
    /// Creates a new, empty string.
    ///
    /// ```
    /// use appendvec::AppendStr;
    ///
    /// let mut container = AppendStr::new();
    /// let index = container.push_str_mut("Hello world!");
    /// assert_eq!(&container[index], "Hello world!");
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new, empty string, pre-allocating space for at least
    /// `capacity` bytes.
    ///
    /// # Panics
    ///
    /// This function panics if the capacity exceeds the maximum allocation
    /// size.
    ///
    /// ```
    /// use appendvec::AppendStr;
    ///
    /// let mut container = AppendStr::with_capacity(42);
    /// for i in 0..42 {
    ///     let bytes = [i];
    ///     let s = std::str::from_utf8(&bytes).unwrap();
    ///     let index = container.push_str_mut(s);
    ///     assert_eq!(&container[index], s);
    /// }
    /// ```
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: AppendVec::with_capacity(capacity),
        }
    }

    /// Returns the length in bytes of this collection.
    ///
    /// Given that writes can happen concurrently, beware of
    /// [TOCTOU](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
    /// bugs! The value returned here is only a lower-bound of the collection
    /// size.
    ///
    /// To know the index of an added item, use the return value of the
    /// [`push_str()`](Self::push_str) function.
    ///
    /// ```
    /// use appendvec::AppendStr;
    /// use std::thread;
    ///
    /// let container = AppendStr::with_capacity(42);
    /// thread::scope(|s| {
    ///     s.spawn(|| {
    ///         for i in 0..42 {
    ///             let index = container.push_str(std::str::from_utf8(&[i]).unwrap());
    ///             // There is only one writer thread.
    ///             assert_eq!(index, i as usize..i as usize + 1);
    ///         }
    ///     });
    ///     s.spawn(|| {
    ///         loop {
    ///             let l = container.len();
    ///             if l != 0 {
    ///                 // The unique writer thread pushes values in order.
    ///                 assert_eq!(
    ///                     &container[l - 1..l],
    ///                     std::str::from_utf8(&[l as u8 - 1]).unwrap()
    ///                 );
    ///             }
    ///             if l == 42 {
    ///                 break;
    ///             }
    ///         }
    ///     });
    /// });
    /// ```
    #[expect(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Adds the given string to this collection, and returns the byte range at
    /// which it was added.
    ///
    /// The string is guaranteed to be pushed contiguously, so that indexing the
    /// result allows to retrieve back a contiguous string. If the current
    /// bucket doesn't have enough remaining capacity to accommodate this
    /// contiguous string, it will be padded with zero bytes (which are valid
    /// UTF-8).
    ///
    /// See also [`push_str_mut()`](Self::push_str_mut), which is more efficient
    /// if you hold a mutable reference to this collection.
    ///
    /// # Panics
    ///
    /// This function panics if this collection has reached the maximum
    /// allocation size.
    ///
    /// ```
    /// use appendvec::AppendStr;
    ///
    /// let container = AppendStr::new();
    /// for i in 0..42 {
    ///     let blob = vec![123; i];
    ///     let s = std::str::from_utf8(blob.as_slice()).unwrap();
    ///     let index = container.push_str(s);
    ///     assert_eq!(&container[index], s);
    /// }
    /// ```
    pub fn push_str(&self, s: &str) -> Range<usize> {
        self.inner.push_slice_copy(s.as_bytes())
    }

    /// Adds the given string to this collection, and returns the byte range at
    /// which it was added.
    ///
    /// Contrary to [`push_str()`](Self::push_str), no write lock is held
    /// internally because this function already takes an exclusive mutable
    /// reference to this collection.
    ///
    /// # Panics
    ///
    /// This function panics if this collection has reached the maximum
    /// allocation size.
    ///
    /// ```
    /// use appendvec::AppendStr;
    ///
    /// let mut container = AppendStr::new();
    /// for i in 0..42 {
    ///     let blob = vec![123; i];
    ///     let s = std::str::from_utf8(blob.as_slice()).unwrap();
    ///     let index = container.push_str_mut(s);
    ///     assert_eq!(&container[index], s);
    /// }
    /// ```
    pub fn push_str_mut(&mut self, s: &str) -> Range<usize> {
        self.inner.push_slice_copy_mut(s.as_bytes())
    }

    /// Gets the byte slice at the given index range, without checking that they
    /// are valid UTF-8.
    ///
    /// Bypassing the UTF-8 check can be useful if you'll only look at bytes
    /// anyway, for example for string comparison.
    ///
    /// # Panics
    ///
    /// The passed `index` range:
    /// - must be correctly ordered, i.e. `index.start <= index.end`,
    /// - must be lower than the size of the collection, i.e. a call to
    ///   [`push_str()`](Self::push_str) (or its variants) that returned a
    ///   superset of `index` must have happened before this function call,
    /// - must be contained in a single contiguous bucket; this is always the
    ///   case when passing a subset of a range returned by a previous call to
    ///   [`push_str()`](Self::push_str).
    ///
    /// Otherwise, this function panics.
    ///
    /// ```
    /// use appendvec::AppendStr;
    ///
    /// let container = AppendStr::new();
    /// for i in 0..42 {
    ///     let blob = vec![123; i];
    ///     let s = std::str::from_utf8(blob.as_slice()).unwrap();
    ///     let index = container.push_str(s);
    ///     assert_eq!(container.get_bytes(index), blob.as_slice());
    /// }
    /// ```
    pub fn get_bytes(&self, index: Range<usize>) -> &[u8] {
        &self.inner[index]
    }
}

impl Index<Range<usize>> for AppendStr {
    type Output = str;

    /// # Panics
    ///
    /// The passed `index` range:
    /// - must be correctly ordered, i.e. `index.start <= index.end`,
    /// - must be lower than the size of the collection, i.e. a call to
    ///   [`push_str()`](Self::push_str) (or its variants) that returned a
    ///   superset of `index` must have happened before this function call,
    /// - must be contained in a single contiguous bucket; this is always the
    ///   case when passing a subset of a range returned by a previous call to
    ///   [`push_str()`](Self::push_str),
    /// - must be fall on UTF-8 boundaries; this is always the case when exactly
    ///   passing a range returned by a previous call to
    ///   [`push_str()`](Self::push_str).
    ///
    /// Otherwise, this function panics.
    fn index(&self, index: Range<usize>) -> &Self::Output {
        if index.start == index.end {
            return "";
        }

        assert!(index.start <= index.end);
        let index_len = index.end - index.start;

        let len = self.inner.len.load(Ordering::Acquire);
        assert!(index.end <= len);
        let (bucket, bucket_index) = bucketize(index.start, self.inner.bucket_offset);
        let bucket_len = bucket_len(bucket, self.inner.bucket_offset);
        assert!(index_len <= bucket_len - bucket_index);

        let bucket_ptr = self.inner.buckets[bucket].load(Ordering::Relaxed) as *const u8;
        debug_assert_ne!(bucket_ptr, std::ptr::null());

        let (last_bucket, last_bucket_len) = bucketize(len, self.inner.bucket_offset);
        let bucket_available_len = if bucket == last_bucket {
            last_bucket_len
        } else {
            bucket_len
        };

        // SAFETY:
        // - bucket_ptr is non-null, as this collection has an invariant that all
        //   buckets until the length are non null, futher confirmed by a debug
        //   assertion,
        //   - note that the index range isn't empty in this code branch,
        // - bucket_ptr is aligned as u8 has an alignment of 1,
        // - bucket_ptr is valid for reads of bucket_available_len items of type u8, as
        //   the length is the length of the bucket, clamped to the current length if it
        //   is the last bucket,
        // - bucket_ptr points to bucket_available_len initialized values of type u8, as
        //   the collection maintains the invariant that all items until `self.len` are
        //   initialized,
        // - the memory isn't mutated for the whole output lifetime, which is bound by
        //   the lifetime of this collection,
        // - index_len * size_of::<u8>() fits in an isize, as the checks within push()
        //   and its variants ensure that at most AppendVec::MAX_LEN + 1 items are
        //   stored in this collection.
        let bucket_slice: &[u8] =
            unsafe { std::slice::from_raw_parts(bucket_ptr, bucket_available_len) };
        // SAFETY:
        // - the collection maintains the invariant that all full buckets are valid
        //   UTF-8, as they are the concatenation of valid UTF-8 strings (pushed via the
        //   public API) followed by padding of zeros (which is also valid UTF-8),
        // - likewise, the last bucket is valid UTF-8 until the collection length, as it
        //   is the concatenation of valid UTF-8 strings that have been pushed.
        let bucket_str: &str = unsafe { std::str::from_utf8_unchecked(bucket_slice) };

        &bucket_str[bucket_index..bucket_index + index_len]
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::thread;

    #[test]
    fn test_2_bytes_utf8() {
        let s = AppendStr::new();
        for _ in 0..100 {
            let index = s.push_str("é");
            assert_eq!(index.end, index.start + 2);
            assert_eq!(&s[index.clone()], "é");
            assert_eq!(s.get_bytes(index), &[0xc3, 0xa9]);
        }
    }

    #[test]
    fn test_3_bytes_utf8() {
        let s = AppendStr::new();
        for _ in 0..100 {
            let index = s.push_str("");
            assert_eq!(index.end, index.start + 3);
            assert_eq!(&s[index.clone()], "");
            assert_eq!(s.get_bytes(index), &[0xe2, 0xa0, 0x9d]);
        }
    }

    #[test]
    fn test_4_bytes_utf8() {
        let s = AppendStr::new();
        for _ in 0..100 {
            let index = s.push_str("💖");
            assert_eq!(index.end, index.start + 4);
            assert_eq!(&s[index.clone()], "💖");
            assert_eq!(s.get_bytes(index), &[0xf0, 0x9f, 0x92, 0x96]);
        }
    }

    #[test]
    #[should_panic(expected = "byte index 2 is not a char boundary; it is inside '⠝' (bytes 0..3")]
    fn test_index_non_utf8() {
        let s = AppendStr::new();
        let index = s.push_str("");
        let _ = s[index.start..index.start + 2];
    }

    const NUM_READERS: usize = 4;
    const NUM_WRITERS: usize = 4;
    #[cfg(not(miri))]
    const NUM_ITEMS: usize = 1_000_000;
    #[cfg(miri)]
    const NUM_ITEMS: usize = 100;

    #[test]
    fn test_push_str_index_concurrent_reads() {
        const STR: &str = "⠝💖";
        const STR_LEN: usize = STR.len();

        let s = AppendStr::new();
        thread::scope(|scope| {
            for _ in 0..NUM_READERS {
                scope.spawn(|| {
                    loop {
                        let len = s.len();
                        if len > 0 {
                            let last = len - STR_LEN;
                            assert_eq!(&s[last..len], STR);
                            if len >= STR_LEN * NUM_ITEMS {
                                break;
                            }
                        }
                    }
                });
            }
            scope.spawn(|| {
                for j in 0..NUM_ITEMS {
                    assert!(s.push_str(STR).start >= STR_LEN * j);
                }
            });
        });
    }

    #[test]
    fn test_push_str_index_concurrent_writes() {
        const STR: &str = "⠝💖";
        const STR_LEN: usize = STR.len();

        let s = AppendStr::new();
        thread::scope(|scope| {
            scope.spawn(|| {
                loop {
                    let len = s.len();
                    if len > 0 {
                        let last = len - STR_LEN;
                        assert_eq!(&s[last..len], STR);
                        if len >= NUM_WRITERS * STR_LEN * NUM_ITEMS {
                            break;
                        }
                    }
                }
            });
            for _ in 0..NUM_WRITERS {
                scope.spawn(|| {
                    for j in 0..NUM_ITEMS {
                        assert!(s.push_str(STR).start >= STR_LEN * j);
                    }
                });
            }
        });
    }

    #[test]
    fn test_push_str_index_concurrent_readwrites() {
        const STR: &str = "⠝💖";
        const STR_LEN: usize = STR.len();

        let s = AppendStr::new();
        thread::scope(|scope| {
            for _ in 0..NUM_READERS {
                scope.spawn(|| {
                    loop {
                        let len = s.len();
                        if len > 0 {
                            let last = len - STR_LEN;
                            assert_eq!(&s[last..len], STR);
                            if len >= NUM_WRITERS * STR_LEN * NUM_ITEMS {
                                break;
                            }
                        }
                    }
                });
            }
            for _ in 0..NUM_WRITERS {
                scope.spawn(|| {
                    for j in 0..NUM_ITEMS {
                        assert!(s.push_str(STR).start >= STR_LEN * j);
                    }
                });
            }
        });
    }

    #[test]
    fn test_with_little_capacity() {
        const STR: &str = "⠝💖";
        const STR_LEN: usize = STR.len();

        for capacity in [0, STR_LEN] {
            let s = AppendStr::with_capacity(capacity);
            thread::scope(|scope| {
                for _ in 0..NUM_READERS {
                    scope.spawn(|| {
                        loop {
                            let len = s.len();
                            if len > 0 {
                                let last = len - STR_LEN;
                                assert_eq!(&s[last..len], STR);
                                if len >= NUM_WRITERS * STR_LEN * NUM_ITEMS {
                                    break;
                                }
                            }
                        }
                    });
                }
                for _ in 0..NUM_WRITERS {
                    scope.spawn(|| {
                        for j in 0..NUM_ITEMS {
                            assert!(s.push_str(STR).start >= STR_LEN * j);
                        }
                    });
                }
            });
        }
    }

    #[test]
    fn test_with_enough_capacity() {
        const STR: &str = "⠝💖";
        const STR_LEN: usize = STR.len();

        let s = AppendStr::with_capacity(NUM_WRITERS * STR_LEN * NUM_ITEMS);
        thread::scope(|scope| {
            for _ in 0..NUM_READERS {
                scope.spawn(|| {
                    loop {
                        let len = s.len();
                        if len > 0 {
                            let last = len - STR_LEN;
                            assert_eq!(&s[last..len], STR);
                            if len == NUM_WRITERS * STR_LEN * NUM_ITEMS {
                                break;
                            }
                        }
                    }
                });
            }
            for _ in 0..NUM_WRITERS {
                scope.spawn(|| {
                    for j in 0..NUM_ITEMS {
                        assert!(s.push_str(STR).start >= STR_LEN * j);
                    }
                });
            }
        });
        assert_eq!(s.len(), NUM_WRITERS * STR_LEN * NUM_ITEMS);
    }
}