SyncCow: A thread-safe clone-on-write container for fast concurrent writing and reading

51 points w0xel

I was looking for a container like this when working on another project and didn't find any. So I implemented it myself: https://crates.io/crates/sync\_cow

It's like \RwLock\\, as you can share it between threads and read and write safely, but faster in certain circumstances. The difference is that "read" operations are never blocked and will return instantly, while write operations are only blocked by other write operations. In other words: you can read and write at the same time.

This is especially helpful in a performance-critical context where a value is often read by many threads and seldom updated. The disadvantages are a higher memory footprint, as the container stores at least two copies of the data (and more for every reader that still reads old data) and for large data structures the clone-on-write operation will nullify performance benefits (but the readers will still be super fast). I think I could reduce the memory overhead by using \Weak\\ pointers for the duplicates, but that's a nice optimization for the future when required.

I'd be super happy for an audit or two, as there are a few \unsafe\\ lines in it, and general feedback. I'll bump the version to \1.0.0\\ and keep backwards-compatibility once your feedback is implemented.

If there's already a better lib that does the above also let me know, I'll take it as a nice learning exercise.