-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Update speaker notes for arrays.md with clarification on runtime check optimization #2781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
According to the error I got ``` Compiling playground v0.0.1 (/playground) error: this operation will panic at runtime --> src/main.rs:3:5 | 3 | a[7] = 0; | ^^^^ index out of bounds: the length is 5 but the index is 7 | = note: `#[deny(unconditional_panic)]` on by default error: could not compile `playground` (bin "playground") due to 1 previous error ``` Array accesses seem to be checked at compile time.
src/tuples-and-arrays/arrays.md
Outdated
@@ -27,8 +27,8 @@ fn main() { | |||
later. | |||
|
|||
- Try accessing an out-of-bounds array element. Array accesses are checked at | |||
runtime. Rust can usually optimize these checks away, and they can be avoided | |||
using unsafe Rust. | |||
compile time. Rust can usually optimize these checks away, and they can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original meaning was correct, and important. In more sophisticated cases than this simple example, Rust may very well leave the runtime bounds-checks in place, and these can result in a substantial performance overhead above unchecked languages like C++.
It's difficult to demonstrate this in a short code sample because the checks are often optimized away at compile time -- in this case to an unconditional panic. I suspect black_box
could be useful to avoid this optimization. Do you want to try adjusting the example so that it shows the runtime bounds check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see what you mean. I tried using black_box
but it seems to not prevent the compile-time safety checks. I changed the out-of-bound index to be returned from a function and add that example to below the note. PTAL. Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! The speaker note still says "compile time" though. It is also a good idea to add a sentence about how the optimization is so good that it's hard to give an example of runtime checks failing, and here requires a function to generate the index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. PTAL again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks!!
(just needs formatting fixed) |
According to the error I got from trying out-of-bound index, array accesses seem to be checked at compile time.