# \[Pitch\] Distributed Actor Runtime

**URL:** <https://forums.swift.org/t/pitch-distributed-actor-runtime/54045>\
**Category:** Pitches\
**Tags:** distributed\
**Created:** [December 14, 2021, 1:09pm UTC](https://forums.swift.org/t/pitch-distributed-actor-runtime/54045 "2021-12-14T13:09:26Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [December 17, 2021, 1:30pm UTC](https://forums.swift.org/t/pitch-distributed-actor-runtime/54045/4 "2021-12-17T13:30:46Z")

</div>

> [@Alejandro\_Martinez](#):
>
> One thing I'm not clear about yet is how a "local" ActorSystem exposes the IDs of alive actors to other "remote" ActorSystems. Since the register calls are sync I don't see how that can transmit the data across the wire. But then... I guess that's done outside the runtime? as in with a "framework" on top like the Receptionist pattern (which I'm still trying to grasp)?

Right, what you're asking about is effectively "where is ~~service~~ actor discovery implemented?"

Indeed it is not the Swift runtime's job to implement any of that. Though maybe some day as we gain enough confidence we could solidify the Receptionist "pattern" into a specific protocol that actor systems are asked to implement... but realistically: all actor systems implement these very differently.

You'll likely enjoy this talk I did recently: [[Video] Distributed Actors announced at Scale by the Bay](https://forums.swift.org/t/video-distributed-actors-announced-at-scale-by-the-bay/53909) where we announced our distributed actor cluster implementation. In there I mention a little bit about how the receptionist is implemented. If you're really adventurous, you could even read the implementation, over here: [https://github.com/apple/swift-distributed-actors/blob/main/Sources/DistributedActors/Cluster/Reception/OperationLogDistributedReceptionist.swift#L18-L149](https://github.com/apple/swift-distributed-actors/blob/main/Sources/DistributedActors/Cluster/Reception/OperationLogDistributedReceptionist.swift#L18-L149)

* * *

> [@Alejandro\_Martinez](#):
>
> What is fascinating to me is how this system provides a sort of runtime reflection in Swift that reminds me to what we could do in Objective-C (invocations, selectors....). I wonder if this capabilities have utility outside the distributed actor feature and could be used in other parts of the language; being able to implement proxy objects that delegate protocol witnesses or straight method calls to other instances, etc.

Yeah, the mechanism to "register functions and look them up" is general and not tied to distributed actors per se. We currently only surface this feature for `distributed funcs` but the implementation is not tied to actors specifically. It is not a goal of this proposal to make this into a general mechanism, but we're doing out best to not hardcode it to anything actor specific.

> [@Alejandro\_Martinez](#):
>
> On that note, I see the "summon types" is not part of the system, but I'm curious if Swift provides a way of doing that already. I know and agree that since this is for system implementors it can be complicated and is fine, but I was wondering if it would make sense to provide facilities for it. Maybe they could be used also for other parts of the language like summoning dynamic types from Decoding.

Oh, summon can take many forms which is why I skipped over it.

The simplest thing could really be:

```swift
func summonType(id: Int) -> Any.Type {
  [1: Int.self, 2.String.self][id]
}

```

😉 Which _may_ seem silly but is actually very powerful! Thanks to known type registries like that a system is able to survive type _renames_ as well as avoid sending serialized mangled names over the wire for "well known types". This is extensible to user types, so not only Swift types can be send around as IDs. This style does require registering all types you intend to trust in messages though. Which honestly sometimes is a _good_ thing, even if annoying 🙂

The other approach, is what we'll be doing for now in our impls at least... is sending around mangled names, and yes, there is a function though it is _not supported_ to get a type from a mangled name. So you can `_typeByName` a mangled name and get an `Any.Type` back... To actually make use of it statically there are a bunch of other trickery one can do, none are really supported -- which is another reason I did not want to explain them in the proposal.

So this is the two techniques: mangled names (very convenient, not super great for security), or type registration (great for security, performance).

--

Thanks for reading the early proposal and the comments already, cheers!

---

_[View the full topic](https://forums.swift.org/t/pitch-distributed-actor-runtime/54045)._
