[swift-server-dev] Existential - Thoughts on the current HTTP API

Helge Heß me at helgehess.eu
Fri Oct 13 07:07:42 CDT 2017


It is a little off-topic, but since you brought it up in here ;-) Thinking about the existential:

On 13. Oct 2017, at 12:39, Helge Heß via swift-server-dev <swift-server-dev at swift.org> wrote:
>> `UnsafeHTTPResponseBody` is an existential (a protocol-typed variable). That means with this type signature, you'll do an allocation for every call, that's pretty bad.

Is that intended or a compiler bug? My assumption for passing something as a protocol would be that

 writeBody(_ data: UnsafeHTTPResponseBody)
 writeBody(myImp)

Essentially becomes something like

 writeBody(_ dataOwner: Any, _ data: vtable<UnsafeHTTPResponseBody>)
 writeBody(myImp, myImp.vtableForProtocol<UnsafeHTTPResponseBody>)

You are saying that it is more like this?:

 struct existential {
   let dataOwner : Any
   let data      : vtable<UnsafeHTTPResponseBody>
 }
 let e = malloc(existential)
 writeBody(e)
 free(e)

That sounds a little crazy, doesn’t it? :-)


>> A better option would be
>> `func writeBody<Body: UnsafeHTTPResponseBody>(_ data: Body, …)`

But this is only better if the concrete `Body` type lives in the same module like the `writeBody` function, right? (so that WMO optimisation can kick in and inline the instantiation of the generic type?)

In the HTTP module case `Body` would be in a different module and in my understanding would always use some dynamic ‘generics’ dispatcher? Which is slow? Is that really faster than a protocol type?

hh



More information about the swift-server-dev mailing list