Skip to content

[stdlib] Use ContiguousArray internally in Sequence dropLast, prefix, suffix #21658

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
Jan 8, 2019
Merged
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
31 changes: 14 additions & 17 deletions stdlib/public/core/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,9 @@ extension Sequence {

// FIXME: <rdar://problem/21885650> Create reusable RingBuffer<T>
// Put incoming elements into a ring buffer to save space. Once all
// elements are consumed, reorder the ring buffer into an `Array`
// and return it. This saves memory for sequences particularly longer
// than `maxLength`.
var ringBuffer: [Element] = []
// elements are consumed, reorder the ring buffer into a copy and return it.
// This saves memory for sequences particularly longer than `maxLength`.
var ringBuffer = ContiguousArray<Element>()
ringBuffer.reserveCapacity(Swift.min(maxLength, underestimatedCount))

var i = 0
Expand All @@ -906,19 +905,18 @@ extension Sequence {
ringBuffer.append(element)
} else {
ringBuffer[i] = element
i += 1
i %= maxLength
i = (i + 1) % maxLength
}
}

if i != ringBuffer.startIndex {
var rotated: [Element] = []
var rotated = ContiguousArray<Element>()
rotated.reserveCapacity(ringBuffer.count)
rotated += ringBuffer[i..<ringBuffer.endIndex]
rotated += ringBuffer[0..<i]
return rotated
} else {
return ringBuffer
return Array(rotated)
} else {
return Array(ringBuffer)
}
}

Expand Down Expand Up @@ -976,8 +974,8 @@ extension Sequence {
// holding tank into the result, an `Array`. This saves
// `k` * sizeof(Element) of memory, because slices keep the entire
// memory of an `Array` alive.
var result: [Element] = []
var ringBuffer: [Element] = []
var result = ContiguousArray<Element>()
var ringBuffer = ContiguousArray<Element>()
var i = ringBuffer.startIndex

for element in self {
Expand All @@ -986,11 +984,10 @@ extension Sequence {
} else {
result.append(ringBuffer[i])
ringBuffer[i] = element
i += 1
i %= k
i = (i + 1) % k
}
}
return result
return Array(result)
}

/// Returns a sequence by skipping the initial, consecutive elements that
Expand Down Expand Up @@ -1072,15 +1069,15 @@ extension Sequence {
public __consuming func prefix(
while predicate: (Element) throws -> Bool
) rethrows -> [Element] {
var result: [Element] = []
var result = ContiguousArray<Element>()

for element in self {
guard try predicate(element) else {
break
}
result.append(element)
}
return result
return Array(result)
}
}

Expand Down