Watch channel impl #482
Replies: 7 comments 15 replies
-
|
Two possible improvements:
Overall it looks pretty good. |
Beta Was this translation helpful? Give feedback.
-
|
I've implemented the watch channel based on RwLock. I'm concerned there might be some safety flaws and would appreciate it if more experienced developers could review my code. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Some logistical questions:
|
Beta Was this translation helpful? Give feedback.
-
|
I just found out that... uh... |
Beta Was this translation helpful? Give feedback.
-
|
Let's add the crate to compio-rs first as is, and I can open a PR for the unsync variant later. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've put together a demo of a watch channel that works via atomic replacement, aiming for a completely lock-free design. The main limitation, inherent to this Arc-based lock-free approach, is that it can't provide in-place modifications; every update has to be a clone-and-replace, which is why several of the APIs require a T: Clone bound.
The performance results are really interesting. In a single-threaded environment, it doesn't lag too far behind tokio::watch. And if you look at the raw throughput without considering the async machinery, its read performance in read-heavy scenarios is definitely higher than Tokio's RwLock-based version.
But the real-world story is different. Once you measure the end-to-end performance, including the overhead from the waker system, it's consistently about twice as slow as the native Tokio channel. That performance gap gets even wider as the size of the state T grows, because the clone() cost really starts to hurt.
So, my takeaway is that a robust, RwLock-based implementation is probably the more pragmatic path forward. Still, I think this little "toy" was a meaningful experiment. It could be a great baseline to benchmark an RwLock version against in the future, or maybe it could live on as an alternative watch implementation for specific use cases. What do you all think?
Beta Was this translation helpful? Give feedback.
All reactions