Skip to content

Inefficient compilation of ? operator #88616

Closed
@stepancheg

Description

@stepancheg

Consider these two functions:

pub fn bar(x: Result<String, String>) -> Result<String, String> {
    Ok(x?)
}

pub fn baz(x: Result<String, String>) -> Result<String, String> {
    x
}

Ideally the compiler should generate identical machine code. But it does not.

Compiler explorer

Because of this issue, ? operator is not as convenient to use in high performance parts of code. E. g.

fn f() -> Result<(), MyError> {
  f1()?;
  f2()?;
  Ok(())
}

has to be written for performance as

fn f() -> Result<(), MyError> {
  f1()?;
  f2()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions