Understanding assembly code of rust and c

โš“ rust    ๐Ÿ“… 2025-07-15    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

I was going through some addition (a+b) operation in the code that I wrote and figuring out why Rust was little ยตs faster than the C in my specific implementation.

I have three distinctive assembly code of (a+b) operation.

  • C assembly code for a+b (release build) - this implementation took 1.3ยตs

    image

  • Rust assembly code for a+b (release build) - this implementation took 1.38ยตs

    image

  • Rust assembly code for a.wrapping_add(b) (release build) - this implementation took 1.24ยตs

    image

From the above code, if you see, C and Rust with normal (a+b) have the similar assembly instructions, as they are loading the values from memory and storing the values. While, the wrapping_add function does it through the data registers, hence it is faster a bit compared to C.

What I am interested in knowing from you, which I couldn't find a concrete reason is, as to why this behaviour exists. Please correct me if my analysis of the assembly instruction is wrong here and can you tell me if this behaviour is from hightec llvm rust compiler (this is the one i am using) optimization for such functions or it is a rust language feature by default.

1 post - 1 participant

Read full topic

๐Ÿท๏ธ rust_feed