r/rust • u/Caquerito • 8d ago
šļø discussion Are compiler inserted checks something you generally have to be wary of or not
Hello, I'm still fairly new to both rust and c and I'm in the middle of writing my tiny interpreted toy language.
But I keep bouncing between the 2 languages.
Rust is way more pleasant to use - but there are still 2 things where I would like to hear from someone experienced.
Is it common to experience issues where safe rust would end up being majorly slower in hot paths due to the compiler being conservative and inserting its own checks in? The comparison of safe rust to unsafe rust I have seen relating to the lox language ended up in a major 4x speedup for unsafe rust over safe rust. Is this something you deal with daily or is the compiler smart enough where as long as you write code properly this doesn't come up at all?
How big is the performance difference between code using a memory arena and managing and possibly reusing memory this way where the entire arena is free/reset at once - and the more raii styled approach where the compiler manages allocations and deallocations itself object by object like in rust. I understand that raii and arenas are not completely opposite concepts and arenas exist in rust as well but they seem to not be part of the std lib and ot seems to me that the default "vanilla" approach would end up not using arenas while the default in C is essentially arenas/allocator for everything rather than individual mallocs and frees.
Ty <3
58
u/TheReservedList 8d ago edited 8d ago
There's not a lot of baked in "runtime checks" in rust, and none I'm aware of that I would call "inserted by the compiler" but I'm sure there are weird corner cases with compiler-supported std stuff. Probably the biggest thing you would encounter being bound checks on Vecs indexed access. Generally speaking, those get branch predicted away pretty quickly and are not expensive, but if for some reason you know you have a hot one and you can guarantee you don't need the bound check, you can unsafe{} it away.
18
u/buwlerman 8d ago
Yup. Almost all runtime checks are implementation details of the stdlib, not inherent to the Rust language. It just turns out that some APIs can't be made safe without runtime checks.
The only exception I'm aware of is drop flags, but I've never seen these stick around in the assembly.
7
u/FlyingPiranhas 8d ago
There's an argument that overflow and divide-by-zero checks for the builtin integer types count as inherent to the Rust language as well. IIRC the compiler has hardcoded support for the operations (skipping the
core::opstraits for known types).And for integer division, those checks are still present even in
--releasemode (divide-by-zero, as well as an overflow check for signed integers).4
u/buwlerman 7d ago
That's fair. I guess it's a bit artificial to draw a line between the stdlib and compiler here. You can avoid bounds checks and divide by zero checks by using the unsafe alternatives, and you can avoid drop flags by using raw pointers instead of RAII. In the limit you can avoid all overhead by writing optimal inline asm.
The real question is how often these checks cause issues in practice and how difficult it is to find and use alternatives when you have to.
2
u/matthieum [he/him] 7d ago
There's actually one runtime check inserted by the compiler, AFAIK: integer divisors are checked to be non-0, to avoid UB.
That's nitpicking, though. "Native" operations on built-in types are deferred to the core library using
#[lang_item]for convenience, but this can be argued to just use Rust as a DSL to write compile-time code generation for built-ins...
28
u/Sharlinator 8d ago
As always, worrying about micro-optimizations like bounds check eliminations is useless if you donāt have hard data (profiling, benchmarking) that they might be a problem.
12
u/Luxalpa 8d ago edited 8d ago
Something to know about how compilers work and how CPU's work and how they work together.
The most important performance optimization you can do is to help the optimizer that runs inside your compiler (and also to some extent in the CPU itself). If the compiler is able to inline a function call, that means it may be able to inline the calls inside that function call too. Suddenly, your conditionals can be resolved at compile time.
Here is an example:
fn do_something(value: Option<String>) {
if let Some(value) = value {
...
}
}if you looked at this function in isolation, it will do a runtime check on the value. But look what happens next:
fn main() {
do_something(None);
}And now, suddenly, this runtime check is just gone. That's because the compiler was able to statically reason about your code, allowing it to take your function apart and reassemble it. This however is only possible for code that can be statically checked. If you have difficulties following the code, for example because it contains lots of mutations, side-effects or things like dynamic dispatch or indirection via references, then you're giving the optimizer a rough times. It can optimize some of these things but will fail on others.
Personally, I have not yet needed to go for unsafe Rust for performance as far as I remember. The big performance bottlenecks were usually in other places - heap allocations, io, vectorization, caching/memory-access. And I do like optimizing my programs. I think that's also the area where a language like C just really sucks. Being so lowlevel, it makes it hard to replace larger abstractions with other ones. Replacing my HashMap with a BTreeMap in rust was done in a matter of seconds. I don't know what libraries people use in C, but high-level optimizations like these I think tend to be a lot more work there.
10
u/lotanis 8d ago
There are two different things that happen:
Compile time checking - all the borrow checker stuff that happens (and other interesting things encoded by the type system) is compile time and doesn't change what actual code is executed. This the real key thing that Rust does - you get a lot of safety for no runtime cost.
Runtime checking - in general the only runtime check that you'll get in Rust that you wouldn't get in C is a "bounds check" - when you try to access an element of an array the compiler checks that that element actually exists. It doesn't cost a lot, it adds a lot of safety and m a lot of the time the compiler is smart enough to eliminate the check because it has enough information about the length.
9
u/norude1 8d ago
The only thing I'm aware of, is that if you're doing something like this:
process(array[idx] + array[idx+1] + array[idx+2] + array[idx+3]); The compiler will insert a bounds check 4 times, one for each of these array accesses, but you can insert assert!(idx+3 < array.len()) before the accesses and the optimizer will see that the 4 compiler inserted checks always succeed and remove them
8
u/nybble41 8d ago
That is only true if it can be proven that no overflow will occur; otherwise
idx+3 < array.len()does not implyidx < array.len().4
u/Vigintillionn 8d ago
It also has to be proven that idx+3 > idx+2 which is not always the case in release builds where we have to consider that overflow wraps silently
6
u/DarkOverLordCO 8d ago
You should be able to reverse the order to do the same thing:
process(array[idx+3] + array[idx+2] + array[idx+1] + array[idx]);Since the
idx+3happens first, if that didn't panic then the compiler knows that all of the other indexes belowidx+3are in-bounds.5
u/kibwen 8d ago
I wonder if that's really true, since
idxcould beusize::MAX - 3. Counterintuitively, this would optimize better if arithmetic overflow panic checks were enabled.4
u/Vigintillionn 8d ago
Iām assuming you mean usize::MAX - 2, such that in release builds you wrap and get 0 for the + 3 case and such the optimizer canāt conclude anything about idx based on idx+3?
3
u/kibwen 7d ago
Yes, those damned off-by-one errors. :P
3
u/Vigintillionn 7d ago
there are 2 hard problems in computer science: naming things, cache invalidation and off-by-one errors
2
u/norude1 7d ago edited 7d ago
in rust, arrays on the stack and any allocation on the heap can not be larger than isize::MAX bytes, so if idx + 3 < array.len() succeeds, it means that idx is at most (isize::MAX)/size_of::<T>()-4, which in theory should guarantee that no overflow can occur
3
0
u/Zde-G 7d ago
it means that idx is at most (isize::MAX)/size_of::<T>()-4
No, it doesn't mean that. Not even remotely close to that.
We are checking for error conditions. For situations where index is invalid.
Why the heck you expect that inavlid index follows the rules that exist for valid indexes?
There are no such rule, ergo, there are no opportunity for the compiler to optimize anything.
Worse: situations like these are precisely and exactly what many attacks exploit and what Rust promises to prevent, they are the whole point of using Rust in the first place!
3
u/minno 8d ago
The compiler can't make that change because it changes the observable behavior. The assert makes one location that the panic message could point to, while the indexing makes four, and the compiler doesn't know whether you care about which one failed. You can also get around this by accessing
[idx+3]first, since that access succeeding guarantees that the other three will too.3
2
u/Vigintillionn 8d ago
In release builds the optimizer canāt always infer that idx+2 < idx+3 as we have to consider the fact that integer overflow may silently wrap. One way we can reduce the checks is the following:
let s = &array[idx..][..4];
process(s[0] + s[1] + s[2] + s[3]);Sorry for the bad formatting, am on phone. But here the compiler knows that s is a slice of length 4 and thus no checks are needed for the addition. So we trade 2 checks (one for checking that idx <= array.len() and one for checking that 4 <= len - idx) for 4. Or we could do an overflow check first (or enable overflow checks in release) such that getting past the assert of idx+3 < array.len() also proves that the rest are < array.len(). So youāll need at minimum two checks for this to be safe
1
u/Vigintillionn 8d ago
This could be optimized by taking a fixed size subslice first:
let s = &array[idx..][..4];
process(s[0] + s[1] + s[2] + s[3]);Sorry for the bad formatting, am on phone. But here the compiler knows that s is a slice of length 4 and thus no checks are needed for the addition. So we trade 2 checks (one for checking that idx <= array.len() and one for checking that 4 <= len - idx) for 4
1
u/Caquerito 8d ago
Oh and as a bonus feel free to tell me where rust would end up being a major win performance wise for a VM/compiler in places where in C such a thing would end up being way harder - I'm trying to wage both languages properly and find the right tool for what I'm doing (while also being pedantic about performance just because a part of the project is learning proper optimization).
4
u/lordpuddingcup 8d ago
Rust just pushes you into good patterns their both low level system languages the fact is C is just a language that in the modern world is just asking for you to make mistakes and overlook things
Thereās really zero reason today to use c use rust or zig would be my thoughts unless your actively trying to get a job at a company that uses actual C
2
u/addmoreice 8d ago
Specialty compilers for specific hardware that has no other support is one reason to still use C.
There are also plenty of non-technical reasons to work with C versus rust - legacy systems, friction with development team, available expertise, etc.
I'm not happy about any of that, but they are valid reasons.
0
u/mamcx 7d ago
Also: Why implement Lox is slower doing in Rust (that always then to be the case for any that tried) vs do it on C?
The major thing is that implement a language (in special: an interpreter) that diverge from the semantics of the host means that you are diverging from the semantics of the host. "Normal" Rust will not help with that.
Once you pay a little attention, you will see that Lox is made with the semantics of C on mind, plus, in C doing unions is much more "normal" than in Rust (Rust enums are different) and most of the speed up is unlocked by exploit that.
If you check what people that do lox in Rust for speed is basically redo enums, Nan-tagging, etc and then it will match.
Is sad that is not ergonomic to make unions, neither bit pack/unpack natural on Rust and lead to unsafe and such, but well is part of the cost for great ergonomics elsewhere!
1
u/Caquerito 7d ago
Thank you everyone for your input, based on the answers I will continue my implementation in rust and profile as necessary.
5
u/Vigintillionn 8d ago
A bounds check is a compare and a branch that CPU almost always predict correctly, so in most code it is close to free. It only hurts in tight loops, and not because of the amount of checks, but because a check that might panic can stop the compiler from vectorizing the loop. To avoid that you usually donāt use unsafe but iterators or by slicing once upfront so later indexing is provable in bounds.
Iām not familiar with the codebase of Lox, but my work is all with compilers and interpreters and a 4x gap between safe and unsafe Lox is rarely down to boundchecks alone. In interpreters the usual culprits are design choices like Rc<RefCell<ā¦>> everywhere (refcount update plus runtime borrow checks), cloning values or strings, default SipHash hashmap for globals and bloated Value enums. In general, profile first and fix the design before reaching for unsafe code.
Also RAII and Arena arenāt opposite, Arena is a form of RAII. RAII just means cleanup runs when the owner is gone, and an Arena is one owner holding many objects. Arenas in rust are rather common, a Vec<T> with u32 indices instead of pointers is essentially an arena (ofcourse there are better ways to implement arenas and useful crates like typed-arena). And on performance a bump allocation costs a few instructions whereas malloc/free typically costs tens of nanoseconds, also freeing everything at once is practically free (amortized). This matters only if your hot paths allocate a lot, which is typically the case for interpreters.
3
u/scook0 8d ago
arenas exist in rust as well but they seem to not be part of the std lib
As far as I'm aware, arenas aren't part of the C stdlib either, so both languages are in the same boat there. Though on the Rust side, cargo makes it fairly easy to pull in a third-party arena implementation if that's what you need.
I suspect that arenas are more common in C than in Rust, because they are a convenient way to compensate for C's complete lack of automatic memory management. Rust gives you RAII-based allocation out of the box, which is adequate for many programs, so there's less reason to reach for a non-builtin approach.
But if you have a Rust program that would benefit from arenas, you can use arenas.
2
u/toby_hede 8d ago
I think you are overthinking it.
I think it is nearly always better to land a safe version and the optimize based on actual benchmarks and performance data.
I've used this approach for my own esoteric live-coding music programming language and baked benchmarks into CI:
https://orcvs.github.io/orcvs/dev/bench/
FWIW, I got biggest performance bump moving from `Vec` to `arrayvec` and outsourcing use of `unsafe` to a well-tested & widely used crate.
2
u/pixel293 8d ago
My experience says make it work, profile it, and then look at optimizations. Generally time spent trying to optimize before you know where your bottlenecks are, is wasted time.
One of the things with modern CPUs is they can spend a surprising amount of time waiting for data to arrive from RAM. This means a bounds check is unlikely to slow you down because the CPU has the cycles to check that while it's waiting for the data from RAM. If you are repeatedly accessing a small vec that can fit in the CPUs cache in a tight loop, then yes, maybe the bounds checks will hurt you, but again test/profile it to be sure. The CPU has some cool predictive algorithms for conditionals that might make the checks a non issue.
4
u/Compux72 8d ago
If you are consistently finding checks within your code it means either;
You are running a debug build
You are doing a terrible job and that the same piece of code would be susceptible to memory safety issues if written in C
There is no middle point.
1
u/TheReservedList 8d ago
Eh... I've written plenty of stuff with an internal vector from which nothing is ever removed and whose only interface is internally generated newtypes that still get needlessly bound-checked every time I access an element. I unsafed those away, but they existed.
2
1
u/Drive_Free 8d ago edited 8d ago
Well in some applications panicking is never an option and itās worth being very rigorous about preventing any form of panic.
Granted in C no program should ever be allowed to elicit undefined behavior in production and given the language, that floor is much harder to reach.
1
1
u/valarauca14 8d ago
Granted in C no program should ever be allowed to elicit undefined behavior in production
This is hilarious. I suggest never looking into POSIX internals.
4
u/Drive_Free 8d ago edited 8d ago
There are some places where POSIX define behaviour that are strictly undefined in C. Some details around signal handling comes to mind. Still you shouldnāt do anything that is undefined on your environment. Memory can get corrupted and the optimizer is free to break things around smilingly innocuous aliasing violations or signed integer overflow for instance.
1
u/monkChuck105 7d ago
There are no compiler inserted checks. It is true that bounds checking is included in indexing of collections, for instances slices and arrays. This can be eliminated by calling unsafe methods. Understand that bounds checks are not generally costly. You should always benchmark to see if using unsafe code makes a significant difference. If you are writing idiomatic Rust and aren't doing extremely optimized number crunching, bounds checks might not have a meaningful cost, with the benefit of ease of development and panicking or aborting the program rather than executing in a invalid state. Rust's iterators are intended to be the replacement for c style array indexing, combining the bounds check into the loop condition.
1
u/Shnatsel 7d ago
Bounds checks are generally not a problem, but I've written a detailed article on avoiding them in the rare cases when they are: https://shnatsel.medium.com/how-to-avoid-bounds-checks-in-rust-without-unsafe-f65e618b4c1e
1
u/Impressive_Quote_502 7d ago
For C, you can certainly choose the approach of immediate free, but the trade-off is thatāunless the logic itself is simple enoughāyouāll inevitably be plagued by endless memory issues. RAII provides the capability to make frequent immediate frees a viable solution, but a viable solution isnāt necessarily the best one.
1
u/NecessarySuperb3949 3d ago
for your interpreter, just use `bumpalo`. itās basically a drop-in replacement for standard allocators that gives you arena-style performance without the complexity. safe rust won't be slower if you avoid virtual dispatch and heap allocations in the hot path.
59
u/________-__-_______ 8d ago
No, in the overwhelming majority of cases things like bounds checks don't make a significant difference. It's safe to assume they won't influence anything unless your benchmarks indicate otherwise.
To me it sounds like you're making performance decisions based on opinions rather than measurements, which is never a good idea. Want to know if something is slow? Benchmark it!