rust-lang / rust-lang/rust

Allow inlining of `array::map()`

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

Nobody has claimed this yet.

A-codegen A-LLVM C-optimization E-needs-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

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

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.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.