4 releases

0.1.3 Sep 3, 2023
0.1.2 Mar 29, 2022
0.1.1 Mar 10, 2022
0.1.0 Mar 8, 2022

#155 in Concurrency

Download history 1012/week @ 2023-12-11 1247/week @ 2023-12-18 737/week @ 2023-12-25 873/week @ 2024-01-01 1322/week @ 2024-01-08 1225/week @ 2024-01-15 1039/week @ 2024-01-22 927/week @ 2024-01-29 1233/week @ 2024-02-05 1579/week @ 2024-02-12 1283/week @ 2024-02-19 1258/week @ 2024-02-26 2191/week @ 2024-03-04 2265/week @ 2024-03-11 1795/week @ 2024-03-18 1759/week @ 2024-03-25

8,124 downloads per month
Used in 12 crates (7 directly)

MIT/Apache

14KB
174 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