Async locks are bad #504
mickvandijke
started this conversation in
General
Replies: 1 comment
-
More links |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
After pounding my head for two weeks on trying to benchmark our async function that handles the addition and updates of torrents, and somehow not finding anyone else with the same problem or a solution. I came to the realization that perhaps that function shouldn't be async at all.
Oftentimes we use
tokio::sync::Mutex
&tokio::sync::RwLock
for our lock as these are async compared to the std library's sync locks. And async is faster right? Well apparently, no. The overhead of switching tasks on worker threads when awaiting on an async lock is relatively a lot compared to just blocking the thread and using a sync lock.Ok, that is cool and all, but you can't use sync locks in tokio tasks (which every client connection basically is in our case), so how do we make it work? The Tokio docs have a nice answer for that: https://tokio.rs/tokio/tutorial/shared-state#holding-a-mutexguard-across-an-await
So should we try and use sync locks instead of async locks now as much as possible? Yes I think so. Not only should it be faster, it would also make it easier for us to test and benchmark the synchronous code.
Other useful resources:
Relevant issues:
tracker.torrents
field into smaller pieces #495Tracker.update_torrent_with_peer_and_get_stats
performance using Criterion #496Beta Was this translation helpful? Give feedback.
All reactions