# KeyPath to String: Efficient runtime conversion alternatives?

**URL:** <https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456>\
**Category:** Using Swift\
**Tags:** performance, keypath, stdlib, reflection, macros\
**Created:** [November 4, 2026, 1:12pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456 "2026-11-04T13:12:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [November 4, 2026, 1:12pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/1 "2026-11-04T13:12:43Z")

</div>

Hi everyone,

I am working on a library where a dictionary-like data structure consumes instance property values using a list of key-paths. To use these key-paths as keys, I need to convert them into strings.

However, I noticed that using `String(reflecting:)` is roughly 295 times slower than assigning values using a hardcoded string subscript. When writing a large number of properties, this overhead accumulates into a noticeable millisecond-range delay.

If the overhead of using key-paths were, say, 5, 10, or even 20 times slower, it would be acceptable. However, a nearly 300-fold performance gap makes this API unacceptable for real-world production use.

Right now, the issue boils down to an unpleasant trade-off: either suffer from poor runtime performance or endure noticeably longer compilation times due to SwiftSyntax dependencies in macros.

Are there any ways to **convert a `KeyPath` to a `String` in a faster way** , other than using a macro that converts the property name into a string at compile-time?

---

<div class="post-metadata">

**Author:** ![ravindu](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ravindu/32/47051_2.png) [@ravindu](https://forums.swift.org/u/ravindu)\
**Post date:** [November 4, 2026, 4:13pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/2 "2026-11-04T16:13:32Z")

</div>

Interesting question. From what I’ve found `KeyPath` doesn’t directly store property names and `String(reflecting:)` is mainly intended for debugging rather than retrieving names for program logic.

I wonder if caching the reflected strings could help when the same key paths are reused frequently. Another possibility might be a lightweight compile-time mapping that avoids bringing in the full SwiftSyntax dependency.

Curious to hear if anyone has explored alternative approaches, especially ones that balance runtime performance and compilation overhead ツ

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [November 4, 2026, 4:36pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/3 "2026-11-04T16:36:17Z")

</div>

> [@ravindu](#):
>
> doesn’t directly store property names

Yes, this topic was brought up 1.5 years ago in [this thread](https://forums.swift.org/t/keypath-string/77732). However, at that time, plans to optimize KeyPath were also being discussed, as well as the possibility of using KeyPath to call functions, and some work may have been done on this topic.

> [@ravindu](#):
>
> Another possibility might be a lightweight compile-time mapping that avoids bringing in the full SwiftSyntax dependency

Do you have any ideas on how to make something like this?

---

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [November 4, 2026, 4:36pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/4 "2026-11-04T16:36:52Z")

</div>

> [@Dmitriy\_Ignatyev](#):
>
> To use these key-paths as keys, I need to convert them into strings.

`AnyKeyPath` is `Hashable` so you can use them as dictionary keys if that is helpful:

```swift
let values: [AnyKeyPath: Int] = [
  \Int.description: 1,
  \String.count: 2,
]

```

Has the added benefit of making properties with the same name on different types be distinct:

```swift
\Int.description == \String.description // false

```

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [November 4, 2026, 6:21pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/5 "2026-11-04T18:21:32Z")

</div>

> [@mbrandonw](#):
>
> `AnyKeyPath` is `Hashable` so you can use them as dictionary keys

Unfortunately it is not suitable, as we need String representation for monitoring / observability.

---

<div class="post-metadata">

**Author:** ![ravindu](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ravindu/32/47051_2.png) [@ravindu](https://forums.swift.org/u/ravindu)\
**Post date:** [November 5, 2026, 3:06am UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/6 "2026-11-05T03:06:51Z")

</div>

I havent explored this deeply yet, but one idea that comes to mind is to associate the property name with the `KeyPath` directly, rather than trying to recover it at runtime.

Maybe a lightweight macro or some form of code generation could produce a small mapping between the property name and its `KeyPath` without requiring the full SwiftSyntax dependency in the main library. Im not sure how practical this would be, especially regarding compilation time but it could be an interesting direction to explore.

---

<div class="post-metadata">

**Author:** ![aviva](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aviva/32/44327_2.png) [@aviva](https://forums.swift.org/u/aviva)\
**Post date:** [November 5, 2026, 6:51pm UTC](https://forums.swift.org/t/keypath-to-string-efficient-runtime-conversion-alternatives/90456/8 "2026-11-05T18:51:51Z")

</div>

Probably tangential: could you make them strings "later"? It should be _possible_ to record the raw bytes\[1\] of the key path and interpret it into a string wherever you do monitoring / observability, if you record the relevant information about your types. This is probably complicated but it seems like the kind of thing a library could handle? The simplest thing would be to reconstruct the keypath wherever you interpret observability data and use reflection\[2\], but it also seems conceptually possible to parse type headers and witness tables out of the binary into a data structure that is optimal for doing this kind of conversion efficiently, without requiring reflection at runtime, although you'd end up storing quite a lot of strings in there, in theory for all the members and types in your program\[3\]?

Edit: this is probably prohibitively hard without compiler support or runtime support in the general case?

* * *

1. except, you'd need to convert the pointer to something ASLR stable... 

2. I suspect there are devastating security implications to doing this if observability comes from devices you don't control 

3. since keypaths are known statically, maybe the compiler could record only the needed strings per module? this seems like a ton of complexity though...
