Allow inlining of `array::map()`

Open Beginner friendly
#155,667 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Quiet
Tech stack
rust
Domain
performance

Research direction

Locate the standard-library definitions of array::map() and array::try_map(), then inspect how their current implementations handle inlining. Add the requested inlining attributes to both methods and verify that the relevant standard-library checks pass and the methods can inline in performance-sensitive use cases.

Written by the indexing model from the issue text.

Description

A-codegen A-LLVM C-optimization E-needs-mcve T-compiler

Right now array::map() and array::try_map() do not allow inlining, which makes it not possible to use these convenience methods in performance-sensitive cases and requires writing much more verbose loops.

For example, this doesn't inline:

let lut = rs1.to_le_bytes();
let result = rs2.to_le_bytes().map(|idx| {
    *lut.get(usize::from(idx)).unwrap_or(&0)
});

But this does:

let lut = rs1.to_le_bytes();
let mut result = [0; _];
for (&idx, r) in rs2.to_le_bytes().iter().zip(&mut result) {
    *r = *lut.get(usize::from(idx)).unwrap_or(&0);
}

But the first option is much more natural.

Adding #[inline] on both array::map() and array::try_map() would really help here.

Dominant language
Rust
Stars
119k
Forks
16.2k
Avg merge
2d 11h
Merged PRs (30d)
510

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.

More from rust-lang/rust

All issues in rust-lang/rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.