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?

  • 2
    There's no reason it wouldn't be possible. For example, look at Apple's Swift, which is a higher level language that compiles to native code. – GrandmasterB Jul 2 '15 at 4:12
  • 3
    Look also into Common Lisp and the SBCL compiler – Basile Starynkevitch Jul 2 '15 at 4:43
  • There are continuous advances in how dynamic languages can be implemented efficiently. Julia is a gradually-typed language for scientific programming and number crunching. It achieves excellent performance by using the LLVM for JIT-compilation. PyPy is a high-performance Python VM written in RPython, a subset of Python that is transpiled to C. The limiting factor for optimizations is that most dynamic language implementations are online, whereas C is usually pre-compiled. Nevertheless, we are at a point in VM technology where it makes sense to compare VMs by the assembly they JIT-compile. – amon Jul 2 '15 at 5:47
  • What do you still consider similar to Python? Maybe C# or so is already what you mean... – RemcoGerlich Jul 2 '15 at 6:13
  • 3
    recommended reading: Why is “Is it possible to…” a poorly worded question? – gnat Jul 2 '15 at 6:14

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.

  • Update: Truffle does what I described in the last paragraph, and when it is ready for "production" use, it looks like it will be far, far faster than any other Ruby implementation. On the other hand, Crystal is a language which is designed to be as Ruby-like as possible, but still possible to compile to efficient native code. – Alex D Apr 21 '16 at 3:53

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.
  • #4 seems like the most where I'm stuck - can you expand on that one? thanks for the answer! – Brian Armstrong Jul 2 '15 at 5:40

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.