rust-lang / rust-lang/rust-clippy

Suggest `#[inline]`ing `Iterator::next` on concrete types

Open
#8,523 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Inspired by https://github.com/rust-lang/rust/pull/94776/files#diff-8af5d8e765e2bed4c60f695e2779241af1f313dfecd28f5e9059c30c93fa0311R114

Very often, the performance of iterators depends on Iterator::next getting inlined.

That's usually not a problem, because iterators are often generic, and thus the monomorphization is available to be inlined.

However, non-generic iterators can't cross-crate inline by default, so it would be good to suggest marking them #[inline].

Lint Name

non_inlined_concrete_iterator

Category

perf

Advantage

Allows for the possibility of improved optimizations at the call site.

Drawbacks

All the usual drawbacks of inlining apply, like potentially slower compilation or increased binary size.

This might not be necessary for iterators not exported from their crate.

Example
impl Iterator for EscapeDefault {
    type Item = u8;

    fn next(&mut self) -> Option<u8> {
        self.range.next().map(|i| self.data[i as usize])
    }
}

Could be written as:

impl Iterator for EscapeDefault {
    type Item = u8;

    #[inline]
    fn next(&mut self) -> Option<u8> {
        self.range.next().map(|i| self.data[i as usize])
    }
}

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.

Research direction

Review the proposed non_inlined_concrete_iterator lint and its EscapeDefault example first. Establish how the lint should identify concrete iterator implementations and what diagnostic should recommend; done means the lint's behavior matches the stated example and accounts for the listed inlining drawbacks.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.