rust-lang / rust-lang/rust-clippy

Use escape analysis to catch unnecessary heapification

Open
#393 5 comments 6 reactions 1 assignee View on GitHub

@Manishearth is already working on this.

Since Dec 5, 2015.

A-lint C-an-interesting-project E-hard T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Many languages with a pervasive GCs use "escape analysis" to avoid boxing things. So if an object can't escape a given scope, plop it on the stack instead of giving it to the GC heap. This reduces GC load.

While we're not a GCd language, the same principle can apply to the regular heap (with less optimization wins because we already got that optimization win by not having a GC in the first place)

The idea is to find places where optimizations like this are possible.

Basically, if:

  • A vec/string/box/heap thing is constructed locally (Via Vec::with_capacity, vec![], into_string(), Box::new, etc. Catching vec![] will be somewhat hard)
  • It's not modified (we can perhaps allow in-place modification)
  • It's not passed to a function expecting that heap thing (method expecting things like &[T] which are passed a deref coerced Vec are okay)
  • There is no way for the heap thing to "escape" its scope, i.e. by being returned or otherwise moved out.

Then we should suggest using the unboxed version.

This would be a fun lint to work on, but it's not easy. I might work on this myself but I don't have any time right now. Willing to mentor anyone who does want to.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.