<div dir="ltr">I think this proposal follow the same principle of the Tail Call Optimization keyword/attribute proposal. It's a "thing" with the compiler can do alone, but can be "improved" with some user annotation.<br><div><br></div><div>The main question (for me) of this proposal, is: The idea is evaluate the result of any runtime function, for the "next build"? Like Profile Guided Optimization (PGO) do for code paths?<br></div><div><br></div><div>If is: How to handle with functions with call "external modules" functions, there's no way to sure the result of this functions will be consistent; so any technique to detect the need to reevaluate the result may fail. Is this case, the developer need to "manually" clean the cache or similar to recompute the result.<br></div><div><br></div><div>Like said, there alot of "market" to "pre compiled" things, like in games.<br></div><br><div class="gmail_quote"><div dir="ltr">Em seg, 8 de fev de 2016 às 07:39, Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> escreveu:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 8 Feb 2016, at 04:29, Steve Richey via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><div><div style="word-wrap:break-word;font-size:14px;font-family:Calibri,sans-serif"><div><br>
</div>
<div>Example:</div>
<div>```</div>
<div>func myFunction() -> String {</div>
<div><span style="white-space:pre-wrap"></span>return "hello"</div>
<div>}</div>
<div><br>
</div>
<div>let myValue = #run myFunction()</div>
<div>```</div>
<div><br>
</div>
<div>At compile time, `myFunction` is evaluated and the result inlined to the `myValue` definition. At run time, `myValue` is a `String` containing `"hello"`.</div>
<div><br>
</div>
<div>This is useful for tasks that are relatively expensive to run but only need to be done once, such as lookup tables. Running the algorithm to generate those tables can be handled at compile-time, and the results retrieved at no cost at run time. Furthermore,
this structure allows code reuse between the run time and build time code, obviating the need to perform similar tasks in, say, a Swift method and a Python script.</div></div></div></blockquote><br></div></div><div style="word-wrap:break-word"><div>Do we actually need the #run attribute in this case? If the compiler can detect that myFunction() has a fixed return value, or is only ever called once etc., then could it not just optimise away the function call entirely?</div><div><br></div><div>For example:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>func powersOfTwo() -> [UIntMax] { // Return an array of the first 64 powers of two.</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>var steps = (sizeof(UIntMax) * 8)</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>var powers[UIntMax] = [];</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>repeat {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                        </span>steps -= 1</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                        </span>powers.append(UIntMax(1) << steps)</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>} while (steps > 0)</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>return powers</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>}</font></div><div><font face="Monaco"><br></font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>let powersOfTwoList = powersOfTwo()</font></div><div><br></div><div>Since powersOfTwo() is only ever used once, it can be computed into an array constant; the compiler might already do that, I’m not sure. The main difficulty is whether the compiler can detect code that has no predictable return value, e.g- a call that returns the current time, as this would prevent a function from being precomputed into a constant in this way. There may also be cases where a function’s precomputed value could be very large in which case it might be more desirable to compute it only as needed, though if powersOfTwoList were a static value this wouldn’t matter.</div><div><br></div><div>In other words, I think we should be clear on where macros will actually offer functionality that the compiler can’t provide for us, as it seems to me that in the examples given so far we could just use regular code and the compiler can factor it out for us, or we could have an attribute that allows us to indicate which code the compiler should try to factor out. But as a general rule I think if the compiler can factor out unnecessary functions itself, then this could be better in the long run for efficiency anyway, and that’s assuming it doesn’t already do this to some degree (hopefully someone can weigh in on that).</div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div>