<div dir="ltr">Try:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">struct SegmentedArray {<br> private static let segmentCapacityPowerOf2 = 13<br> private static let segmentCapacity = 1 << SegmentedArray.segmentCapacityPowerOf2<br> private var offset: Int<br> private var firstSegments: [[UInt8]]<br> private var lastSegment: [UInt8]<br> var count: Int {<br> return (firstSegments.count << SegmentedArray.segmentCapacityPowerOf2) + lastSegment.count - offset<br> }<br> var capacity: Int {<br> return (firstSegments.count + 1) << SegmentedArray.segmentCapacityPowerOf2<br> }<br> private init(offset: Int, firstSegments: [[UInt8]], lastSegment: [UInt8]) {<br> (self.offset, self.firstSegments, self.lastSegment) = (offset, firstSegments, lastSegment)<br> }<br> private init(offset: Int) {<br> self.init(offset: offset, firstSegments: [[UInt8]](), lastSegment: [UInt8]())<br> lastSegment.reserveCapacity(SegmentedArray.segmentCapacity)<br> }<br> init() { self.init(offset: 0) }<br> init(sA: SegmentedArray) {<br> (offset, firstSegments, lastSegment) = (sA.offset, sA.firstSegments, sA.lastSegment)<br> }<br> mutating func append(_ x: UInt8) {<br> if lastSegment.count == SegmentedArray.segmentCapacity {<br> firstSegments.append(lastSegment)<br> lastSegment = [UInt8]()<br> lastSegment.reserveCapacity(SegmentedArray.segmentCapacity)<br> }<br> lastSegment.append(x)<br> }<br> subscript(r: Range<Int>) -> SegmentedArray {<br> let l = r.lowerBound + offset<br> let lI = l >> SegmentedArray.segmentCapacityPowerOf2<br> let o = l - (lI << SegmentedArray.segmentCapacityPowerOf2)<br> let u = r.upperBound + offset<br> let uI = u >> SegmentedArray.segmentCapacityPowerOf2<br> let fS = Array(firstSegments[lI ..< uI])<br> let r = 0 ..< (u - (uI << SegmentedArray.segmentCapacityPowerOf2))<br> let lS = Array(uI >= firstSegments.count ? lastSegment[r] : firstSegments[uI][r])<br> return SegmentedArray(offset: o, firstSegments: fS, lastSegment: lS)<br> }<br>}<br></blockquote><br><div>It has fast slicing and the slice returned is a `SegmentedArray` and hence doesn't require wrapping. The append method is a little slower though.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 10 May 2017 at 04:32, Michael Gottesman <span dir="ltr"><<a href="mailto:mgottesman@apple.com" target="_blank">mgottesman@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Could you file a bug report? <a href="http://bugs.swift.org" target="_blank">bugs.swift.org</a>?<span class="HOEnZb"><font color="#888888"><div><br></div><div>Michael</div></font></span><div><div class="h5"><div><br><div><blockquote type="cite"><div>On May 9, 2017, at 12:59 AM, Howard Lovatt via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_1479273195415671914Apple-interchange-newline"><div><div dir="ltr">My mistake. If I create a new array I get the problem. EG:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">import Foundation<br><br>func elapsed(s: DispatchTime, e: DispatchTime) -> Double {<br> return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000<br>}<br><br>let s = 250_000_000<br>var a = [UInt8]()<br>a.reserveCapacity(s)<br><br>let sa = DispatchTime.now()<br>for i in 1 ... s {<br> a.append(0)<br>}<br>let ea = DispatchTime.now()<br>print("Append time: \(elapsed(s: sa, e: ea)) s")<br><br>let st = DispatchTime.now()<br>a = Array(a[0 ..< (s >> 1)])<br>let et = DispatchTime.now()<br>print("Trim time: \(elapsed(s: st, e: et)) s")<br>print("a count: \(a.count), capacity: \(a.capacity)")<br><br><br></blockquote>Prints:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">Append time: 2.65726525 s<br>Trim time: 36.954417356 s<br>a count: 125000000, capacity: 125001696</blockquote></div><div class="gmail_extra"><br clear="all"><div><div class="m_1479273195415671914gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 9 May 2017 at 17:53, Howard Lovatt <span dir="ltr"><<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I find trimming relative to appending OK on my 6 year old MacBook Pro. EG:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">import Foundation<br><br>func elapsed(s: DispatchTime, e: DispatchTime) -> Double {<br> return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000<br>}<br><br>let s = 250_000_000<br>var a = [UInt8]()<br>a.reserveCapacity(s)<br><br>let sa = DispatchTime.now()<br>for i in 1 ... s {<br> a.append(0)<br>}<br>let ea = DispatchTime.now()<br>print("Append time: \(elapsed(s: sa, e: ea)) s")<br><br>let st = DispatchTime.now()<br>let ha = a[0 ..< (s >> 1)]<br>let et = DispatchTime.now()<br>print("Trim time: \(elapsed(s: st, e: et)) s")<br><br>print("ha count: \(ha.count), capacity: \(ha.capacity)")<br><br><br></blockquote>Prints:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">Append time: 2.612397389 s<br>Trim time: 0.000444132 s<br>ha count: 125000000, capacity: 125000000</blockquote></div><div class="gmail_extra"><span class="m_1479273195415671914HOEnZb"><font color="#888888"><br clear="all"><div><div class="m_1479273195415671914m_-6131575969107924050gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div></font></span><div><div class="m_1479273195415671914h5">
<br><div class="gmail_quote">On 9 May 2017 at 12:56, Kelvin Ma via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Depending on what you’re trying to do with the data, you might be better off using an UnsafeBufferPointer and allocating and reallocating that, C-style.<br></div><div class="m_1479273195415671914m_-6131575969107924050HOEnZb"><div class="m_1479273195415671914m_-6131575969107924050h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 8, 2017 at 7:01 PM, Philippe Hausler via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I wonder if Data might be a better tool for the job here since it is it’s own slice type and would avoid copying large amounts of data into temporary buffers.<br>
<div class="m_1479273195415671914m_-6131575969107924050m_7331902355709494831HOEnZb"><div class="m_1479273195415671914m_-6131575969107924050m_7331902355709494831h5"><br>
> On May 8, 2017, at 16:47, Rick Mann via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br>
><br>
> I have this C library that interacts with some hardware over the network that produces a ton of data. It tells me up front the maximum size the data might be so I can allocate a buffer for it, then does a bunch of network requests downloading that data into the buffer, then tells me when it's done and what the final, smaller size is.<br>
><br>
> Thanks to previous discussions on the list, I settled on using a [UInt8] as the buffer, because it let me avoid various .withUnsafePointer{} calls (I need the unsafe buffer pointer to live outside the scope of the closures). Unfortunately, When I go to shrink the buffer to its final size with:<br>
><br>
> self.dataBuffer = Array(self.dataBuffer![0 ..< finalBufferSize])<br>
><br>
> This ends up taking over 2 minutes to complete (on an iPad Pro). finalBufferSize is very large, 240 MB, but I think it's doing a very naive copy.<br>
><br>
> I've since worked around this problem, but is there any way to improve on this?<br>
><br>
> Thanks,<br>
><br>
> --<br>
> Rick Mann<br>
> <a href="mailto:rmann@latencyzero.com" target="_blank">rmann@latencyzero.com</a><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>