6

I have a tokio-based single-threaded async app where using Arcs or other Sync types seems to be an overhead. Because there is no need for synchronization between threads, I am looking for something like tokio::sync::oneshot::channel, Sender and Receiver of which should be !Sync and could be wrapped into Rc instead of Arc.

Are there any specially crafted synchronization primitives for usage in single-threaded async apps in Rust?

Fedorov7890
  • 1,173
  • 13
  • 28
  • 1
    I'm not sure you understand the synchronization tools you're referring to. `Arc` is generally very low overhead - it's just a reference counted smart pointer, the only overhead is the atomic increment/decrement when it's cloned/dropped. Unless you're creating them in a hot loop, it shouldn't affect performance much. One-shot channels use an `Arc` internally, and so it's unlikely you could improve performance by using them instead of `Arc`. – apetranzilla Mar 18 '21 at 14:10
  • @apetranzilla Well, a hot loop is a very likely usage case, and why to pay for atomic operations if I don't need them? – Fedorov7890 Mar 18 '21 at 14:17
  • We can't offer any advice without seeing more of your use case. Do you have some sample code? – apetranzilla Mar 18 '21 at 14:43
  • @apetranzilla Of course. I will make short samples to demonstrate the problem and update the question (this may take some time) – Fedorov7890 Mar 18 '21 at 14:47
  • 1
    @kmdreko Tokio also has [LocalSet](https://tokio-rs.github.io/tokio/doc/tokio/task/struct.LocalSet.html) which I am using successfully. – Fedorov7890 Mar 18 '21 at 19:01

1 Answers1

2

You can take a look at the various Local types in futures-intrusive. E.g. the LocalOneshotChannel requires no mutex.

Matthias247
  • 9,836
  • 1
  • 20
  • 29
  • Thank you, that's a very interesting package written by you! I'm planning to do some testing and comparison later, and update the question to publish the results. – Fedorov7890 Mar 29 '21 at 07:45