Skip to content

Commit 8863a55

Browse files
authored
Merge pull request #21024 from milseman/fass_and_smol
[String] In-register comparison of small ASCII strings
2 parents 4394ce1 + b08d94d commit 8863a55

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

stdlib/public/core/StringComparison.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,31 @@ internal func _stringCompare(
1818
_ lhs: _StringGuts, _ rhs: _StringGuts, expecting: _StringComparisonResult
1919
) -> Bool {
2020
if lhs.rawBits == rhs.rawBits { return expecting == .equal }
21+
return _stringCompareWithSmolCheck(lhs, rhs, expecting: expecting)
22+
}
23+
24+
@usableFromInline
25+
@_effects(readonly)
26+
internal func _stringCompareWithSmolCheck(
27+
_ lhs: _StringGuts, _ rhs: _StringGuts, expecting: _StringComparisonResult
28+
) -> Bool {
29+
// ASCII small-string fast-path:
30+
if lhs.isSmallASCII && rhs.isSmallASCII {
31+
let lhsRaw = lhs.asSmall._storage
32+
let rhsRaw = rhs.asSmall._storage
33+
34+
if lhsRaw.0 != rhsRaw.0 {
35+
return _lexicographicalCompare(
36+
lhsRaw.0.byteSwapped, rhsRaw.0.byteSwapped, expecting: expecting)
37+
}
38+
return _lexicographicalCompare(
39+
lhsRaw.1.byteSwapped, rhsRaw.1.byteSwapped, expecting: expecting)
40+
}
41+
2142
return _stringCompareInternal(lhs, rhs, expecting: expecting)
2243
}
2344

45+
@inline(never) // Keep `_stringCompareWithSmolCheck` fast-path fast
2446
@usableFromInline
2547
@_effects(readonly)
2648
internal func _stringCompareInternal(

stdlib/public/core/StringGuts.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ extension _StringGuts {
9090
@inline(__always) get { return _object.isSmall }
9191
}
9292

93+
internal var isSmallASCII: Bool {
94+
@inline(__always) get { return _object.isSmall && _object.smallIsASCII }
95+
}
96+
9397
@inlinable
9498
internal var asSmall: _SmallString {
9599
@inline(__always) get { return _SmallString(_object) }

0 commit comments

Comments
 (0)