8 releases

0.1.7 Oct 12, 2024
0.1.6 Sep 28, 2024
0.1.5 Jul 19, 2024
0.1.4 Jun 22, 2024
0.1.2 Mar 29, 2022

#111 in Concurrency

Download history 1665/week @ 2024-07-16 1438/week @ 2024-07-23 1277/week @ 2024-07-30 2236/week @ 2024-08-06 2475/week @ 2024-08-13 2530/week @ 2024-08-20 3314/week @ 2024-08-27 3109/week @ 2024-09-03 4331/week @ 2024-09-10 3379/week @ 2024-09-17 3667/week @ 2024-09-24 6187/week @ 2024-10-01 10884/week @ 2024-10-08 11841/week @ 2024-10-15 10671/week @ 2024-10-22 8969/week @ 2024-10-29

44,107 downloads per month
Used in 19 crates (12 directly)

MIT/Apache

20KB
292 lines

Append-only-vec

Latest version Documentation Build Status

Note: currently there are frequent CI failures above, which are simply due to failure to install miri to run the test. The tests do pass when run locally.

This crate defines a single data simple structure, which is a vector to which you can only append data. It allows you to push new data values even when there are outstanding references to elements of the AppendOnlyVec. Reading from a AppendOnlyVec is much faster than if it had been protected by a std::sync::RwLock.


lib.rs:

AppendOnlyVec

This is a pretty simple type, which is a vector that you can push into, but cannot modify the elements of. The data structure never moves an element once allocated, so you can push to the vec even while holding references to elements that have already been pushed.

Scaling

  1. Accessing an element is O(1), but slightly more expensive than for a standard Vec.

  2. Pushing a new element amortizes to O(1), but may require allocation of a new chunk.

Example

use append_only_vec::AppendOnlyVec;
static V: AppendOnlyVec<String> = AppendOnlyVec::<String>::new();
let mut threads = Vec::new();
for thread_num in 0..10 {
    threads.push(std::thread::spawn(move || {
         for n in 0..100 {
              let s = format!("thread {} says {}", thread_num, n);
              let which = V.push(s.clone());
              assert_eq!(&V[which], &s);
         }
    }));
}
for t in threads {
   t.join();
}
assert_eq!(V.len(), 1000);

No runtime deps