A faithful implementation of the actor message-passing semantics would mean that message contents are deep-copied from a logical point-of-view, even for immutable types. Deep-copying of message contents remains one the biggest bottlenecks for naïve implementations the actor model, so some implementations (开发者_JAVA技巧ie. Kilim) support zero-copy message passing.
My question is, how is zero-copy message-passing (as part of an Actor library/framework) implemented in a shared-memory platform like the JVM? I assume it can only work for messages with immutable content, and that the visibility of message references must be restricted in some way. However, I'm having trouble finding the "theory" behind implementations of the Actor model.
Not that I know how any actual implementation is done, but when immutability is ensured at compile time like this:
class Immutable {
private final String str = "A";
public String getString(){
return str;
}
}
You could simply pass along a reference, right? It's not a Actor library, but Google Guava gives you this idiom return ImmutableList.copyOf(someList);
and it will be a zero copy if someList
is immutable (that is, if it is an instance of ImmutableList). guess a similar approach could be used, e.g. by implementing a marker interface Immutable
and checking for that, and thereby deciding if to copy or not.
Kilim does zero-copy message passing by enforcing a single owner for a Message object and implementing an actor to actor handoff of the Message reference during message passing. At the programmer level the reference literally disappears from one heap and appears on the other, but no Message is allocated or deallocated in the process. Erjang is implemented using Java+Kilim.
Unsafe zero-copy message passing is standard practice in Scala Actors and Akka Actors within the same process. I say it is unsafe because they do not protect you from sharing references to mutable objects between actors. It is the programmer's responsibility to stick to sending immutable messages. In practice this is a totally reasonable trade-off. You just need to be aware of it when coding or reviewer other people's code.
精彩评论