Settings

Theme

The Surprising Cost of Protocol Conformances in Swift

emergetools.com

59 points by sond813 4 years ago · 12 comments

Reader

earthboundkid 4 years ago

I feels like I must be missing something. 3ms is extremely slow. Surely it should be 3us, no? What’s it doing that is so slow?

  • flohofwoe 4 years ago

    Function names like "_dyld_find_protocol_conformance" seem to indicate that DLLs might need to be accessed, and my (entirely subjective) impression is that this general area seems to have become a lot slower in recent macOS versions, at least when system DLLs are involved.

  • sond813OP 4 years ago

    It’s ms, that slow because every protocol conformance in every dylib needs to be checked. When this gets to 100k+ like in many large apps it takes multiple milliseconds

andrekandre 4 years 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?
  • favorited 4 years ago

    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...

    • sond813OP 4 years ago

      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.

olliej 4 years ago

This is about the casting cost specifically, not the direct cost of using a protocol as interface, which itself has some annoying performance characteristics

syspec 4 years ago

Swift has a real performance problem, it doesn't seem competitive with other "systems languages"

Is the goal to make it faster later? What can be done to get closer to that goal?

  • jb1991 4 years ago

    Swift is quite fast as an app programming language. I’ve never understood why anyone could consider it a systems programming language with a straight face. But to say that it has a “real performance problem” is a bit of an exaggeration. Compared to the many other languages out there, it’s faster than most.

    • syspec 4 years ago

      Probably because that is one of the states goals as voiced by the languages creator

      • jb1991 4 years ago

        That was a long time ago, I thought. The word "systems" was removed from the swift website years ago when it was clear that it did not really have that priority.

        • syspec 4 years ago

          > The goal of the Swift project is to create the best available language for uses ranging from systems programming, to mobile and desktop apps, scaling up to cloud services.

          > Fast. Swift is intended as a replacement for C-based languages (C, C++, and Objective-C). As such, Swift must be comparable to those languages in performance for most tasks. Performance must also be predictable and consistent, not just fast in short bursts that require clean-up later. There are lots of languages with novel features — being fast is rare.

          Those are literally the stated goals. It does not get more clear than saying "Swift is intended to be a replacement for C-based languages (C, C++)..."

          [0]https://www.swift.org/about/

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection