3 ms·
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
by chrismorgan 9y ago
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.
- muizelaar 9y agoLLVM 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
- andoma 9y agoFor 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
- wolf550e 9y agoThat optimization is useful to C and C++ code, they use "restrict": https://en.wikipedia.org/wiki/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++?
- steveklabnik 9y ago&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.
- Manishearth 9y agoNot anymore, LLVM was buggy about this and we had to remove this.
- steveklabnik 9y agoThat’s why I said “basically”, it’s been fixed upstream and will be turned on again soon.
- deleted 9y ago[deleted]
- rurban 9y agoPony does use most LLVM optimizations and already IS faster than C++ with OpenMP so there's still a lot of catchup to do.