JVM 上的零复制消息传递
角色消息传递语义的忠实实现意味着,即使对于不可变类型,消息内容也会从逻辑角度进行深度复制。消息内容的深度复制仍然是参与者模型的简单实现的最大瓶颈之一,因此某些实现(即 Kilim)支持零复制消息传递。
我的问题是,零拷贝消息传递如何(作为 Actor 的一部分库/框架)在像 JVM 这样的共享内存平台中实现?我认为它只能适用于具有不可变内容的消息,并且消息引用的可见性必须以某种方式受到限制。然而,我很难找到 Actor 模型实现背后的“理论”。
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 (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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并不是说我知道实际的实现是如何完成的,而是当在编译时确保不变性时,如下所示:
您可以简单地传递引用,对吧?它不是一个 Actor 库,但 Google Guava 给了你这个习惯用法
return ImmutableList.copyOf(someList);
并且如果someList
是不可变的(也就是说,如果它是 ImmutableList 的实例)。猜测可以使用类似的方法,例如通过实现标记接口Immutable
并检查它,从而决定是否复制。Not that I know how any actual implementation is done, but when immutability is ensured at compile time like this:
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 ifsomeList
is immutable (that is, if it is an instance of ImmutableList). guess a similar approach could be used, e.g. by implementing a marker interfaceImmutable
and checking for that, and thereby deciding if to copy or not.Kilim 通过强制 Message 对象具有单一所有者并在消息传递期间实现 Message 引用的执行者到执行者切换来实现零复制消息传递。在程序员级别,引用实际上从一个堆中消失并出现在另一堆上,但在此过程中没有分配或释放消息。 Erjang是使用Java+Kilim实现的。
不安全的零拷贝消息传递是同一进程中 Scala Actor 和 Akka Actor 的标准做法。我说这是不安全的,因为它们不能保护您免于在参与者之间共享对可变对象的引用。程序员有责任坚持发送不可变的消息。实际上,这是一个完全合理的权衡。您只需要在编码或审查其他人的代码时意识到这一点即可。
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.