3 ms·
> I'm surprised we don't have better, performance-neutral hardware support for safety constructs like bounds checking. It isn't necessary. Languages that have
by kazinator 4y ago
> I'm surprised we don't have better, performance-neutral hardware support for safety constructs like bounds checking.
It isn't necessary. Languages that have properly bounded arrays just emit the code and optimize it. E.g. if a loop steps a variable from i from 0 to N-1, accessing arr[i], the arr[i] bounds check can be moved out of the loop: if N-1 is within bounds, it's good to go.
I think the tooling being alluded to here is for checking bounds in programs that not only don't check it themselves, but don't pass around the information which makes it possible to know the size of an array.
The tooling has to build its own data structure to track objects and instrument the binary to intercept accesses; all of that is expensive.
- cjbprime 4y agoBounds checking isn't the only type of memory unsafety detected by sanitizers: they can also detect things like use-after-free, undefined behavior, and so on.
- Someone 4y ago> Languages that have properly bounded arrays just emit the code and optimize it. E.g. if a loop steps a variable from i from 0 to N-1 Modern languages have for value in arr, arr.forEach or equivalent. Such code makes it easier for compiler writers to make this optimization. Swift even chose to remove for loops that step a variable (https://github.com/apple/swift-evolution/blob/main/proposals/0007-remove-c-style-for-loops.md https://github.com/apple/swift-evolution/blob/main/proposals...), and I don’t think Rust has them, either. I think that’s the right decision. In modern languages, classical for loops are a code smell. And of course, golang goes the other way. https://go.dev/tour/flowcontrol/1 https://go.dev/tour/flowcontrol/1: Go has only one looping construct, the for loop. The basic for loop has three components separated by semicolons: - the init statement: executed before the first iteration - the condition expression: evaluated before every iteration - the post statement: executed at the end of every iteration
- pjmlp 4y ago> Modern languages have for value in arr, arr.forEach or equivalent. Such code makes it easier for compiler writers to make this optimization. Those features were modern 40 years ago, even Visual Basic (pre-.NET) already had them.
- kazinator 4y agoIf the compiler has good loop optimizations, the front end can turn foreach into an ordinary loop with a hidden variable: for (x in array) print(x) -> const __local0017 = array.len; for (var __local0018 = 0; __local0018 < __local0017; __local++) { const x = array[__local0018]; print(x); } The middle and back ends will infer that __local0018 doesn't exceed __local0017 which is the length of array and get rid of the bound check.