# Improving Type Name Computation Performance

**URL:** <https://forums.swift.org/t/improving-type-name-computation-performance/85076>\
**Category:** Standard Library\
**Tags:** performance, type-casting, metatype\
**Created:** [March 1, 2026, 7:46pm UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076 "2026-03-01T19:46:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![k.benua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/k.benua/32/44172_2.png) [@k.benua](https://forums.swift.org/u/k.benua)\
**Post date:** [March 1, 2026, 7:46pm UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/1 "2026-03-01T19:46:41Z")

</div>

# Improving Type Name Computation Performance

## Introduction

The recommended way to obtain a type name from a generic type parameter is:

- `String(describing: type(of:))` — returns the type name without the module prefix

- `String(reflecting: type(of:))` — returns the fully qualified type name, including the module prefix (e.g., `Foundation.Data`)

However, both `String(describing:)` and `String(reflecting:)` carry significant performance overhead that may be unacceptable in performance-sensitive code paths.

* * *

## Deep Dive

### Performance Overhead

Internally, `String(reflecting: type(of: self))` performs multiple protocol conformance checks in [OutputStream.swift](https://github.com/swiftlang/swift/blob/main/stdlib/public/core/OutputStream.swift#L460), testing against:

- `CustomDebugStringConvertible`

- `CustomStringConvertible`

- `TextOutputStreamable`

- `CustomReflectable` (inside the `Mirror` initializer)

The first conformance check for any given `(type, protocol)` pair is expensive because [the runtime must scan all protocol conformance type descriptors](https://github.com/swiftlang/swift/blob/0552f3640c2e01d4faeb1e7b86677419446ac8ce/stdlib/public/runtime/ProtocolConformance.cpp#L1091-L1097). The same overhead applies to `String(describing:)` ([OutputStream.swift](https://github.com/swiftlang/swift/blob/402ad33463ead732fa15ae447a696b3983977642/stdlib/public/core/OutputStream.swift#L409)).

As an application grows and accumulates more protocol conformance descriptors, the cost of the first call to `String(describing:)` or `String(reflecting:)` for any given type increases proportionally.

### How Type Names Are Computed Internally

Despite the overhead above, both `String(reflecting:)` and `String(describing:)` ultimately delegate to the internal [`_typeName(_:qualified:)`](https://github.com/swiftlang/swift/blob/402ad33463ead732fa15ae447a696b3983977642/stdlib/public/core/OutputStream.swift#L308) function:

- `String(reflecting:)` calls it with `qualified: true`

- `String(describing:)` calls it with `qualified: false`

All of the protocol conformance scanning described above is therefore unnecessary overhead when the goal is simply to retrieve a type name.

Full call-stack:

1. [`String(reflecting:)`](https://github.com/swiftlang/swift/blob/bd554ba524968bb4f10ec0016ce8adca85c268b6/stdlib/public/core/Mirror.swift#L773C5-L773C25)

2. [`_debugPrint_unlocked`](https://github.com/swiftlang/swift/blob/bd554ba524968bb4f10ec0016ce8adca85c268b6/stdlib/public/core/OutputStream.swift#L476). Because Any.Type does not conform any of checked protocols.

3. [`_adHocPrint_unlocked`](https://github.com/swiftlang/swift/blob/bd554ba524968bb4f10ec0016ce8adca85c268b6/stdlib/public/core/OutputStream.swift#L389)

4. [`_adHocPrint_unlocked.printTypeName`](https://github.com/swiftlang/swift/blob/bd554ba524968bb4f10ec0016ce8adca85c268b6/stdlib/public/core/OutputStream.swift#L308)

* * *

## Performance Measurements

In our production app, which contains approximately **200,000 protocol conformance descriptors** , the first call to `String(describing:)` or `String(reflecting:)` for a given `Any.Type` takes **6–9 ms on an iPhone 13**.

To isolate and quantify this overhead, I wrote a benchmark running on an M2 Max with 32 GB RAM.

### First Call (nearly empty binary)

| Method | Duration |
| --- | --- |
| `String(describing:)` / `String(reflecting:)` | 0.6 – 1.1 ms |
| `_typeName(_:qualified:)` | 0.0085 – 0.03 ms |

### Subsequent Calls for the Same Type

| Method | Standard types (`Int`, `Double`, non-generic) | Generic types |
| --- | --- | --- |
| `String(describing:)` / `String(reflecting:)` | 600 – 690 ns | 600 – 690 ns |
| `_typeName(_:qualified: false)` | 10 – 12 ns | 40 – 50 ns |
| `_typeName(_:qualified: true)` | 10 – 12 ns | 40 – 50 ns |

### Summary

**First call** (nearly empty binary):

- `String(reflecting:)` is **40–100×** slower than `_typeName(_:qualified: true)`

- `String(describing:)` is **40–130×** slower than `_typeName(_:qualified: false)`

**Subsequent calls** for the same type:

- `String(reflecting:)` is **13–50×** slower than `_typeName(_:qualified: true)`

- `String(describing:)` is **14–60×** slower than `_typeName(_:qualified: false)`

* * *

## Current Workaround and Its Limitations

In our codebase, we rely heavily on `_typeName(_:qualified:)` to avoid this overhead. Unfortunately, the leading underscore indicates that this function is officially **unstable and unsupported** , meaning we cannot depend on it in production code long-term.

This is the motivation for proposing a proper public API solution.

* * *

## Proposed Solutions

### Option 1: Add `Any.Type` Overloads for `String(reflecting:)` and `String(describing:)`

Introduce overloads that accept an `Any.Type` argument and delegate directly to `_typeName(_:qualified:)`, bypassing all unnecessary protocol conformance checks.

```swift

public init(reflecting subject: Any.Type) {

self = _typeName(subject, qualified: true)

}

public init(describing subject: Any.Type) {

self = _typeName(subject, qualified: false)

}

```

**Precedent:** Similar type-specific overloads already exist, such as [`DefaultStringInterpolation.appendInterpolation`](https://github.com/swiftlang/swift/blob/main/stdlib/public/core/StringInterpolation.swift#L188).

**Pros:**

- ✅ No call-site changes required. Existing code using `String(describing: type(of: x))` or `String(reflecting: type(of: x))` will automatically resolve to the new, faster overload after upgrading to a stdlib that includes it.

- ✅ Full performance benefit with no manual migration effort.

**Cons:**

- ⚠ Introduces new overloads, which could theoretically affect overload resolution in edge cases.

* * *

### Option 2: Introduce a Public `typeName(_:qualified:)` Function

Expose `_typeName(_:qualified:)` as a supported public API by removing the underscore prefix.

**Pros:**

- ✅ A dedicated, clearly named function is more semantically precise than repurposing `String(describing:)` or `String(reflecting:)` for type name lookup.

- ✅ Full performance benefit.

**Cons:**

- ❌ Requires updating all existing call sites manually.

- ❌ Adds a new top-level function to the standard library.

* * *

### Option 3: Optimize Swift Runtime Casting to Short-Circuit for Metatypes

Modify the Swift runtime so that when the argument to `String(describing:)` or `String(reflecting:)` is a metatype, the protocol conformance scanning is skipped entirely.

I am currently exploring the Swift runtime source to understand whether this is feasible, but I do not yet have a concrete implementation approach.

**Pros:**

- ✅ No new public API surface.

**Cons:**

- ❌ Likely complex to implement correctly.

- ❌ Even if implemented, some overhead from protocol casting and `Mirror` initialization would remain without also addressing the `String` API layer.

* * *

## My opinion

**Option 1** is the most practical path forward. It delivers the full performance improvement with zero migration cost for existing code, and the pattern of providing type-specialized overloads is already established in the standard library.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 2, 2026, 1:26am UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/2 "2026-03-02T01:26:13Z")

</div>

Overloads degrade type checking performance and can cause source breakage due to hard-to-predict changes in how expressions resolve. If I was going to look into this, I would focus on option 3. There's quite a bit of low-hanging fruit in how we handle printing; aside from providing short circuits for commonly-printed types, we should arguably also cache the results of the conformance lookup so we know how to print the same type again without redoing the lookup every time.

It's also worth taking a step back to ask why you're rendering type names in a hot loop, and what you might do differently in your client code. In particular, people commonly complain about type printing performance because they're trying to use the type names as a `Hashable` key for dictionaries or sets, but printing is almost always the wrong thing to use for this; `ObjectIdentifier` is also `Hashable` and is very cheap to construct from a type reference (since it just uses the type metadata pointer value).

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [March 2, 2026, 4:48am UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/3 "2026-03-02T04:48:32Z")

</div>

On that note, is there a reason why Metatypes aren’t Hashable? I’ve had to do the `storage[ObjectIdentifier(T.self)]` thing many times to implement per-type storage behind a generic interface. Why not just `storage[T.self]`?

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [March 2, 2026, 4:52am UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/4 "2026-03-02T04:52:32Z")

</div>

Regarding the first option, if it’s valid to implement an overload for `Any.Type` that skips protocol conformance checks, that approach should be valid for _all_ types, since `Any.Type` is a supertype of all Metatype instances. I would expect `String(describing: Foo.self)` and `String(describing: Foo.self as Any.Type)` to behave identically. But my understanding is that the optimization of solution 1 may cause them to differ in subtle and unsurprising ways.

---

<div class="post-metadata">

**Author:** ![k.benua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/k.benua/32/44172_2.png) [@k.benua](https://forums.swift.org/u/k.benua)\
**Post date:** [March 2, 2026, 8:11am UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/5 "2026-03-02T08:11:58Z")

</div>

> we should arguably also cache the results of the conformance lookup

All conformance lookups are being cached already in [ProtocolConformance.cpp](https://github.com/swiftlang/swift/blob/0552f3640c2e01d4faeb1e7b86677419446ac8ce/stdlib/public/runtime/ProtocolConformance.cpp#L1040-L1044).

Moreover, there is caching for type-names in [Casting.cpp](https://github.com/swiftlang/swift/blob/4c1e2c55ccc8fe405ae6e8043821f62bd50f1ab0/stdlib/public/runtime/Casting.cpp#L150-L167).

But major issue with `String(describing: type(of:))` is about first call for any meta-type (in our app from 6 to 9 ms). Consequent calls performance is basically optional information illustrating that not only first calls have performance struggles.

> It's also worth taking a step back to ask why you're rendering type names in a hot loop, and what you might do differently in your client code. In particular, people commonly complain about type printing performance because they're trying to use the type names as a `Hashable` key for dictionaries or sets, but printing is almost always the wrong thing to use for this; `ObjectIdentifier` is also `Hashable` and is very cheap to construct from a type reference (since it just uses the type metadata pointer value).

You're right, using class name as key for hashing is not optimal. And there are plenty of other cases where `ObjectIdentifier(Any.Type)` init can help us with. But sometimes type name is needed.

For example, in generic functions:

- Generic helper function for registering and dequeuing `UICollectionViewCell`s and `UITableViewCell`s
- Generic helper function for inserting new objects to CoreData (using `NSEntityDescription.insertNewObject`)
- All kinds of performance/error tracking and logging when you need some human-readable name for identifying what classes are involved

> Overloads degrade type checking performance and can cause source breakage due to hard-to-predict changes in how expressions resolve

For sure, introducing many overloads can impact type checking performance. I don't think there are many usages of `String(describing:)` and `String(reflecting:)` in codebases so it can significantly degrade type checking performance. And I can't see why `String(describing/reflecting:)` can't have such overload while `DefaultStringInterpolation` has.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [March 2, 2026, 6:08pm UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/6 "2026-03-02T18:08:04Z")

</div>

> [@ksluder](#):
>
> On that note, is there a reason why Metatypes aren’t Hashable?

In Swift, metatypes do not conform to _any_ protocols.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [March 2, 2026, 6:09pm UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/7 "2026-03-02T18:09:51Z")

</div>

Option #3 has the least potential ABI-/source-breaking impact, however it won't help developers with back-deployment needs. It may make sense to try a hybrid approach where a back-deployable implementation of #1 is provided, or we make `String.init(describing:)` `@_alwaysEmitIntoClient` and add the appropriate check there (before calling into the existing ABI-stable initializer).

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 2, 2026, 6:52pm UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/8 "2026-03-02T18:52:40Z")

</div>

> [@grynspan](#):
>
> Option #3 has the least potential ABI-/source-breaking impact, however it won't help developers with back-deployment needs. It may make sense to try a hybrid approach where a back-deployable implementation of #1 is provided, or we make `String.init(describing:)` `@_alwaysEmitIntoClient` and add the appropriate check there (before calling into the existing ABI-stable initializer).

Backward deployment is a fair question. A faster implementation of runtime printing seems like it could be implemented largely separate from the standard library as is, since the necessary base primitives of "look up a protocol conformance" and "print a string" exist in the standard library as is. So maybe the better implementation could be shipped as a static library linked into back-deployed binaries. That might also make it easier to build that better implementation to begin with.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [March 2, 2026, 7:11pm UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/9 "2026-03-02T19:11:31Z")

</div>

> [@Joe\_Groff](#):
>
> So maybe the better implementation could be shipped as a static library linked into back-deployed binaries. That might also make it easier to build that better implementation to begin with.

That probably isn't necessary from a technical perspective if we can just do:

```swift
@_alwaysEmitIntoClient
public init(describing value: Any) {
  if let value = value as? Any.Type {
    self = _typeName(value, qualified: false)
    return
  }
  self.init(describingAny: value)
}

@abi(init(describing value: Any))
@usableFromInline init(describingAny value: Any) {
  // existing codepath
}

```

---

<div class="post-metadata">

**Author:** ![j-f1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/j-f1/32/12287_2.png) [@j-f1](https://forums.swift.org/u/j-f1)\
**Post date:** [March 3, 2026, 6:21am UTC](https://forums.swift.org/t/improving-type-name-computation-performance/85076/10 "2026-03-03T06:21:32Z")

</div>

That might also help with the overhead of doing this check, since in many cases I imagine the compiler would be able to statically know the value is not a metatype and therefore elide the call to `_typeName` entirely.
