←back to thread

837 points turrini | 1 comments | | HN request time: 0s | source
Show context
titzer ◴[] No.43971962[source]
I like to point out that since ~1980, computing power has increased about 1000X.

If dynamic array bounds checking cost 5% (narrator: it is far less than that), and we turned it on everywhere, we could have computers that are just a mere 950X faster.

If you went back in time to 1980 and offered the following choice:

I'll give you a computer that runs 950X faster and doesn't have a huge class of memory safety vulnerabilities, and you can debug your programs orders of magnitude more easily, or you can have a computer that runs 1000X faster and software will be just as buggy, or worse, and debugging will be even more of a nightmare.

People would have their minds blown at 950X. You wouldn't even have to offer 1000X. But guess what we chose...

Personally I think the 1000Xers kinda ruined things for the rest of us.

replies(20): >>43971976 #>>43971990 #>>43972050 #>>43972107 #>>43972135 #>>43972158 #>>43972246 #>>43972469 #>>43972619 #>>43972675 #>>43972888 #>>43972915 #>>43973104 #>>43973584 #>>43973716 #>>43974422 #>>43976383 #>>43977351 #>>43978286 #>>43978303 #
monkeyelite ◴[] No.43972915[source]
> If dynamic array bounds checking cost 5% (narrator: it is far less than that)

It doesn’t work like that. If an image processing algorithm takes 2 instructions per pixel, adding a check to every access could 3-4x the cost.

This is why if you dictate bounds checking then the language becomes uncompetitive for certain tasks.

The vast majority of cases it doesn’t matter at all - much less than 5%. I think safe/unsafe or general/performance scopes are a good way to handle this.

replies(3): >>43973436 #>>43975046 #>>43976715 #
Aurornis ◴[] No.43976715[source]
> It doesn’t work like that. If an image processing algorithm takes 2 instructions per pixel, adding a check to every access could 3-4x the cost.

Your understanding of how bounds checking works in modern languages and compilers is not up to date. You're not going to find a situation where bounds checking causes an algorithm to take 3-4X longer.

A lot of people are surprised when the bounds checking in Rust is basically negligible, maybe 5% at most. In many cases if you use iterators you might not see a hit at all.

Then again, if you have an image processing algorithm that is literally reading every single pixel one-by-one to perform a 2-instruction operation and calculating bounds check on every access in the year 2025, you're doing a lot of things very wrong.

> This is why if you dictate bounds checking then the language becomes uncompetitive for certain tasks.

Do you have any examples at all? Or is this just speculation?

replies(1): >>43976765 #
monkeyelite ◴[] No.43976765[source]
> Your understanding of how bounds checking works in modern languages and compilers is not up to date.

One I am familiar with is Swift - which does exactly this because it’s a library feature of Array.

Which languages will always be able to determine through function calls, indirect addressing, etc whether it needs to bounds check or not?

And how will I know if it succeeded or whether something silently failed?

> if you have an image processing algorithm that is literally reading every single pixel one-by-one to perform a 2-instruction operation and calculating bounds check on every access in the year 2025, you're doing a lot of things very wrong

I agree. And note this is an example of a scenario you can encounter in other forms.

> Do you have any examples at all? Or is this just speculation?

Yes. Java and python are not competitive for graphics and audio processing.

replies(2): >>43984858 #>>43989886 #
MonkeyClub ◴[] No.43984858[source]
> Java and python

Java and Python are not on the same body of water, let alone the same boat.

You can see some comparisons here:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

replies(1): >>43989659 #
monkeyelite ◴[] No.43989659{3}[source]
Yes. And why is that?
replies(1): >>43989892 #
Aurornis ◴[] No.43989892{4}[source]
Interpreted languages and garbage collection.

Bounds checking barely registers as a factor.

replies(1): >>43991013 #
1. monkeyelite ◴[] No.43991013{5}[source]
You can write Java that uses arrays that are allocated once - will it have loops as fast as C? Why not?

And furthermore I don’t suspect you’re proposing we should be using C + bounds checking (that’s already a Gcc flag?). But rather bounds checking is one of many safety features.

The whole pitch of Java is exactly what OP said - let’s just pay 10-30% cost to get these nice portability and memory benefits over C++, and it didn’t work that way especially as memory speeds have diverged from CPU.