Convert Ruby to low level languages?

后端 未结 6 760
Happy的楠姐
Happy的楠姐 2020-12-31 05:09

I have all kind of scripting with Ruby:

rails (symfony)
ruby (php, bash)
rb-appscript (applescript)

Is it possible to replace low level lan

6条回答
  •  灰色年华
    2020-12-31 06:05

    Such a compiler would be an enormous piece of work. Even if it works, it still has to

    1. include the ruby runtime
    2. include the standard library (which wasn't built for performance but for usability)
    3. allow for metaprogramming
    4. do dynamic dispatch
    5. etc.

    All of these inflict tremendous runtime penalties, because a C compiler can neither understand nor optimize such abstractions. Ruby and other dynamic languages are not only slower because they are interpreted (or compiled to bytecode which is then interpreted), but also because they are dynamic.

    Example

    In C++, a method call can be inlined in most cases, because the compiler knows the exact type of this. If a subtype is passed, the method still can't change unless it is virtual, in which case a still very efficient lookup table is used.

    In Ruby, classes and methods can change in any way at any time, thus a (relatively expensive) lookup is required every time.

    Languages like Ruby, Python or Perl have many features that simply are expensive, and most if not all relevant programs rely heavily on these features (of course, they are extremely useful!), so they cannot be removed or inlined.

    Simply put: Dynamic languages are very hard to optimize, simply doing what an interpreter would do and compiling that to machine code doesn't cut it. It's possible to get incredible speed out of dynamic languages, as V8 proves, but you have to throw huge piles of money and offices full of clever programmers at it.

提交回复
热议问题