Hacker News new | past | comments | ask | show | jobs | submit login

In theory, yes, some Rust code can be faster than the C or C++ code people would write, especially as pertains to the guarantees offered by its aliasing control.

In practice, LLVM hasn’t had any incentive to implement those sorts of optimisations at this stage (because before Rust came along nothing would benefit from them), so the benefits are generally theoretical only.

It remains to be seen what this will effect.




LLVM does implement these sorts of optimizations:

C:

  int foo(int *x, int *y) {
    *x = 0;
    *y = 1;
    return *x;
  }
gives:

  foo(int*, int*): # @foo(int*, int*)
    mov dword ptr [rdi], 0
    mov dword ptr [rsi], 1
    mov eax, dword ptr [rdi]
    ret
where as in Rust:

  fn foo(x: &mut i32, y: &mut i32) -> i32 {
    *x = 0;
    *y = 1;
    *x
  }
which compiles to:

  example::foo:
    mov dword ptr [rdi], 0
    mov dword ptr [rsi], 1
    xor eax, eax
    ret


That optimization is useful to C and C++ code, they use "restrict": https://en.wikipedia.org/wiki/Restrict

Does LLVM implement optimization that only Rust can use, or at least those that cannot be used from C/C++?


&mut T pointers are basically restrict by default.

LLVM is adding some semantics specific to non-C or C++ languages. I’m on my phone so I can’t link you, but they’re adding an intrinsic related to infinite loops because languages like Rust have different semantics here.


Not anymore, LLVM was buggy about this and we had to remove this.


That’s why I said “basically”, it’s been fixed upstream and will be turned on again soon.


For the record, gcc and clang does this as well when using the restrict keyword. (I'm still a big fan of Rust though)

  int foo(int *restrict x, int *restrict y) {
    *x = 0;
    *y = 1;
    return *x;
  }
Compiles to:

  foo:
    movl	$0, (%rdi)
    movl	$1, (%rsi)
    xorl	%eax, %eax
    ret


Pony does use most LLVM optimizations and already IS faster than C++ with OpenMP so there's still a lot of catchup to do.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: