[SR-15712] UBP<UInt8>/URBP.elementsEqual is slow #57990
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
performance
standard library
Area: Standard library umbrella
Environment
Xcode 13.2.1, macOS 11.6
Also tested on Swift version 5.6-dev (LLVM 031406b6cea8006, Swift 9f7d3fc)
Target: x86_64-unknown-linux-gnu
Additional Detail from JIRA
md5: ac54ed7b0a71df80a5ca916293426167
Issue Description:
Consider the following example, demonstrating a IPv4 address type:
This generates quite a lot of code, including calls to runtime functions. This is clearly suboptimal, and is so large it won't even be inlined.
If we replace the calls to elementsEqual with the C library function memcmp, things are much better:
Wow. From all of that to just a simple 'cmp' instruction. What a difference! Makes sense - this is just a 32-bit integer, which we're choosing to store as a 4-element tuple of bytes. This is an enormous win both for code-size and performance.
In general, I think a lot more comparisons between contiguous buffers of elements with no padding should optimise to memcmp. It's a lot faster than the generic specialisation for elementsEqual, and the compiler already knows what memcmp "means". Instead, I'm having to add functions like this to my library:
Godbolt link.
The text was updated successfully, but these errors were encountered: