Skip to content

[5.0] String performance and code size improvements #21081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions stdlib/public/core/StringComparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,31 @@ internal func _stringCompare(
_ lhs: _StringGuts, _ rhs: _StringGuts, expecting: _StringComparisonResult
) -> Bool {
if lhs.rawBits == rhs.rawBits { return expecting == .equal }
return _stringCompareWithSmolCheck(lhs, rhs, expecting: expecting)
}

@usableFromInline
@_effects(readonly)
internal func _stringCompareWithSmolCheck(
_ lhs: _StringGuts, _ rhs: _StringGuts, expecting: _StringComparisonResult
) -> Bool {
// ASCII small-string fast-path:
if lhs.isSmallASCII && rhs.isSmallASCII {
let lhsRaw = lhs.asSmall._storage
let rhsRaw = rhs.asSmall._storage

if lhsRaw.0 != rhsRaw.0 {
return _lexicographicalCompare(
lhsRaw.0.byteSwapped, rhsRaw.0.byteSwapped, expecting: expecting)
}
return _lexicographicalCompare(
lhsRaw.1.byteSwapped, rhsRaw.1.byteSwapped, expecting: expecting)
}

return _stringCompareInternal(lhs, rhs, expecting: expecting)
}

@inline(never) // Keep `_stringCompareWithSmolCheck` fast-path fast
@usableFromInline
@_effects(readonly)
internal func _stringCompareInternal(
Expand Down
11 changes: 11 additions & 0 deletions stdlib/public/core/StringCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,16 @@ extension String {
) -> String {
return String._fromCodeUnits(utf16, encoding: UTF16.self, repair: true)!.0
}

@usableFromInline
internal static func _fromSubstring(
_ substring: __shared Substring
) -> String {
if substring._offsetRange == substring._wholeString._offsetRange {
return substring._wholeString
}

return substring._withUTF8 { return String._uncheckedFromUTF8($0) }
}
}

4 changes: 4 additions & 0 deletions stdlib/public/core/StringGuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ extension _StringGuts {
@inline(__always) get { return _object.isSmall }
}

internal var isSmallASCII: Bool {
@inline(__always) get { return _object.isSmall && _object.smallIsASCII }
}

@inlinable
internal var asSmall: _SmallString {
@inline(__always) get { return _SmallString(_object) }
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Substring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension String {
/// - Complexity: O(*n*), where *n* is the length of `substring`.
@inlinable
public init(_ substring: __shared Substring) {
self = substring._withUTF8 { return String._uncheckedFromUTF8($0) }
self = String._fromSubstring(substring)
}
}

Expand Down