<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Could you file a bug report? <a href="http://bugs.swift.org" class="">bugs.swift.org</a>?<div class=""><br class=""></div><div class="">Michael</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 9, 2017, at 12:59 AM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">My mistake. If I create a new array I get the problem. EG:<div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="">import Foundation<br class=""><br class="">func elapsed(s: DispatchTime, e: DispatchTime) -&gt; Double {<br class="">&nbsp; &nbsp; return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000<br class="">}<br class=""><br class="">let s = 250_000_000<br class="">var a = [UInt8]()<br class="">a.reserveCapacity(s)<br class=""><br class="">let sa = DispatchTime.now()<br class="">for i in 1 ... s {<br class="">&nbsp; &nbsp; a.append(0)<br class="">}<br class="">let ea = DispatchTime.now()<br class="">print("Append time: \(elapsed(s: sa, e: ea)) s")<br class=""><br class="">let st = DispatchTime.now()<br class="">a = Array(a[0 ..&lt; (s &gt;&gt; 1)])<br class="">let et = DispatchTime.now()<br class="">print("Trim time: \(elapsed(s: st, e: et)) s")<br class="">print("a count: \(a.count), capacity: \(a.capacity)")<br class=""><br class=""><br class=""></blockquote>Prints:<div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="">Append time: 2.65726525 s<br class="">Trim time: 36.954417356 s<br class="">a count: 125000000, capacity: 125001696</blockquote></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature" data-smartmail="gmail_signature">&nbsp; -- Howard.<br class=""></div></div>
<br class=""><div class="gmail_quote">On 9 May 2017 at 17:53, Howard Lovatt <span dir="ltr" class="">&lt;<a href="mailto:howard.lovatt@gmail.com" target="_blank" class="">howard.lovatt@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">I find trimming relative to appending OK on my 6 year old MacBook Pro. EG:<div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="">import Foundation<br class=""><br class="">func elapsed(s: DispatchTime, e: DispatchTime) -&gt; Double {<br class="">&nbsp; &nbsp; return Double(e.uptimeNanoseconds - s.uptimeNanoseconds) / 1_000_000_000<br class="">}<br class=""><br class="">let s = 250_000_000<br class="">var a = [UInt8]()<br class="">a.reserveCapacity(s)<br class=""><br class="">let sa = DispatchTime.now()<br class="">for i in 1 ... s {<br class="">&nbsp; &nbsp; a.append(0)<br class="">}<br class="">let ea = DispatchTime.now()<br class="">print("Append time: \(elapsed(s: sa, e: ea)) s")<br class=""><br class="">let st = DispatchTime.now()<br class="">let ha = a[0 ..&lt; (s &gt;&gt; 1)]<br class="">let et = DispatchTime.now()<br class="">print("Trim time: \(elapsed(s: st, e: et)) s")<br class=""><br class="">print("ha count: \(ha.count), capacity: \(ha.capacity)")<br class=""><br class=""><br class=""></blockquote>Prints:<div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="">Append time: 2.612397389 s<br class="">Trim time: 0.000444132 s<br class="">ha count: 125000000, capacity: 125000000</blockquote></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888" class=""><br clear="all" class=""><div class=""><div class="m_-6131575969107924050gmail_signature" data-smartmail="gmail_signature">&nbsp; -- Howard.<br class=""></div></div></font></span><div class=""><div class="h5">
<br class=""><div class="gmail_quote">On 9 May 2017 at 12:56, Kelvin Ma via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">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 class=""></div><div class="m_-6131575969107924050HOEnZb"><div class="m_-6131575969107924050h5"><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, May 8, 2017 at 7:01 PM, Philippe Hausler via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><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 class="">
<div class="m_-6131575969107924050m_7331902355709494831HOEnZb"><div class="m_-6131575969107924050m_7331902355709494831h5"><br class="">
&gt; On May 8, 2017, at 16:47, Rick Mann via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; 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 class="">
&gt;<br class="">
&gt; 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 class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; self.dataBuffer = Array(self.dataBuffer![0 ..&lt; finalBufferSize])<br class="">
&gt;<br class="">
&gt; 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 class="">
&gt;<br class="">
&gt; I've since worked around this problem, but is there any way to improve on this?<br class="">
&gt;<br class="">
&gt; Thanks,<br class="">
&gt;<br class="">
&gt; --<br class="">
&gt; Rick Mann<br class="">
&gt; <a href="mailto:rmann@latencyzero.com" target="_blank" class="">rmann@latencyzero.com</a><br class="">
&gt;<br class="">
&gt;<br class="">
&gt; ______________________________<wbr class="">_________________<br class="">
&gt; swift-users mailing list<br class="">
&gt; <a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-users</a><br class="">
<br class="">
______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-users</a><br class="">
</div></div></blockquote></div><br class=""></div>
</div></div><br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div></div></div>
</blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>