Skip to content

Performance issues: Two examples of Rust programs significantly slower than their C++ versions #141476

Open
@hjyamauchi

Description

@hjyamauchi

I thought it might be useful to report here as I encountered two examples where the Rust versions were much slower than the C++ counterparts compiled with Clang. I ported them for my learning.

ary3

ary3.rs is about 4 times slower than ary3.cpp.

Based on the generated code, it seems that the difference is caused by a missed loop vectorization in the Rust version (details).

matrix

matrix.rs is about 2 times slower than matrix.cpp

I think that the difference is caused by the following (details):

  • the mmult function isn't inlined into the main function (so the constant size of the vectors aren't constant propagated to mmult).
  • the bounds checks are not elided.
  • loop unrolling has not been applied.

In both cases, the differences are in the frontend (rustc and clang) output, as opposed to the LLVM backend.

This is with Rustc 1.86.0 (05f9846f8 2025-03-31) compiling with -O on my x86_64 pc. The Clang version I used was Clang 20.1.5 compiling with -O2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions