[SR-14422] UnsafeRawBufferPointer.count can trap #56778
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
standard library
Area: Standard library umbrella
Attachment: Download
Environment
My machine:
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
macOS 10.15.7 (19H524)
Xcode Version 12.4 (12D4e)
Compiler Explorer:
x86-64 swiftc 5.3 and nightly as of 30.03.2021
Additional Detail from JIRA
md5: 67240c4fcdc42409fa2d67362fdcbcac
Issue Description:
See schemekind_rawbuffer_count.swift (Compiler explorer link), which contains part of a project I'm working on.
Looking at the generated assembly in Compiler Explorer (at -O), we can see that the "doIt" function checks to see whether the String's code-units are contiguous, and if they are, calls WebURL.SchemeKind.init(buffer: UnsafeRawBufferPointer). Here's what that function looks like:
Similarly, we can see a number of calls to WebURL.SchemeKind.init(buffer🙂 in the specialisation for String.UTF8View (i.e. when the code-units are not contiguous)
Now, let's change the function so that, rather than an UnsafeRawBufferPointer, it takes separate UnsafeRawPointer and count (Int) parameters. Attached as schemekind_rawptr_splitcount.swift (Compiler Explorer link)
The trap is now gone. Moreover, the function is now inlined - both in "doIt", and the String.UTF8View specialisation. My benchmarks show that this can have a substantial performance benefit.
The text was updated successfully, but these errors were encountered: