Live data from Hacker News

How much does Rust's bounds checking cost?

blog.readyset.io

51–60 of 195 posts

Re: How much does Rust's bounds checking cost?

#51
post #46

TL;DR - in the test bounds checking vs no checks showed no noticeable difference. Very good article though. Worth reading.

Not too long, did read : The benchmark went from 28.5ms to 32.9ms. That as a percentage is 15% and is huge, it’s not noise. The test is flawed in some way, the article is disappointing in that the author didn’t investigate further.

MySQL is a huge amount of code doing a variety of things in each query -- networking, parsing, IO, locking, etc. Each of those can easily have significant and hard to predict latencies.

Benchmarking that needs special care, and planning for whatever it is you want to measure. A million trivial queries and a dozen very heavy queries are going to do significantly different things, and have different tradeoffs and performance characteristics.

Re: How much does Rust's bounds checking cost?

#53

I've been shocked when I've heard C programmers being actually concerned about performance penalty of checks like, why bother? CPUs in next 2 years will win that performance anyway and your software will be safer

All of the CPUs? C runs a lot of places.

Re: How much does Rust's bounds checking cost?

#54
The article mentions measurement noise several times without addressing the uncertainty. It would help to add statistical tests, otherwise the spread could let us conclude the opposite of what is really happened, just because we are out of luck.

Re: How much does Rust's bounds checking cost?

#55

Can someone smarter than me enlighten me when you would consider disabling bounds checking for performance? In ways the compiler is not already doing so? The article starts with a bug that would have been prevented by bounds checking. It's like talking about how much faster a car would go if it didn't have to carry the extra weight of brakes.

In some very hot code (most times loops with math stuff were it prevents some optimizations and/or the compiler fails to eliminate it) it can lead to relevant performance improvements.

Because of this you might find some rust code which opts out of bounds check for such code using unsafe code.

But this code tends to be fairly limited and often encapsulated into libraries.

So I agree that for most code doing so is just plain stupid. In turn I believe doing it on a program or compilation unit level (instead of a case by case basis) is (nearly) always a bad idea.

Re: How much does Rust's bounds checking cost?

#56
post #6

I would expect an iterator into a slice to not incur any bounds checking, as the compiler can deduce the end pointer one time as start + size. So idiomatic looping should be maximally efficient you'd hope.

The compiler shouldn't have to deduce anything, an Iterator shouldn't have a bounds check to begin with. It ought to be using unsafe operations under the hood, because it can guarantee they will only be called with valid arguments.

Calling ‘next’ on an iterator involves a bounds check.

Re: How much does Rust's bounds checking cost?

#57
post #33

I am a Rust fan, but 10% degradation in performance (29ms to 33ms) is not "a pretty small change" nor "within noise threshold". If the accuracy of the tests are +/- 10% then that needs to be proven and then fixed. I didn't see any evidence in the article that there is, in fact, a 10% error, and it looks like there is a genuine 10% drop in performance.

To be clear, removing the bounds checks led to the observed performance degradation. Your statement beginning with "I am a Rust fan, but..." suggests that you might have interpreted it as the other way around

I certainly did. Thank you for pointing that out. That suggests then that the problem is the tests are bogus.

Re: How much does Rust's bounds checking cost?

#58

I've been shocked when I've heard C programmers being actually concerned about performance penalty of checks like, why bother? CPUs in next 2 years will win that performance anyway and your software will be safer

All of the CPUs? C runs a lot of places.

If you need perf, then consider

better algorithms, better data structures, multi-threading, branchless programming (except safety), data-oriented design

and then elimination of checks, not first.

Re: How much does Rust's bounds checking cost?

#59
post #48

What if a compiler were to only allow an array access when it can prove that it's in bounds? Wherever it can't you'd have to wrap the array access in an if, or otherwise refactor your code to help the compiler. Then you'd have no panicking at least and more predictable performance.

Rust has a tool for that, it's iterators. It is only limited however.

also `slice.get()` will return on option

and using a range check manually before an index will normally optimize the internal bounds check away

Post reply on HN