Why use future over sync? #284
-
I'm a little new to Rust specifically, but I'm a little confused as to why I'd use the async vs sync version of Moka. Isn't Moka in-memory with all operations being synchronous against the underlying data structures? In my experience, other languages tend to only really resort to async processing when you have to wait on IO or, in some circumstances, are spreading load across multiple threads. Wouldn't using Moka in sync mode always be faster than async, even from within a standard async loop (i.e. from Tokio)? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi. Sorry for late reply. Although async version of Moka cache will be slightly slower than sync cache, we would recommend to use async cache when it is used within async context (i.e. from Tokio). Async cache will never block the executor threads of async runtime, while sync cache may (i.e. when waiting for a lock). Async cache will keep the async runtime responsive. Here are some obvious examples:
It will be rare but other methods, such as About the speed, here is a micro-benchmark result. The async cache was about 9% slower than the sync cache. But cache read and write operation will be fast enough in both sync and async caches. 9% means 75 nanoseconds in the benchmark.
|
Beta Was this translation helpful? Give feedback.
-
Speaking of the blocking behavior under heavy writes, I'm wondering if it would be better if an async-channel is adopted in the async cache. We can call Please let me know how you feel about it. @tatsuya6502 |
Beta Was this translation helpful? Give feedback.
Hi. Sorry for late reply.
Although async version of Moka cache will be slightly slower than sync cache, we would recommend to use async cache when it is used within async context (i.e. from Tokio). Async cache will never block the executor threads of async runtime, while sync cache may (i.e. when waiting for a lock). Async cache will keep the async runtime responsive.
Here are some obvious examples:
get_with
method (doc):get_with
will evaluate the user function to get the value to insert.