Skip to content

Eliminate required type erasure from distributed actors #40033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

DougGregor
Copy link
Member

Eliminate the required use of existentials in distributed actors by introducing the Transport associated type into the DistributedActor protocol and the Identity associated type into the ActorTransport protocol. This way, each distributed actor has a known (concrete) actor transport type and actor identity type, reducing storage requirements to the minimum and eliminating dynamic dispatch when it isn't needed.

Distributed actors can manually specify their Transport associated type or pick up a default by looking for a type named
DefaultActorTransport. A library that vends an actor transport can make create a public typealias DefaultActorTransport referring to its transport, so importing that library and defining a distributed actor will use that library's transport.

Introduce a type-erased AnyActorTransport type to provide an explicitly dynamic actor transport. This is still an important option, e.g., for cases where one wants to be able to dynamically change the transport for testing or different kinds of deployment.

Introduce some potentially-temporary defaults for both the transport and identity, to smooth the transition to this newer formulation. Add a public typ ealias DefaultActorTransport that refers to AnyActorTransport, so that clients will get the same "type-erased by default" behavior they have today (although we'll still end up breaking source code here). Additionally, default the Identity associated type to AnyActorIdentity, which matches today's default. We likely want to eliminate these defaults, because the DefaultActorTransport definition will likely cause ambiguities with client libraries and, philosophically, the performance costs of type erasure should always be opt-in.

…rotocol.

Eliminate the required use of existentials in distributed actors by
introducing the `Transport` associated type into the
`DistributedActor` protocol. Each distributed actor has a known
(concrete) actor transport type, reducing storage requirements and
transport dynamism when it isn't needed.

Distributed actors can manually specify their `Transport` associated
type or pick up a default by looking for a type named
`DefaultActorTransport`. A library that vends an actor transport can
make create a public typealias `DefaultActorTransport` referring to
its transport, so importing that library and defining a distributed
actor will use that library's transport.

Introduce a type-erased `AnyActorTransport` type to provide an
explicitly dynamic actor transport. This is still an important option,
e.g., for cases where one wants to be able to dynamically change the
transport for testing or different kinds of deployment. For now, we
default to this transport in the library (via `DefaultActorTransport`),
but we may very well want to eliminate this because it will be
ambiguous with client libraries that vend their own
`DefaultActorTransport`.
Eliminate the use of the type-erased `AnyActorIdentity` within the
distributed actor protocol by exposing the identity type of an actor
transport as an associated type, `Identity`, which is then used to
refer to all actors that have that transport.

Retain `AnyActorIdentity`, because it remains useful for type-erasing
actor transports, especially as the `Identity` type for
`AnyActorTransport`. For now, make it the default type of `Identity`:
this helps smooth over the transition from use of `AnyActorIdentity`,
but we might want to remove this to make use of the type-erased forms
always opt-in.
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please built toolchain

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@swift-ci
Copy link
Contributor

swift-ci commented Nov 3, 2021

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - 4aacc1c

Install command
tar zxf swift-PR-40033-724-ubuntu16.04.tar.gz
More info

@ktoso ktoso added the distributed Feature → concurrency: distributed actor label Nov 3, 2021
@@ -151,6 +150,7 @@ public protocol ActorIdentity: Sendable, Hashable, Codable {}

@available(SwiftStdlib 5.6, *)
public struct AnyActorIdentity: ActorIdentity, @unchecked Sendable, CustomStringConvertible {
// FIXME: This probably shouldn't be public
public let underlying: Any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weirdly enough, it has to be public or we have to expose it some other way, perhaps some func as(_ type: T) -> T?? The reason is that when we get "some distributed actor" we need to check if we understand the identity quite often (in the cluster I hit this a lot).

Second thought: If we adopted the well-typed Identity i guess this would become unnecessary since we would only work with "our" actors hm... I do keep wondering if it isn't too limiting to erase without being able to pull out the underlying identity though hm...

Summary: Not sure yet 🤔

@swift-ci
Copy link
Contributor

swift-ci commented Nov 4, 2021

macOS Toolchain
Download Toolchain
Git Sha - 4aacc1c

Install command
tar -zxf swift-PR-40033-1208-osx.tar.gz --directory ~/

@DougGregor
Copy link
Member Author

I'm going to go ahead with this as a baby step and then we can tweak it further

@DougGregor DougGregor merged commit b37f77f into swiftlang:main Nov 5, 2021
@DougGregor DougGregor deleted the distributed-actors-without-existentials branch November 5, 2021 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distributed Feature → concurrency: distributed actor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants