Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adomaskizogian
Copy link

@adomaskizogian adomaskizogian commented Jun 11, 2024

I am proposing to introduce a locking mechanism with RWMutex which helps to optimize read path by allowing multiple reads at once. Perform map initialization using sync.Once.

sync.Map does not have an api for measuring the length of it. Greatly complicates things for eviction implementation. Of course we could use sync/atomic and an int as an external counter but in this case I don't believe this additional complexity is worth it. Theoretically, sync.Map might be a better option

The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

My assumptions might be incorrect, feedback is welcome. Would gladly refactor this to use sync.Map if you see the benefit.

solves #128

@rickar rickar merged commit b70653d into rickar:master Jun 19, 2024
@rickar
Copy link
Owner

rickar commented Jun 19, 2024

I think your current approach is fine and probably less complicated compared to sync.Map with similar benefits. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants