Munksgaard / session-types

MIT License
555 stars 21 forks source link

Unnecessary transmutes #27

Open ebfull opened 9 years ago

ebfull commented 9 years ago

Are the transmutes necessary for performance reasons? Chan could be reconstructing itself as it takes itself by value, and should not need to transmute. I'm reasonably confident LLVM will optimize this away.

laumann commented 9 years ago

The transmutes are not necessary, no. What you mean is that we could do

impl<E, P, A: marker::Send + 'static> Chan<E, Send<A, P>> {
    pub fn send(self, v: A) -> Chan<E, P> {
        unsafe {
            write_chan(&self, v);
        }
        let Chan(tx, rx, _) = self;
        Chan(tx, rx, PhantomData)
    }
}

instead?

We tried it out at some point, and it works just the same - I think we kept the transmutes, because we were not convinced that there was any difference between the two, and thought that the transmute is a "clearer" statement of intention (that we're changing the type).

Munksgaard commented 9 years ago

and thought that the transmute is a "clearer" statement of intention (that we're changing the type).

Perhaps we should consider actually making the types explicit every time we use transmute?

ebfull commented 9 years ago

Well, transmutes should be avoided simply because they require unsafe { }, and wouldn't you want to reduce the amount of unsafe code? :) (Of course, changing the session type with reckless disregard is itself unsafe potentially because it breaks the expectations elsewhere, so you should ensure the session type cannot be changed outside the library at least. This might just lead you back to unsafe to begin with, though.)