<div dir="ltr"><div>You can use a local like this:</div><div> var x: UInt32 = 0<br></div><div> withUnsafePointer(to: x) { randomWordPT in</div><div> //your existing code here</div><div> }</div><div><br></div>Or I'm not sure if small arrays go on the heap, but<div><br><div><div> var bytes: [UInt8] = [0,0,0,0]</div><div> _ = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)</div><div> return bytes.reduce(0) { ($0 << 8) | UInt32($1) }</div><div><br></div><div>reads a little nicer.<br></div></div></div><div><br></div><div>Personally I would be surprised if the malloc caused an actual measurable performance hit tbh.</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, 3 Oct 2016 at 02:15 Jean-Denis Muys via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Hi,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I have some issues using the new raw memory API. For instance, let's suppose I want to call the `<span style="color:rgb(61,29,129);font-family:menlo;font-size:11px" class="gmail_msg">SecRandomCopyBytes</span>` API to generate a cryptographically secure random 32-bit number. The difficulty is its 3rd argument, which is declared as <span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UnsafeMutablePointer</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg"><</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UInt8</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg">></span>. Here is a function that does that:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> entropicRandom() -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UInt32</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> randomWordPT = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UInt32</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">>.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="gmail_msg">allocate</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(capacity: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">1</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"></span><br class="gmail_msg"></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">_</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> = randomWordPT.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="gmail_msg">withMemoryRebound</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(to: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UInt8</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">self</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, capacity: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">4</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">) { (p: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">UInt8</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">>) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">Int32</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">in</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(112,61,170)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg"> result = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="gmail_msg">SecRandomCopyBytes</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">(</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">kSecRandomDefault</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">, </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">MemoryLayout</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg"><</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">UInt32</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">>.</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">size</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg">, p)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> result</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> randomInt32 = randomWordPT[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">0</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">]</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> randomWordPT.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="gmail_msg">deallocate</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(capacity: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">1</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> randomInt32</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></p></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">apparently, the calls to allocate and then deallocate suggest that there is some heap allocation happening behind the scene here, possibly malloc/free. Is that correct?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">If so, this is quite wasteful. Is there a way to use a local variable on the stack to achieve the same result?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Thanks,</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Jean-Denis</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div></div>
_______________________________________________<br class="gmail_msg">
swift-users mailing list<br class="gmail_msg">
<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="gmail_msg">
</blockquote></div>