Take a look at th following code.
class Test1 {
var item3: StringKey {
StringKey()
}
var item4: StringKey {
StringKey()
}
}
class Test2 {
var item3: StringKey {
StringKey()
}
}
Basically, StringKey needs to quickly differentiate between item3 and item4 within the same class (the key is used to access cache values within that class).
If item3 is deduped across classes it doesn't matter. The pointer value of item3 is still different from that of item4 (they must point to unique strings). And the key values must be unique, since the variable names on the class must also be unique.
The key is used to index into several dictionaries and converting the StaticString pointer to an integer and using that as a key is much, much, much faster than continually creating a heap allocated String from the StaticString, passing the string around (and reference counting it each time), and using the string as an index into the dictionaries (which requires hashing/string comps each time).
I could just do a one-time hash of the value... but hashes aren't guaranteed to be unique.
The benchmarked speed difference is about 400%, which is a pretty dramatic improvement.
Bottom line is that the value of # function must always resolve to the same text value due to the definition of that particular function. It should remain the same value during the lifetime of the app. And that value is a StaticString, whose pointed value must also remain constant for the lifetime of the app, since the pointer is now part of the StaticString, and the lifecycle of that string is unknown to Swift.
Those are inferences, true. But they're inferences based on the very definitions of the functions and types.
The only fly in the ointment is whether or not I could get a different pointer value across multiple invocations of the function... and given the other inferences I can't see why that would occur. Since the pointer must persist, I don't see how it could move, or why it would be reloaded/duplicated elsewhere.
I'll noodle some more, but the performance increase is hard to ignore. I appreciate the thoughts.