Hacker News new | ask | show | jobs
by andrekandre 1656 days ago

  > The concept behind zconform is to eagerly load all possible protocol conformances and store them in a map keyed by the protocol’s address in memory.
if it works well in most scenarios, couldn't this just be implemented directly in the swift runtime at some point?
1 comments

Maybe, but Swift is intentionally lazy in lots of ways (globals aren't initialized until the first access, for example).

The trade-off of zconform's approach is that dynamic linking is slowed down by needing to eagerly identify all protocol conformances whenever an image is loaded (including conformances which are never even cast). It does make the performance of dynamic casting more deterministic, but it would slow down (for example) app launch by eagerly pre-caching things which might never be queried.

I'd expect that the response from the Swift team would be to avoid dynamic casting in performance-critical code, rather than pessimize link-time.

[Edit, to clarify why the cache needs to be rebuilt when libraries are loaded]: Swift protocols have a feature called retroactive conformance, which allows apps and libraries to add protocol conformance to types they don't own. So, any library that gets loaded could add a protocol to a type. It's a really powerful feature, but has some unfortunate side-effects...

Building up the cache at launch/dynamic link time does have a performance hit of a few ms, but if you know it will save you many runtime checks it can be worth it. Definitely makes sense for the runtime to have the lazy behavior built in. Hopefully the dyld cache will help get the best of both solutions by persisting a cache across multiple launches.