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?
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 ツ
Yes, this topic was brought up 1.5 years ago in this thread. 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.
Do you have any ideas on how to make something like this?
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.
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?
except, you'd need to convert the pointer to something ASLR stable... ↩︎
I suspect there are devastating security implications to doing this if observability comes from devices you don't control ↩︎
since keypaths are known statically, maybe the compiler could record only the needed strings per module? this seems like a ton of complexity though... ↩︎
If I may take a step back and ask: Isn't it wild that we have this fancy macro system in place that offers so many possibilities - and then we hesitate to use it because of all that swift-syntax build mumbo jumbo that frankly drives me bonkers as well. I build swift-syntax about a thousand times a day - and I am tired, boss.
I guess what I am trying to say is that macros seem like a great solution for this. We should really, really, finally get that macro build system stuff fixed.
There are moves toward accelerating macros, some time ago a proof of concept for running macros via WASM was demonstrated on the forum. Additionally, Xcode has added the ability to use precompiled SwiftSyntax.
However, specifically for my task, burdening users with a SwiftSyntax dependency just for the sake of a sugar API isn’t practical, so I decided to look into alternatives, if any exist.