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

Optimize memory cache's singleflight performance #110

Open
2547409617 opened this issue Mar 5, 2024 · 1 comment
Open

Optimize memory cache's singleflight performance #110

2547409617 opened this issue Mar 5, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@2547409617
Copy link

What is the problem your feature solves, or the need it fulfills?

Acquiring write locks is not that easy in a high-concurrency environment,
imagine there are many read lock requests, the write lock will be acquired later.
After write lock acquired perhaps another one has already completed the lookup and removed the lock from locker container.

Describe the solution you'd like

After write lock acquired, we should first check whether data already exists at the current point.

Describe alternatives you've considered

What other solutions, features, or workarounds have you considered that might also solve the issue?
What are the tradeoffs for these alternatives compared to what you're proposing?

Additional context

This could include references to documentation or papers, prior art, screenshots, or benchmark
results.

@2547409617
Copy link
Author

suppose there are two threads(coroutine) t1 t2
t1 get data -> None
t1 get container read lock, get CacheLock -> None
t1 get container write lock, insert CacheLock
t1 lookup data
t2 get data -> None
t2 pause for some reason such as thread switching
t1 put lookuped data
t1 get container write lock, remove CacheLock
t2 resume and get container read lock, get CacheLock -> None
t2 get container write lock, insert another CacheLock
t2 lookup data

So it is waste that t2 lookup data in this situation.
Double check should be done when get write lock like thread-safe lazy initialization.

@eaufavor eaufavor added the enhancement New feature or request label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants