Would it be possible to create a language similar to Ruby/Python with static typing that had the speed/memory usage of a compiled C program? [closed]

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/288467

Question

One of the main drawbacks of Ruby/Python is performance. I understand that they are interpreted and C is compiled. (And there are things like JRuby which do JIT compilation with Ruby). But they never manage to get the same speed/memory usage as a compiled program. Would it be possible to create a new language that had the same syntax as Ruby for example (with one change: static typing) that compiled down to something as fast as a C program?

I'm trying to think of what other differences there are other than static typing than prevent you from getting the same speed. (Maybe allocating buffers of a fixed size as opposed to an abstraction which handles this for you). But it seems like you could get pretty close?

If so, why has no one created a compiled version of Ruby/Python with static typing?

Was it helpful?

Solution

Just adding explicit type annotations to Ruby wouldn't do the trick. One key characteristic of Ruby is that any method can be redefined at runtime. So even if you know that some value will always be an instance of class C, that doesn't allow you to do things like inlining.

Note that in Ruby, even loops are actually implemented as method calls. So your compiler couldn't even assume that a loop is... actually a loop, since #each could be redefined at runtime.

You could design a language with "Ruby syntax", but take away all the runtime metaprogramming. That could be compiled to efficient machine code, but it wouldn't be Ruby. The key characteristic which makes Ruby what it is, is the extreme flexibility to modify almost everything dynamically, at runtime.

You could conceivably write an incredibly sophisticated compiler which would do global analysis of an entire program, and determine whether any of those "dynamic" features are actually used or not. In many cases, this would practically require something close to "strong AI". Not practical.

Another option, which has been done with other languages and could be done in Ruby, would be to write a JIT compiler which does optimizations like inlining based on the assumption that the current class and method definitions won't change, and backs out of those optimizations if they do. That is the way to make Ruby code very, very fast. Unfortunately, nobody has done it yet.

OTHER TIPS

Performance doesn't always come down to interpreted vs. compiled. There are so many other factors that can influence the performance equation:

  1. The nature of the software can favor programmer productivity over clock cycles.
  2. Other factors are more important, like network and database latency.
  3. Compilation and interpretation are a gray area anyway. Is a JIT interpreted or compiled?
  4. Byte code and certain programming language constructs can make optimizations possible that wouldn't otherwise be available.

Cython and Numba are examples of what this would look like for Python, and yes, you can get performance comparable to C code. Cython is an extension of the Python language that adds type annotations and makes it easy to call C functions. Numba uses hints provided as Python @decorators to inform it of the data types for which it will generate efficient vectorized C code, and will raise an error if the resulting function is called with incorrectly typed arguments.

Licensed under: CC-BY-SA with attribution
scroll top