[string] Fast-path for small string comparison

Promote small-string to small-string comparison into the fast path for
equality and less-than.

Small ASCII strings that are not binary equal do not compare equal,
allowing us to early exit. Small ASCII strings otherwise compare
lexicographically, which we can call prior to jumping through a few
intermediaries.
This commit is contained in:
Michael Ilseman
2018-05-23 15:13:41 -07:00
parent 614016fecd
commit ebdd5e6d98
2 changed files with 24 additions and 18 deletions

View File

@@ -120,25 +120,19 @@ func _compareUnicode(
// TODO: coalesce many of these into a protocol to simplify the code
extension _SmallUTF8String {
@inlinable
func _compare(_ other: _SmallUTF8String) -> _Ordering {
#if arch(i386) || arch(arm)
_conditionallyUnreachable()
#else
if _fastPath(self.isASCII && other.isASCII) {
// TODO: fast in-register comparison
return self.withUnmanagedASCII { selfView in
return other.withUnmanagedASCII { otherView in
return _Ordering(signedNotation: selfView.compareASCII(to: otherView))
}
}
}
// TODO: fast in-register comparison
return self.withUnmanagedUTF16 { selfView in
return other.withUnmanagedUTF16 { otherView in
return selfView._compare(otherView)
}
// TODO: Ensure normality when adding UTF-8 support
_sanityCheck(self.isASCII && other.isASCII, "Need to ensure normality")
if self._storage == other._storage { return .equal }
for i in 0..<Swift.min(self.count, other.count) {
if self[i] < other[i] { return .less }
if self[i] > other[i] { return .greater }
}
return self.count < other.count ? .less : .greater
#endif // 64-bit
}
func _compare(_contiguous other: _StringGuts) -> _Ordering {