[rust-dev] optimizing away const/pure function calls?

Daniel Micay danielmicay at gmail.com
Fri Jun 20 11:14:13 PDT 2014

On 20/06/14 02:02 PM, Josh Haberman wrote:
> Does Rust have any way of optimizing away repeated calls to the same
> function where possible? Like GCC's "pure" function attribute?
> To get a little more crazy, say you're working with a Map. Sometimes
> it's convenient to write code like this:
>   if (map.contains_key(foo)) {
>     let val = map.get(foo);
>     // ...
>   }
> This code, naively compiled, would perform two lookups. But only one is
> logically required, and caching the lookup would only require a single
> pointer.
> Is there any reasonable scenario under which the compiler could decide
> to allocate stack space to cache that lookup, so that the code above
> would be optimized to only perform one lookup?
> Josh

Rust has no way to mark effects. LLVM is able to infer the readonly,
readnone and nounwind attributes in some cases, but not most.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140620/0bcee066/attachment.sig>

More information about the Rust-dev mailing list