通过演员选择向所有孩子转发消息是否比遍历子ActorRefs更有效? [英] Is forwarding a message to all children via an actor selection more efficient than iterating over the child ActorRefs?
问题描述
我有一种情况,当演员收到一条特定的消息时,它必须将该消息转发给当时存在的所有孩子。
I have a scenario where when an actor receives a specific message it must forward the message to all of it's children that exist at that time.
我想我有有2个解决方案。
I think I have 2 options to solve this.
getContext().actorSelection("*").forward(message, getContext());
或
getContext().getChildren().forEach(child -> child.forward(message, getContext()));
在没有充分了解演员选择的内部实现的情况下,很难知道哪个会更好。我计划按照我期望的规模进行一些基准测试,但是希望有经验的用户对此有所了解。
Without fully understanding the internal implementation of actor selection it's hard to know which will perform better. I plan to perform some bench marks with the kind of scale I am expecting to require but would appreciate any insight to this that experienced users may have.
谢谢
推荐答案
我没有对它进行基准测试,但是我正在寻找两个选项的实现。两者实际上在性能上应该非常相似,因为它们最终会使用可迭代的actor引用。
I don't benchmark it, but I was looking the implementation of both options. Both should be practically very similar in terms of performance, because they use an iterable of actor references ultimately.
另一方面,您可以检查字符串 *是否为翻译成一个表达式并在actorSelection中转发一个正则表达式,它将使该表达式与actor的子代匹配,然后将消息转发给每个匹配的子代。
On the other hand, you can check that the string "*" is translated into a expression and a forward in an actorSelection will just match that expression with the children of the actor, and then forward the message to each of the matching children.
matchingChildren.foreach(_.tell(sel.msg, sender))
,然后在需要时进行递归操作(例如在 * / *中)。
and do that recursively if needed (for example in "*/*").
因此,我建议使用getChildren以避免解析字符串的重载( *),将其转换为正则表达式,然后过滤匹配的子代。
So I would recommend to use getChildren to avoid the overload of parse the string ("*"), translate it in a regular expression, and filter the matching children.
这篇关于通过演员选择向所有孩子转发消息是否比遍历子ActorRefs更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!