Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make isHolCache access thread-safe #127

Merged
merged 1 commit into from
Jun 19, 2024
Merged

Conversation

adomaskizogian
Copy link
Contributor

@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
2 checks passed
@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