crossbeam icon indicating copy to clipboard operation
crossbeam copied to clipboard

Add disconnected flavor which immediately fails

Open ryoqun opened this issue 2 years ago • 3 comments

Yet another small niceties, i'd like to add to crossbeam_channel.

This doesn't look meaningful by itself at first glance. However, this is useful for conditionally inserting an artificial select arm at predetermined priority in the select_biased!() #1040, combined with never() like this:

select_biased! {
    recv(important_channel) -> urgent_message => {
        ...
    },
    recv(if is_local_task_available { disconnected() } else { never() }) -> _dummy_signal => {
        ...
    },
    recv(normal_channel) -> normal_message => {
        ...
    },
}

Note that using tick() like below introduces needless calls to Instant::now() (which is a vdso basically) per select invocation on top of the needless stack memory consumption. (On the other hand, the zst & const-code-heavy disconnected() will be optimized out to great degree after the macro expansion)..:

fn always() -> Receiver<Instant> {
    tick(Duration::default())
}

Also, unrolling the select is possible, but creates code duplication. Lastly, checking is_local_task_available after select_biased!() alone would introduce a priority inversion.

Thus, i think there's no acceptable alternative solution currently.

ryoqun avatar Dec 08 '23 05:12 ryoqun

Thanks for the PR. I guess this could be replaced by never + disconnect once the API for explicit disconnection is exposed?

taiki-e avatar Dec 21 '23 13:12 taiki-e

Thanks for the PR. I guess this could be replaced by never + disconnect once the API for explicit disconnection is exposed?

hey, really thanks for replying!

yeah, assuming the disconnected never is implemented in a hard-coded way without any state much like the never itself for maximum optimization opportunity by rustc.

note that the tick approach in the pr description or just let disconnected = unbounded().1 would work much like disconnected(). however the two approaches introduce some overhead, which i'd like to avoid entirely.

ryoqun avatar Dec 22 '23 07:12 ryoqun

i think disconnected dummy sender might also be useful. ref: #750

how about this api? let (sender, receiver) = disconnected()? (i.e. align with unbounded() and bounded(...))

ryoqun avatar Jan 04 '24 06:01 ryoqun