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

Josh Haberman jhaberman at gmail.com
Fri Jun 20 11:02:54 PDT 2014

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

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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140620/f0d3fd84/attachment.html>

More information about the Rust-dev mailing list