[SR-13111] String allocates twice when constructing from non-contiguous collection #55557
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
performance
standard library
Area: Standard library umbrella
Environment
Swift 5.3
Additional Detail from JIRA
md5: dfd669c7f10cade2c786ee7ff78dbb26
Issue Description:
Consider the Swift program below:
This code constructs 100 Strings from ranges. It does a few extra things just to prevent the compiler from optimising that work out.
I'd expect this program to allocate 100 times on String construction (as none of these strings are small). However, in practice, it allocates 200 times:
This is because of the implementation of
String(decoding:as🙂
, which ultimately callsString.fromNonContiguousUnsafeBitcastUTF8Repairing<C>(🙂
. That function looks like this:Note that this constructs an intermediary Array, before dropping into
String._fromUTF8Repairing
.Given that this call is specialised for
Collection
and notSequence
, there seems to be no good reason that this call path cannot be reimplemented on top ofString.init(unsafeUninitializedCapacity:initializingWith🙂
and then simply copying the bytes over that way. This should be a net performance win: we'll halve the allocations of this code path, and we'll also remove an intermediarymemcpy
that we otherwise have to do.The text was updated successfully, but these errors were encountered: