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

Faster search with RWMutex #14

Merged
merged 5 commits into from
Aug 28, 2024
Merged

Conversation

alipourhabibi
Copy link
Collaborator

@alipourhabibi alipourhabibi commented Aug 22, 2024

Overview:
This PR is going to increase the performance of searching with introducing another route.
This new routes will use RWMutex with RLock/Unlock for reading.

TODO:

  • Add other routes
  • Add benchmark tests for rwmutex routes
  • Run benchmark tests concurrently
  • Modify grafana dashboard
  • Add hurl tests

Grafana panel now:
grafana

@alipourhabibi alipourhabibi marked this pull request as draft August 22, 2024 15:28
@alipourhabibi alipourhabibi marked this pull request as ready for review August 22, 2024 16:03
@alipourhabibi alipourhabibi changed the title WIP: Faster search Faster search with RWMutex Aug 23, 2024
api/v2/echo.go Outdated
Comment on lines 183 to 186
s.mutex.RLock()
defer s.mutex.RUnlock()

index, ok := s.list.Find(value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A suggestion, We can modify this part with:

s.mutex.RLock()
index, found := s.list.Find(value)
s.mutex.RUnlock()

because this approach could be beneficial as it locks the data only during the find operation
allowing multiple read operations to happen concurrently It ensures thread safety while minimizing the time the lock is held which can improve performance in read heavy scenarios

The problem is that it introduces the potential for errors or bugs if the code between the lock and unlock calls becomes more complex in the future

What do you think? @armineyvazi @alipourhabibi

Copy link
Collaborator

@Iajrdev Iajrdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed the code and have a suggestion that we might consider discussing further.

Handle Unlocking right after finish working with list
Copy link
Collaborator

@Iajrdev Iajrdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good.

@Iajrdev Iajrdev merged commit dbedff3 into PwPJ:main Aug 28, 2024
4 checks passed
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.

3 participants