puzpuzpuz / xsync

Concurrent data structures for Go
Apache License 2.0
1.13k stars 40 forks source link

Copy on write map #114

Open jorgebay opened 1 year ago

jorgebay commented 1 year ago

(First of all, cool project @puzpuzpuz! A faster and typed concurrent map like MapOf is something that should be part of go's stdlib)

Copy-on-write maps are useful for read-mostly performance-critical areas, as LoadOrCompute() calls only cost an atomic load for reads. Additionally, these semantics provide a snapshot view that allow fast and predictable iteration.

Typical use cases for these structures are config settings and topology management.

I can contribute it, if you think it belongs in this project.

puzpuzpuz commented 1 year ago

Hi Jorge,

I'm all for a contribution. COW data structures are on the radar and I have a COW list issue open (#27), but there wasn't enough interest for it, unfortunately. In MapOf, LoadOrCompute()'s read path assumes two atomic loads in the best case. So the end COW map performance might be close to MapOf, but it's certainly worth exploring.

jorgebay commented 1 year ago

I agree that the improvement on the performance side for read-mostly cases is minor.

I think the main benefit is related to the consistent snapshot semantics: iterate a point-in-time structure, e.g., if you have a map with [a, b, c] keys that get mutated into [a, b, d, e] the caller will never get [a, b, d].

puzpuzpuz commented 1 year ago

I think the main benefit is related to the consistent snapshot semantics

Yes, that's not possible with non-COW or single mutex protected data structures.