Skip to content

[stdlib] Some cleanup enabled by _alwaysEmitIntoClient. #23026

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 1 commit into from
Mar 4, 2019
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
13 changes: 3 additions & 10 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ extension Character {
_internalInvariant(_str.count == 1)
_internalInvariant(_str._guts.isFastUTF8)

// TODO(@eject): Switch to a helper property on StringObject/StringGuts.
_internalInvariant(
_str._guts.isSmall || _str._guts._object._countAndFlags.isTailAllocated)
_internalInvariant(_str._guts._object.isPreferredRepresentation)
}
#endif // INTERNAL_CHECKS_ENABLED
}
Expand Down Expand Up @@ -178,16 +176,11 @@ extension Character :
_debugPrecondition(s.index(after: s.startIndex) == s.endIndex,
"Can't form a Character from a String containing more than one extended grapheme cluster")

// TODO(@eject): Switch to a helper property on StringObject/StringGuts.
if _fastPath(
s._guts.isSmall || s._guts._object._countAndFlags.isTailAllocated
) {
if _fastPath(s._guts._object.isPreferredRepresentation) {
self.init(unchecked: s)
return
}

// TODO(@eject): Outline this
self.init(unchecked: s._withUTF8 { String._uncheckedFromUTF8($0) })
self.init(unchecked: String._copying(s))
}
}

Expand Down
10 changes: 2 additions & 8 deletions stdlib/public/core/ContiguouslyStored.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ extension String: _HasContiguousBytes {
try body($0)
}
}

return try ContiguousArray(self.utf8).withUnsafeBufferPointer {
try body($0)
}
return try String._copying(self)._guts.withFastUTF8 { try body($0) }
}

@inlinable @inline(__always)
Expand All @@ -116,10 +113,7 @@ extension Substring: _HasContiguousBytes {
return try body($0)
}
}

return try ContiguousArray(self.utf8).withUnsafeBufferPointer {
try body($0)
}
return try String._copying(self)._guts.withFastUTF8 { try body($0) }
}

@inlinable @inline(__always)
Expand Down
20 changes: 19 additions & 1 deletion stdlib/public/core/StringCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,25 @@ extension String {
return substring._wholeString
}

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

@_alwaysEmitIntoClient
@inline(never) // slow-path
internal static func _copying(_ str: String) -> String {
return String._copying(str[...])
}
@_alwaysEmitIntoClient
@inline(never) // slow-path
internal static func _copying(_ str: Substring) -> String {
if _fastPath(str._wholeGuts.isFastUTF8) {
return str._wholeGuts.withFastUTF8(range: str._offsetRange) {
String._uncheckedFromUTF8($0)
}
}
return Array(str.utf8).withUnsafeBufferPointer {
String._uncheckedFromUTF8($0)
}
}
}

9 changes: 8 additions & 1 deletion stdlib/public/core/StringObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,15 @@ extension _StringObject {
_internalInvariant(isLarge)
return (discriminatedObjectRawBits & 0x4000_0000_0000_0000) != 0
}
}

// Whether this string is in one of our fastest representations:
// small or tail-allocated (i.e. mortal/immortal native)
@_alwaysEmitIntoClient
@inline(__always)
internal var isPreferredRepresentation: Bool {
return _fastPath(isSmall || _countAndFlags.isTailAllocated)
}
}

/*

Expand Down
4 changes: 4 additions & 0 deletions stdlib/public/core/StringStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ extension __StringStorage {
_internalInvariant(rawSelf + Int(_StringObject.nativeBias) == rawStart)
_internalInvariant(self._realCapacity > self.count, "no room for nul-terminator")
_internalInvariant(self.terminator.pointee == 0, "not nul terminated")
let str = asString
_internalInvariant(str._guts._object.isPreferredRepresentation)

_countAndFlags._invariantCheck()
if isASCII {
Expand Down Expand Up @@ -803,6 +805,8 @@ extension __SharedStringStorage {
_countAndFlags._invariantCheck()
_internalInvariant(!_countAndFlags.isNativelyStored)
_internalInvariant(!_countAndFlags.isTailAllocated)
let str = asString
_internalInvariant(!str._guts._object.isPreferredRepresentation)
}
#endif // INTERNAL_CHECKS_ENABLED
}