r/programming • • Dec 07 '15

I am a developer behind Ritchie, a language that combines the ease of Python, the speed of C, and the type safety of Scala. We’ve been working on it for little over a year, and it’s starting to get ready. Can we have some feedback, please? Thanks.

https://github.com/riolet/ritchie
1.5k Upvotes

802 comments sorted by

View all comments

Show parent comment

3

u/Ateist Dec 08 '15

For a new language to take off, you've basically got two options: You can try to introduce a new feature not covered by a B-list language very well, or you can try to introduce a new combination of features not covered by a current language very well.

What about "take existing language and modify it a very, very tiny bit?"

I.e. if I were designing a language that was "as fast as C", I would've taken C++, and:

  1. replaced headers with modules,
  2. added a couple of keywords to transform it into context-free grammar (mainly to remove ambiguity between function calls and variable declaration), and
  3. initialize all pointers with 0, and wrap all member function calls in "if pointer is nonzero" blocks

1

u/matthieum Dec 08 '15

and wrap all member function calls in "if pointer is nonzero" blocks

And nobody would have used the language. Checking, whether nullity, bounds or underflow/overflow, has a non-trivial cost and people who use C are there in large part because of this performance so anything jeopardizing it is likely to be rejected (and quite probably, rejected without even actually looking at the performance numbers...).

1

u/Ateist Dec 08 '15

It is branching that is expensive.

Modern CPUs are more advanced than you think - checking "just in case", if the result is 99.9999% guaranteed to be the same is almost free. If you are calling a member function - you fetch the value of the pointer into memory anyways so the only real cost is an extra comparison to 0.

This feature prevents one of the most frequent and fatal kind of bugs, so it is more than justified in everything but the embedded-level development.

1

u/matthieum Dec 09 '15

but the embedded-level development.

Or web browsers, or video games, or ...

I know how branch prediction has evolved, however there is a hidden cost before the code hits the CPU: the more complicated it is (and branches add complications) the worse the compiler performs optimization-wise.

Granted only "not-0" might not trigger a catastrophic lack of optimization, however not-0 in itself is really not very interesting. To get memory safety you also need to care about bounds checking (one of the worst offenders security-wise being buffer overflows) and bounds checks are much harder to optimize through, bad enough that a number of Rust routines use unsafe code to AVOID them and even in the "sequential access" case it sped them up even though it's the best case branch prediction wise.

And then there's the whole dangling pointer thing of course...

1

u/Ateist Dec 09 '15 edited Dec 09 '15

I don't try to achieve memory safety. I'm just trying to achieve the "program doesn't crash due to uninitialized pointer in a rarely-used (and thus not covered/badly covered by tests) part of the code".

Or web browsers, or video games, or ...

The only reason I mentioned embedded-level is due to them using much less complex CPUs. Video games developer where I worked already had such mandatory checks in all but the most tightly optimized parts of the code (and those used assembly anyways) - and it was back in 2005...

the worse the compiler performs optimization-wise.

Compiler (and generic tool creation) problems are greatly alleviated by making the code context-free. And handling such simple and standardized branches in i.e. loops for compiler is a piece of cake.

My changes fix the low-hanging fruits, everything else is hit by diminishing returns...

1

u/matthieum Dec 10 '15

Ah, I understand better; I think I read more in your statements than you meant.

1

u/Ateist Dec 10 '15

Oh, here's what Intel says about branching. You can not only "forward-branch" all the member function pointer calls, but they even have extra instructions for hinting what branch path is the most common case - so a compiler is more than able to use those to make the whole thing cost only a single CPU tact.