[stdlib] Dump _StringStorage.grow()

This implementation detail was adding lots of needless complexity
This commit is contained in:
Dave Abrahams
2017-05-31 14:38:09 -07:00
parent fbf5a5162a
commit ce804529a1
3 changed files with 4 additions and 71 deletions

View File

@@ -194,63 +194,6 @@ public struct _StringBuffer {
return cap + offset <= capacity
}
/// Attempt to claim unused capacity in the buffer.
///
/// Operation succeeds if there is sufficient capacity, and either:
/// - the buffer is uniquely-referenced, or
/// - `oldUsedEnd` points to the end of the currently used capacity.
///
/// - parameter bounds: Range of the substring that the caller tries
/// to extend.
/// - parameter newUsedCount: The desired size of the substring.
@inline(__always)
@discardableResult
mutating func grow(
oldBounds bounds: Range<UnsafeRawPointer>, newUsedCount: Int
) -> Bool {
var newUsedCount = newUsedCount
// The substring to be grown could be pointing in the middle of this
// _StringBuffer. Adjust the size so that it covers the imaginary
// substring from the start of the buffer to `oldUsedEnd`.
newUsedCount
+= (bounds.lowerBound - UnsafeRawPointer(start)) &>> elementShift
if _slowPath(newUsedCount > capacity) {
return false
}
let newUsedEnd = start + (newUsedCount &<< elementShift)
if _fastPath(self._storage.isUniquelyReferenced()) {
usedEnd = newUsedEnd
return true
}
// Optimization: even if the buffer is shared, but the substring we are
// trying to grow is located at the end of the buffer, it can be grown in
// place. The operation should be implemented in a thread-safe way,
// though.
//
// if usedEnd == bounds.upperBound {
// usedEnd = newUsedEnd
// return true
// }
// &StringBufferIVars.usedEnd
let usedEndPhysicalPtr = UnsafeMutableRawPointer(_storage._value)
.assumingMemoryBound(to: Optional<UnsafeRawPointer>.self)
// Create a temp var to hold the exchanged `expected` value.
var expected : UnsafeRawPointer? = bounds.upperBound
if _stdlib_atomicCompareExchangeStrongPtr(
object: usedEndPhysicalPtr, expected: &expected,
desired: UnsafeRawPointer(newUsedEnd)) {
return true
}
return false
}
var _anyObject: AnyObject? {
return _storage.storage != nil ? _storage.storage! : nil
}